I need to update my graphic, In which there are rectangulars. And at each step I need to highlight and modify the status of some of them. But now in my code, for each repaint(), it draw a totally new picture. I have read the followint similar quesion: Keeping draw graphics - removing super.paintComponent ,which cannot solve my problem. The next time it calls repaint(), those rectangulars disappear. It means it erases the prevous graphic. Please help! Here is the code: ("firstPaint = false" is just before the next repaint())
@Override
protected void paintComponent(Graphics g2)
{
super.paintComponent(g2);
final Graphics2D g = (Graphics2D) g2.create();
try{
if(firstPaint){
int status;
g.setColor(Color.BLACK);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
status = current[i][j];
if(status == 1)
{
g.fillRect(j * 20, i * 20, 20, 20);
}
}
}
}
if(executeResult == 2){
g.setColor(Color.BLUE);
for(Cell c:highlightedCells){
g.drawRect(c.j * 20, c.i * 20, 20, 20);
}
}
if(executeResult == 1){
g.setColor(Color.RED);
for(Cell c:highlightedCells){
g.fillRect(c.j * 20, c.i * 20, 5, 5);
}
}
}
finally{
g.dispose();
}
}