title: Java JPanel drawing shapes
body: <p>I am for the first time working with JPanel and drawing basic shapes on the JPanel.</p>
  
  <p>I have written code for the shape like this:</p>
  
  <pre><code>public class Shape extends JPanel{
  
  int x,y;
  
  public Shape(int x, int y){
      this.x = x;
      this.y = y;
  }
  
  public void paint(Graphics g){
      super.paint(g);
      g.setColor(Color.black);
      g.drawRect(x, y, 20, 20);
      }
  }
  </code></pre>
  
  <p>I have another class where I will be using this shape. It extends JFrame and implements MouseListener. On this JFrame I have put the JPanel it is called simply "panel".</p>
  
  <p>I have the method, which reads the mouse position, and draws the shape on the "panel".</p>
  
  <pre><code>public void mouseClicked(MouseEvent e){
      Shape shape = new Shape(e.getX(),e.getY());
      panel.add(shape);
      panel.revalidate();
      panel.repaint();
  }
  </code></pre>
  
  <p>The problem is that it doesn't draw the shape on the coordinate where my mouse is. It just draws on panel at the upper-side and draws, them in a line.</p>
  
  <p>Thank you for you answers.</p>