Java 设置背景时屏幕闪烁
我试图在不使用JComponents的情况下制作一个简单的GUI程序。 目前,我有一个BuffereImage,我将其绘制到屏幕外,这样它就不会闪烁(或者我认为是这样) 我在这里制作了一个新程序来复制这个问题:Java 设置背景时屏幕闪烁,java,swing,double,buffer,flicker,Java,Swing,Double,Buffer,Flicker,我试图在不使用JComponents的情况下制作一个简单的GUI程序。 目前,我有一个BuffereImage,我将其绘制到屏幕外,这样它就不会闪烁(或者我认为是这样) 我在这里制作了一个新程序来复制这个问题: package Main; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.sw
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循环试图更新BuffereImage
,但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使用被动渲染算法,这意味着绘画将在需要时进行,很多次都不需要您的知识或干预。当您想要更新某些内容时,您所能做的最好的事情就是对框架提出建议
有关更多详细信息,请参见和。在进行任何自定义绘制之前,请调用super.paintComponent
。您的while循环有点疯狂。我仅将while(true)作为示例。在我的实际计划中,情况并非如此p我会试试。你可能也有线程争用的情况,因为面板正在“尝试”绘制图像,你正在更新它…只是尝试调用super.paintComponent,但它没有改变任何东西。我想我还是把它留在那里吧。