我正在使用Swing作为GUI开发
Java应用程序.在这个项目中,我使用的是Java 8 Update 25.我一直在用Java编写另一个图形应用程序,但我使用的是Java 6.在这两个项目中,我都编写了相同的paint()方法. (如下所示)我也以同样的方式称呼’repaint()’.在这两个项目中,我正在绘制一个字符串.此字符串显示本地int,count的值;每次调用paint()方法时,计数增加1.
当两个项目表现不同时,我的问题出现了.在Java 6中,屏幕更新速度非常快,应用程序的行为也很理想.但是,在Java 7和8中,应用程序不显示任何内容.如果我增加重绘之间的延迟,(大约300毫秒)我能看到字符串闪烁.但是,如果我想用Java 8开发游戏,例如,角色的闪烁和抖动是非常不可取的.
为什么不同的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 ++;
}
}