I am for the first time working with JPanel and drawing basic shapes on the JPanel.
I have written code for the shape like this:
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);
}
}
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".
I have the method, which reads the mouse position, and draws the shape on the "panel".
public void mouseClicked(MouseEvent e){
Shape shape = new Shape(e.getX(),e.getY());
panel.add(shape);
panel.revalidate();
panel.repaint();
}
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.
Thank you for you answers.