【问题标题】:Screen flickers when setting background设置背景时屏幕闪烁
【发布时间】:2015-08-30 10:23:00
【问题描述】:

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

【问题讨论】:

  • 在进行任何自定义绘画之前请致电super.paintComponent
  • 你的 while 循环有点疯狂
  • 我在 (true) 时做到了,这里仅作为示例。在我的实际程序中,情况并非如此。 :p 不过我会试试的。
  • 您可能还存在线程竞争情况,因为面板正在“尝试”绘制图像,您正在更新它...
  • 刚刚尝试调用 super.paintComponent,但并没有改变任何东西。不过我猜我会把它留在那里。

标签: java swing double buffer flicker


【解决方案1】:

我最好的猜测是你有一个竞争条件,你的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 了解更多详情。

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 2023-03-15
    • 2011-01-06
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多