Double cache solution for JFrame flickering in Java

Double cache solution for JFrame flickering in Java

problem causes

When writing a small Java game, I use the inherited JFrame

public class flyView extends JFrame {
    
    
	.....................................................................................
}

In the following code, write paint to draw

public void paint(Graphics graphics){
    
    
        super.paint(graphics);  /
        drawBackGround(graphics);  
        moveGround(graphics);   
        Bird(graphics);     
        //update(getGraphics());

Because it is a dynamic effect, it needs to delay the refresh

public void mainWindow(){
    
    
        setBounds(700,250,288,512);   
        setTitle("Test");
        setVisible(true);   
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);    //
        setResizable(false);    


/*线程刷新实现动效*/
        while(true){
    
        //让窗口自动刷新
            repaint();;
            try{
    
    
                Thread.sleep(180);   //线程休眠180ms,控制刷新频率
            }catch (InterruptedException e){
    
    
                e.printStackTrace();
            }
        }
    }

The reason for this problem is simply: " Using repaint will call the clearing operation in the update() method and cause flickering ."

This image dynamic principle can be understood as: first, you need to draw a picture in the window, when drawing the next picture, the previous picture will be cleared, the white background displayed after clearing, and then drawing, more The reason is that the refresh rate of the display is higher than the frequency of the code running repaint. After a short period of clearing, the white background that appears causes the appearance of flickering.

Solution

Here we use double buffering technology to eliminate screen flicker. The principle is: "In the background that needs to be redrawn in advance, cache the next picture on the picture in advance, and then directly overlay the picture drawn in advance on it" No After clearing the blank display on the screen, there is no flickering

Therefore, the update method needs to be refactored This
is the source update method

public void update(Graphics g)  
  {
    
      
		paint(g);
  }  

Refactor the update method

Image offScreenImage = null;    //新建一张背景图

public void update(Graphics g){
    
    
	if (offScreenImage == null)
    	offScreenImage = createImage(“画布width”,“画布height” );     //背景图的大小尺寸
   		Graphics gOff = offScreenImage.getGraphics();       
   			paint(gOff);    //调用paint()传入缓冲图象
   			g.drawImage(offScreenImage, 0, 0, null);    //将缓存图像传入到 g
    }

notes

As of the time of posting, I wrote Java code on MacOS in advance, but I tried a lot of refactoring update methods, and double buffering still occurs flickering
. Using double buffering on Windows does not flicker. I have not found a solution to flickering on MacOS. If there is any The big guy knows, please give pointers, thank you for your criticism and corrections!

Guess you like

Origin blog.csdn.net/weixin_50679163/article/details/119490947