Add items to an array repaint problem
Hey, im having some problems with making a 2d canvas. I have made a canvas that adds images to it when i click. I want to store the x and y coordinates of each image i add into an array when they are added to the canvas.
The problem i am having is that if i resize the window and left click the code performs the add item to array hundreds of times because of the repaint, heres my code :
Code:
public void mouseClicked(MouseEvent e)
{
if(this.contains(e.getX(),e.getY()))
{
// set last mouse click position
last_X = e.getX();
repaint();
//add xposition to the array
Integer xpos=new Integer(last_x);
imageposx.add(xpos);
//set image to draw as 1
paintimage = 1;
}
}
public void paintComponent(Graphics g)
{
if (paintimage == 1)
{
drawthis(g);
}
}
public void drawthis(Graphics g)
{
g.setColor (Color.BLACK);
g.fillRoundRect(last_X, last_Y, 200, 200, 2, 2);
}
So im really just looking for a way to add the values to the array when i click but without it adding values on each repaint of the graphics context. Any help would be good :)