I'm currently working with painting in Java and have run into an issue with repaint() erasing the previously drawn shape. Here is an example of my issue:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.LIGHT_GRAY);
click.paintPiece(g);
}
public void paintPiece(Graphics g) {
int x = (getMouseX())*70 + 10;
int y = mover.getRow()*70 + 10;
g.fillOval(x, y, 50, 50);
}
Every time the mouse is clicked, the paintPiece() method is called within the paintComponent() method. The problem I have encountered from this is that the previously drawn oval is erased when the new oval is created. Something also to note is that on each mouse click I am repainting the window. How can I paint a new oval without erasing the previously drawn one?
ArrayList<Ellipse2D>
and fill it with a new item on mouse click. In your paintComponent(...)
method, iterate through the List, drawing each shape.getGraphics()
on the BufferedImage, and then draw that BufferedImage in your paintComponent method. Don't forget to dispose of the BI's Graphics object when done using it.