.setVisible(true)立即重绘 [英] .setVisible(true) immediate repaint

查看:418
本文介绍了.setVisible(true)立即重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简短的方法中,我使用setVisible(false)隐藏了一个JFrame。
然后我拍摄截图并使用setVisible(true)恢复JFrame。

In a short method, I hide a JFrame using setVisible(false). Then I take a screenshot and restore the JFrame with setVisible(true).

再次显示后,窗口应显示与以前不同的图片(比如说截图的一部分)。

After made visible again, the window is supposed to show a different picture than before (lets say a part of that screenshot taken).

问题是在调用setVisible(true)之后,窗口会闪烁旧内容一瞬间在调用paintComponent并绘制更新状态之前。

The problem is that after setVisible(true) is called, the window is flashed with the old content for a split second before paintComponent is called and the updated state is drawn.

我可能会以丑陋的方式解决这个问题,但我想知道是否有更好的解决方案。

I could probably workaround this in an ugly way, but I wanted to know if there are better solutions.

提前感谢您的任何帮助

编辑:在准备一个例子的时候,我注意到当这个效果几乎没有被观察到时不像我在程序中那样使用透明度。应该提到这一点。以下是我提出的建议:

edit: While preparing an example, I noticed that the effect was hardly ever observable when not using transparency like I do in my program. Should probably have mentioned that. Here is what I came up with:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.awt.AWTUtilities;
public class Test {

    static boolean flag = false;
    static Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        AWTUtilities.setWindowOpaque(frame, false);  //draw on a transparent window
        frame.setSize(scrSize.width, scrSize.height);
        frame.setContentPane(new JPanel() {
            protected void paintComponent(Graphics g) 
            {
                if (Test.flag) {
                    g.setColor(Color.RED);
                    g.drawRect(50, 50, scrSize.width - 100, scrSize.height - 100);
                }
                else {
                    g.setColor(Color.GREEN);
                    g.fillOval(50, 50, scrSize.width - 100, scrSize.height - 100);
                }
            }
        });
        frame.setVisible(true); //green oval shown
        Thread.sleep(1000);
        frame.setVisible(false);
        flag = true; // next draw produces red rectangle
        Thread.sleep(1000);
        frame.setVisible(true); // before the next draw, 
                                // you can see a flash of the green oval
    }

}


推荐答案

我意识到这个答案是在提出问题一年后提出的,但我遇到了类似的问题,当然在我试图解决它之前研究了一下。对于遇到此问题的任何人,请在打包之前尝试丢弃窗口/框架并使其可见。

I realise that this answer is being suggested a year after the question was asked, but I had a similar problem and of course researched a bit before I tried to solve it. For anyone who comes across this question, try disposing your window / frame before you pack and make it visible.

这篇关于.setVisible(true)立即重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆