Java swing JFrame用repaint出現閃爍的問題解決

閃光桐人發表於2021-01-02

這幾天用swing寫登入頁面背景動圖的時候發現一直會有閃爍(我的類是繼承JFrame),就來搜原因後發現好像是因為repaint會呼叫update()方法中的清屏操作導致閃爍。

我當時看的是這個文章

穆梓先生-java 雙緩衝技術解決螢幕閃爍問題

於是按照他的方法重寫了update方法,卻發現問題沒解決

public void paint(Graphics g) {
		g.drawImage(skyImag.getImage(), skyX, skyY, null);
		g.drawImage(groundImag, groundX, groundY, null);
		g.drawImage(dinosaurImag.getImage(), dinoX, dinoY, null);
	}
public void update(Graphics g) {
		System.out.println("==1==");//這個是我拿來測試會不會呼叫的輸出資訊
		if (groundImag == null) {
			System.out.println("==2==");
			//這句話從沒輸出過,說明了JFrame不會執行清屏操作,即groundImag != null,而Frame跟JPanel好像會執行清屏操作
			groundImag = this.createImage(500, 500); // 新建一個影像快取空間,這裡影像大小為800*600,為了使這句話沒問題,我把我的物件從ImagIcon物件改成Imag物件
			}
			Graphics gImage = groundImag.getGraphics(); // 把它的畫筆拿過來,給gImage儲存著
			paint(gImage); // 將要畫的東西畫到影像快取空間去
			g.drawImage(groundImag, 0, 0, null); // 然後一次性顯示出來
		
	}

於是我又繼續查文章,我發現沒什麼人用JFrame出現閃爍現象(我上次寫飛機大戰都沒閃爍的說!),所以我查文章的時候放大範圍,只要是Java swing程式設計出現閃爍的文章我都看一遍過去,終於讓我看到這個大佬的文章

ydcun-雙緩衝原理在awt和swing中實現消除閃爍的方法

就是他做的測試,讓我知道原來在JFrame中repaint()的時候update()方法就沒被呼叫到,JFrame消除閃爍是在update()中“直接呼叫了paint()函式而沒有clearRect(),也就是清屏的方法,這裡他試圖不通過清屏來阻止閃爍的發生。”

所以到底是哪一步出問題了。。paint()方法已經被我重寫了是不會有清屏操作的,問題感覺只能出在repaint()上,看了repaint()的程式碼好像也沒發現類似清屏的程式碼,我能力有限,還在學習中,有大佬知道咋回事就求賜教一下QWQ,為了方便大家找repaint()有沒有問題我就把程式碼貼上來吧

public void repaint(long tm, int x, int y, int width, int height) {
        if (this.peer instanceof LightweightPeer) {
            // Needs to be translated to parent coordinates since
            // a parent native container provides the actual repaint
            // services.  Additionally, the request is restricted to
            // the bounds of the component.
            if (parent != null) {
                if (x < 0) {
                    width += x;
                    x = 0;
                }
                if (y < 0) {
                    height += y;
                    y = 0;
                }

                int pwidth = (width > this.width) ? this.width : width;
                int pheight = (height > this.height) ? this.height : height;

                if (pwidth <= 0 || pheight <= 0) {
                    return;
                }

                int px = this.x + x;
                int py = this.y + y;
                parent.repaint(tm, px, py, pwidth, pheight);
            }
        } else {
            if (isVisible() && (this.peer != null) &&
                (width > 0) && (height > 0)) {
                PaintEvent e = new PaintEvent(this, PaintEvent.UPDATE,
                                              new Rectangle(x, y, width, height));
                SunToolkit.postEvent(SunToolkit.targetToAppContext(this), e);
            }
        }
    }

接下來我繼續說說我是怎麼解決這個問題的,在ydcun大佬那邊是有講他的解決方案的,我還沒試過(因為凌晨3點了都!我寫完立馬睡覺!),我當時(凌晨2點)就想著既然不會去呼叫update(),那我手動呼叫不就好了?

於是

public void paint(Graphics g) {
		g.drawImage(skyImag.getImage(), skyX, skyY, null);
		g.drawImage(groundImag, groundX, groundY, null);
		g.drawImage(dinosaurImag.getImage(), dinoX, dinoY, null);
		update(getGraphics());//跟JPanel不同,JFrame的repaint方法不會自動呼叫update方法。所以我這邊直接讓它呼叫了。
		//而且引數不能用g得用getGraphics()

	}

	public void update(Graphics g) {
//		System.out.println("==1==");
//		if (groundImag == null) {
//			//System.out.println("==2==");

//			groundImag = this.createImage(500, 500); }// 新建一個影像快取空間,這裡影像大小為800*600
			//Image物件在這邊也會出問題
			//報getGraphics() not valid for images created with createImage(producer)
			//得改成BufferedImage,但是我懶得改,所以我直接全註釋了
			
//			Graphics gImage = groundImag.getGraphics(); // 把它的畫筆拿過來,給gImage儲存著
//			paint(gImage); // 將要畫的東西畫到影像快取空間去
//			g.drawImage(groundImag, 0, 0, null); // 然後一次性顯示出來

	}
	//為了實現動畫效果我寫了個計時器來repaint()
	public void startTimeTask() {
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				skyMove();
				groundMove();
				dinoMove();
				repaint();
			}
		};
		timer.schedule(task, 10, 20);
	}

對,我呼叫了一個什麼東西都沒有的update()方法,就解決了閃爍的問題。。。但是如果我呼叫系統的update()

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

就會更閃爍然後報錯
Java HotSpot(TM) 64-Bit Server VM warning: Potentially dangerous stack overflow in ReservedStackAccess annotated method sun.java2d.d3d.D3DBlitLoops.IsoBlit(Lsun/java2d/SurfaceData;Lsun/java2d/SurfaceData;Ljava/awt/image/BufferedImage;Ljava/awt/image/BufferedImageOp;Ljava/awt/Composite;Lsun/java2d/pipe/Region;Ljava/awt/geom/AffineTransform;IIIIIDDDDZ)V [1] Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:714) at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:937) at java.base/java.util.concurrent.locks.ReentrantLock$Sync.lock(ReentrantLock.java:153) at java.base/java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:322) at java.desktop/sun.awt.SunToolkit.awtLock(SunToolkit.java:195) at java.desktop/sun.java2d.pipe.RenderQueue.lock(RenderQueue.java:112) at java.desktop/sun.java2d.d3d.D3DBlitLoops.IsoBlit(D3DBlitLoops.java:313) at java.desktop/sun.java2d.d3d.D3DTextureToSurfaceScale.Scale(D3DBlitLoops.java:768) at java.desktop/sun.java2d.pipe.DrawImage.scaleSurfaceData(DrawImage.java:1001) at java.desktop/sun.java2d.pipe.DrawImage.renderImageScale(DrawImage.java:647) at java.desktop/sun.java2d.pipe.DrawImage.tryCopyOrScale(DrawImage.java:319) at java.desktop/sun.java2d.pipe.DrawImage.transformImage(DrawImage.java:258) at java.desktop/sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:76) at java.desktop/sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1027) at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3425) at java.desktop/sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:813) at java.desktop/sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1034) at java.desktop/sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186) at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3425) at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3401) at cn.zhetech.BackImag.paint(UserLogin2.java:276) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283) at java.desktop/javax.swing.JFrame.update(JFrame.java:469) at cn.zhetech.BackImag.paint(UserLogin2.java:283)

視窗也關不掉,只能從控制檯強制停止執行。。。

等日後有大佬評論教我了或者我自己學習到了再補充一下這個原因,碎覺,溜了||ヽ( ̄▽ ̄)ノミ|Ю,狗命要緊

如果文章有幫到你或者給你提供了思路,那就送我個讚唄(◦˙▽˙◦),不然我就預設每個瀏覽的都是想點然後忘了(自欺欺人,bushi)
懂得都懂

相關文章