设置背景时屏幕闪烁
问题描述:
我正在试图制作一个简单的GUI程序而不使用JComponents。 目前,我有一个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的两件事情,repaint
不会立即发生,根本不可能发生,具体取决于RepaintManager
决定做什么。其次,你不控制绘画过程。
Swing使用被动渲染算法,这意味着绘画将在需要时发生,多次在您不知情或不介入的情况下进行。你可以做的最好的是当你想要更新某些东西时向框架提出建议
有关更多详细信息,请参见Painting in AWT and Swing和Performing Custom Painting。
在做任何自定义绘画之前调用'super.paintComponent' – MadProgrammer
你的while循环有点疯狂 – MadProgrammer
我只是作为一个例子在这里做了(true)。在我的实际计划中,并非如此。 :p我会尽力。 – asdfaweglkelkr