title: Trying to create Circles using MouseListener and MouseMotionListener - what I am doing wrong?
body: <p>I'm trying to create Circles in JFrame using JComponent. Here's what I'm trying to achieve:<a href="https://i.stack.imgur.com/rUH0N.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rUH0N.gif" alt="What I&#39;m trying to achieve."></a></p>
  
  <p>And here's what's happening with my code:<a href="https://i.stack.imgur.com/Rpkqt.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rpkqt.gif" alt="enter image description here"></a></p>
  
  <p>I have no idea what's causing this problem. Here's my code:</p>
  
  <p>CircleViewer.java</p>
  
  <pre><code>import javax.swing.JFrame;
  import java.awt.event.*;
  
  public class CircleViewer
  {
      public static void main(String[] args)
      {
          final CirclePanel panel = new CirclePanel();
          class MousePressListener implements MouseListener, MouseMotionListener
          {
              public void mouseClicked(MouseEvent event) { }
              public void mouseEntered(MouseEvent event) { }
              public void mouseExited(MouseEvent event) { }
              public void mouseWheelMoved(MouseWheelEvent event) { }
              public void mouseMoved(MouseEvent event) { }
  
              public void mousePressed(MouseEvent event)
              {
                  var x = event.getX();
                  var y = event.getY();
                  panel.addCircle(x, y);
              }
  
              public void mouseDragged(MouseEvent event)
              {
                  var x = event.getX();
                  var y = event.getY();
                  panel.moveTo(x, y);
              }
  
              public void mouseReleased(MouseEvent event)
              {
                  panel.finalMove();
              }
          }
          MousePressListener listener = new MousePressListener();
          panel.addMouseListener(listener);
          panel.addMouseMotionListener(listener);
  
          JFrame frame = new JFrame("Circle Shapes");
  
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          frame.add(panel);
          frame.setVisible(true); 
      }
      private static final int FRAME_WIDTH = 700;
      private static final int FRAME_HEIGHT = 500;
  }
  </code></pre>
  
  <p>CirclePanel.java</p>
  
  <pre><code>import java.awt.Graphics;
  import java.awt.Graphics2D;
  import javax.swing.JComponent;
  import java.util.ArrayList;
  import java.awt.Color;
  import java.lang.Math;
  import java.awt.BasicStroke;
  import java.awt.Stroke;
  
  public class CirclePanel extends JComponent
  {
      private int lineX;
      private int lineY;
      private boolean isDraged;
      private ArrayList&lt;Circle&gt; circleList;
      private BasicStroke dashLine;
      public CirclePanel()
      {
          this.circleList = new ArrayList&lt;Circle&gt;();
          this.isDraged = false;
          this.lineX = 0;
          this.lineY = 0;
          this.dashLine = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{6}, 0);
      }
  
      public void addCircle(int x, int y)
      {
          lineX = x;
          lineY = y;
          isDraged = true;
          circleList.add(new Circle(x, y, 0, Color.RED));
          repaint();
      }
  
      public void moveTo(int x, int y)
      {
          var circleTemp = circleList.get(circleList.size() - 1);
          isDraged = true;
          var tempR = (int)Math.sqrt(Math.pow(x - circleTemp.get(0), 2) + Math.pow(y - circleTemp.get(1), 2));
          System.out.println(tempR);
          var tempX = circleTemp.get(0) - (tempR / 2);
          var tempY = circleTemp.get(1) - (tempR / 2);
  
          circleList.get(circleList.size() - 1).setCords(tempX, tempY, tempR);
          lineX = x;
          lineY = y;
          repaint();
      }
      public void finalMove()
      {
          isDraged = false;
          circleList.get(circleList.size() - 1).setColor(Color.BLUE);
          repaint();
      }
  
      public void paintComponent(Graphics g)
      {
          Graphics2D g2 = (Graphics2D)g;
          Stroke defaultStroke;
          defaultStroke = g2.getStroke();
          if (!circleList.isEmpty())
          {
              if (isDraged)
              {
                  g2.setColor(Color.RED);
                  g2.setStroke(dashLine);
                  g2.drawLine(circleList.get(circleList.size() - 1).get(0), circleList.get(circleList.size() - 1).get(1), lineX, lineY);
              }
              for (Circle circle : circleList) 
              {
                  g2.setStroke(defaultStroke);
                  circle.draw(g2);
                  isDraged = false; //this is prob reduntant
              }
          }
      }
  }
  </code></pre>
  
  <p>And finally, Circle.java</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.geom.Ellipse2D;
  
  public class Circle
  {
      private int x;
      private int y;
      private int radius;
      private Color color;
  
      public Circle(int x, int y, int radius, Color color)
      {
          this.x = x;
          this.y = y;
          this.radius = radius;
          this.color = color;
      }
  
      public int get(int option)
      {
          switch (option)
          {
              case 0:
                  return this.x;
              case 1:
                  return this.y;
              case 2:
                  return this.radius;
          }
          return 0;
      }
  
      public Color get()
      {
          return this.color;
      }
  
      public void setCords(int x, int y, int r)
      {
          this.x = x;
          this.y = y;
          this.radius = r;
      }
      public void set(int option, int value)
      {
          switch (option)
          {
              case 0: //set x
                  this.x = value;
                  break;
              case 1:
                  this.y = value;
                  break;
              case 2:
                  radius = value;
                  break;
          }
      }
  
      public void setColor(Color color)
      {
          this.color = color;
      }
  
      public void draw(Graphics2D g2)
      {
          g2.setColor(color);
          g2.draw(new Ellipse2D.Double(x, y, radius, radius));
      }
  }
  </code></pre>