0

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();
    }
}
Community
  • 1
  • 1
user3182473
  • 63
  • 1
  • 9
  • 4
    First rule of Swing, you don't control the paint process. Painting can occur for any number of reasons many of which you don't/can't control. Never try and control the state of the output from within the `paintComponent` method itself. Instead, define some kind of controlling mechanism which is independent of paint process. And yes, paint may be called a number of times when window is first made visible... – MadProgrammer Jun 24 '14 at 22:17
  • 2
    Paint is also a destructive process, you are expected to completely re-draw the output on each request, this means that your `firstPaint` block is actually meaningless and should be painted every time. – MadProgrammer Jun 24 '14 at 22:21
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem, this will reduce the guess work and produce better response – MadProgrammer Jun 25 '14 at 00:50
  • Thank you for answering. And sorry, i didn't make it clear. i need to keep the previous graphics instead of re-drawing the entire graphic. e.g. only the target cell would change its status(color). However, in my code, each time it calls repaint(), it clear the previous one. – user3182473 Jun 25 '14 at 07:09
  • alt-printscreen should do it. – vikingsteve Jun 25 '14 at 07:10
  • 1
    A common approach to achieve this goal is to *not* paint on the screen, but into a `BufferedImage` instead. There you have complete control over what is painted and what is preserved, pixel for pixel. In the `paintComponent` method, you then have to paint *only* the image, using `g.drawImage(bufferedImage,0,0,null)` - it will then paint the current state of the image, whatever it contains. (NOTE: This is only a way to achieve what you described - which does not mean that what you described is the right way to go...) – Marco13 Jun 25 '14 at 08:21
  • Thanks all! Eventually I changed to use Canvas, and override the update(),it give what I need. – user3182473 Jun 26 '14 at 07:22

0 Answers0