8
I trying to create a JPanel that draws rectangles. The Panel needs to draw alot of rectangles, but they dont move.
One solution to my problem was to create an list with all the rectangles i already created and draw they all in every call of "Paint". But there is a lot of rectangles and it slows the computer.
I also tried to use repaint(x, y, height, width) to rapaint just the space of the new rectangle but it did not work. (JPanel keeps erasing previous rectangles.)
In sort, i need to draw rectangles that wont disappear every paint. Or a paint method that wont erase previous draws, or wont paint the background.
That is part of my JPanel class:
class MyPanel extends JPanel{
private int x, y, size;
private Color c;
public void DrawRect(int x, int y, int size, Color c){
this.x = x;
this.y = y;
this.size = size;
this.c = c;
repaint();
}
@Override
public void
paint(Graphics g) {
g.setColor(c);
g.fillRect(x, y, size, size);
}
}