设置背景时屏幕闪烁

Screen flickers when setting background

我试图在不使用 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 来满足我的双缓冲需求?

我最好的猜测是你有一个竞争条件,你的 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 Swing and Performing Custom Painting