gpt4 book ai didi

java - 为什么使用 Swing 绘画在 Java 8 和 Java 6 中表现不同?

转载 作者:搜寻专家 更新时间:2023-11-01 02:24:45 24 4
gpt4 key购买 nike

我正在开发一个使用 Swing 作为 GUI 的 Java 应用程序。在这个项目中,我使用的是 Java 8 Update 25。我一直在用 Java 编写另一个图形应用程序,但我使用的是 Java 6。在这两个项目中,我都编写了相同的 paint() 方法。 (如下所示)我也以同样的方式调用'repaint()'。在这两个项目中,我都在绘制一个字符串。这个字符串显示了一个本地 int,count 的值;每次调用 paint() 方法时,计数加一。

当这两个项目的行为不同时,我的问题就来了。在 Java 6 中,屏幕更新速度超快,应用程序的行为符合预期。但是,在 Java 7 和 8 中,应用程序不显示任何内容。如果我增加重绘之间的延迟(大约 300 毫秒),我就能看到字符串闪烁。但是,如果我想用 Java 8 开发游戏,角色的闪烁和抖动等 Action 将是非常不受欢迎的。

为什么不同的 Java 版本以这种方式表现不同?有没有一种方法可以使用类似的设置在 Java 8 中复制平滑的重绘(通过 Java 6)? (如下所列)如果有,怎么办?如果不是,如何实现平滑、最小闪烁的显示? (我更希望这个重绘不断重绘,但它不像显示的流程那么必要)

提前感谢您的帮助, ~雷恩

Java 6 项目代码:

public class App {

static AppDisplay display = new AppDisplay();

public static void main(String args[]) {

display.setup();

Thread graphics = new Thread() {
public void run() {
while(true) {
display.repaint();
try {
Thread.sleep(17); // This is the delay I am talking about above
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
graphics.start();


}
}

public class AppDisplay() extends JFrame {
private static final long serialVersionUID = 1L;

int count = 0;

public void setup() {
this.setSize(600, 600);
this.setTitle("Application");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);
g.drawString("Count: " + count);
count ++;
}
}

Java 8 代码:

public class App {

static AppDisplay display = new AppDisplay();

public static void main(String args[]) {

display.setup();

Thread graphics = new Thread() {
public void run() {
while(true) {
display.repaint();
try {
Thread.sleep(17); // Delay
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
graphics.start();


}
}

public class AppDisplay() extends JFrame {
private static final long serialVersionUID = 1L;

int count = 0;

public void setup() {
this.setSize(600, 600);
this.setTitle("Application");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);
g.drawString("Count: " + count);
count ++;
}
}

最佳答案

  • 切勿直接在 JFrame 中绘制。
  • 始终在 JComponent 中绘制,例如 JPanel 或 JComponent。
  • 并在其 paintComponent(Graphics g) 覆盖中绘制,而不是在 paint(Graphics g) 覆盖中绘制以获得自动双缓冲的好处。
  • 请不要发布草率的代码。如果您的类(class)扩展了另一个类(class),请展示它。细节很重要。

例如,

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MyApp extends JPanel {

private static final int PREF_W = 600;
private static final int PREF_H = 400;
private static final int DELAY = 17;
private int count = 0;

public MyApp() {
new Timer(DELAY, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
count++;
repaint();
}
}).start();
}

public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Count = " + count, 20, 20);
}

private static void createAndShowGUI() {
MyApp paintEg = new MyApp();

JFrame frame = new JFrame("MyApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于java - 为什么使用 Swing 绘画在 Java 8 和 Java 6 中表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683099/

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