设置背景时屏幕闪烁

时间:2015-06-15 01:54:45

标签: java swing double buffer flicker

我正在尝试在不使用JComponents的情况下制作一个简单的GUI程序。 目前,我有一个BufferedImage,我在屏幕外绘制,因此它不会闪烁(或者我认为)。

我在这里制作了一个新程序来复制这个问题:

package Main;

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

import java.awt.image.BufferedImage;
import javax.swing.*;

public class Main {

private final static JFrame frame = new JFrame();
private final static Panel panel = new Panel();

public static void main(String[] args) {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setPreferredSize(new Dimension(1000, 750));
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);

    while (true) {
        panel.setBackgroundColour(Color.WHITE);
        panel.setBackgroundColour(Color.BLACK);
        panel.repaint();
    }
}

private static class Panel extends JPanel {

    private final BufferedImage offScreen = new BufferedImage(1000, 750, BufferedImage.TYPE_INT_ARGB);
    private final Graphics graphics = offScreen.getGraphics();

    @Override
    protected void paintComponent(Graphics graphics) {
        graphics.drawImage(offScreen, 0, 0, null);
    }

    public void setBackgroundColour(Color colour) {
        graphics.setColor(colour);
        graphics.fillRect(0, 0, 1000, 750);
    }
}

}

在上面的示例中,我将屏幕变为黑色,然后变为白色(屏幕外)。 我期望的是paintComponent()只显示白色屏幕。 相反,也显示了黑屏,但一切都闪烁。

我只是错误地使用Graphics2D,还是应该使用BufferStrategy来满足我的双缓冲需求?

1 个答案:

答案 0 :(得分:2)

我最好的猜测是你有一个竞争条件,你的while-loop正在尝试更新BufferedImage,但Swing也在试图绘制它,这意味着它们之间的更新很脏。此外,您可能会破坏事件调度线程,这可能有它自己的长期问题。

经过一番游戏,我能够得到这样的东西......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    private final static JFrame frame = new JFrame();
    private final static Panel panel = new Panel();

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                panel.setPreferredSize(new Dimension(1000, 750));
                frame.setContentPane(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });

        while (true) {
            panel.setBackgroundColour(Color.WHITE);
            panel.setBackgroundColour(Color.BLACK);
            panel.repaint();
            try {
                Thread.sleep(40);
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    private static class Panel extends JPanel {

        private BufferedImage offScreen = new BufferedImage(1000, 700, BufferedImage.TYPE_INT_ARGB);

        @Override
        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            graphics.drawImage(offScreen, 0, 0, this);
        }

        public void setBackgroundColour(Color colour) {
            Graphics graphics = offScreen.getGraphics();
            graphics.setColor(colour);
            graphics.fillRect(0, 0, 1000, 700);
            graphics.dispose();
        }

    }

    public static BufferedImage createCompatibleImage(int width, int height, int transparency) {

        BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
        image.coerceData(true);
        return image;

    }

    public static GraphicsConfiguration getGraphicsConfiguration() {

        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

    }
}

它只是在更新之间注入一个小延迟(25fps),允许Swing时间呈现结果。

你必须记住Swing的两件事,repaint不会立即发生,根本不会发生,取决于RepaintManager决定做什么。其次,你无法控制绘画过程。

Swing使用被动渲染算法,这意味着绘画将在需要时进行,很多时候在您不知情或没有干预的情况下进行。您可以做的最好的事情是在需要更新内容时为框架提出建议

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting