gpt4 book ai didi

java - JFrame重画图像闪烁

转载 作者:行者123 更新时间:2023-11-30 03:04:24 32 4
gpt4 key购买 nike

我在使用 super.paintComponents(g); 命令时遇到问题更新 JFrame 时。我遇到的问题是我正在加载的图像在代码运行时闪烁。我非常确定代码是双缓冲的,并且没有在网上找到任何有用的资源来解决这个问题。这是我的第一个问题,所以请容忍任何格式错误。

这是我的完整代码(下面是片段):

public class GAME extends JFrame implements KeyListener, MouseMotionListener {
int mouseX, mouseY;

public static ArrayList<Images> images = new ArrayList<Images>();
public static String lastKeyPressed = null;

public GAME() {
this.addMouseMotionListener(this);
this.addKeyListener(this);
}

public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}

public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_LEFT) {
lastKeyPressed = "LEFT";
} else if (e.getKeyCode() == e.VK_RIGHT) {
lastKeyPressed = "RIGHT";
} else if (e.getKeyCode() == e.VK_UP) {
lastKeyPressed = "UP";
} else if (e.getKeyCode() == e.VK_DOWN) {
lastKeyPressed = "DOWN";
}
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

@Override
public void paint(Graphics g) {
super.paintComponents(g);
for (int i = 0; i < images.size(); i++) {
g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
}
}

public static void main(String[] args) throws IOException, InterruptedException {
GAME frame = new GAME();
Dimension dim = new Dimension(800, 600);
frame.setPreferredSize(dim);
frame.setSize(dim);
frame.setResizable(false);
frame.setTitle("GAME");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Images test = new Images("unnamed.png");
images.add(test);
while (true) {
if (lastKeyPressed == "LEFT") {
test.xpos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "RIGHT") {
test.xpos += 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "UP") {
test.ypos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "DOWN") {
test.ypos += 5;
lastKeyPressed = null;
}
frame.repaint();
Thread.sleep(100);
}
}

}

这是图像类:

public class Images {

String name;
BufferedImage img;
int xpos;
int ypos;

public Images (String Name) throws IOException{
name = Name;
xpos = 40;
ypos = 90;
System.out.println(name);
System.out.println("PATH: " + GAME.class.getResource(name));
URL file = getClass().getClassLoader().getResource(name);
img = ImageIO.read(file);
}
}

您可能只想看到以下内容:

我的绘画方法:

@Override
public void paint(Graphics g) {
super.paintComponents(g);
for (int i = 0; i < images.size(); i++) {
g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
}
}

以及运行游戏的主类中的代码块:

Images test = new Images("unnamed.png");
images.add(test);
while (true) {
if (lastKeyPressed == "LEFT") {
test.xpos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "RIGHT") {
test.xpos += 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "UP") {
test.ypos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "DOWN") {
test.ypos += 5;
lastKeyPressed = null;
}
frame.repaint();
Thread.sleep(100);
}

(我添加了 Thread.sleep,因为它似乎降低了闪烁发生的速率)

这是正在发生的事情的 gif(我刚刚使用了第一个 Google 结果作为图像的“测试”): http://i.imgur.com/WjLCSvu.gif?1

我感谢任何帮助和一般建议来改进我的代码。谢谢。

最佳答案

不要覆盖顶级容器的paint,例如JFrame,它们没有双缓冲,而是使用JPanel并覆盖其paintComponent 方法。 JPanel 默认情况下是双缓冲的。

然后,将面板添加到您想要的任何容器

在进行任何自定义绘画之前,不要忘记调用它的 super 方法 (super.paintComponent)。

仔细看看Painting in AWT and SwingPerforming Custom Painting有关 Swing 中绘画工作原理的更多详细信息

这只是您不应该(从 JFrame 扩展并)覆盖顶级容器的 paint 的原因之一,请看一下

了解更多

关于java - JFrame重画图像闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35167531/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com