gpt4 book ai didi

java - 当我添加计时器时,图像将不再加载

转载 作者:行者123 更新时间:2023-12-02 03:17:01 28 4
gpt4 key购买 nike

我正在尝试创建一个小行星游戏,但我一开始就很挣扎:我想在游戏开始之前创建一个倒计时。为此,我使用一个计时器,该计时器在按下“开始”按钮时启动,并且(应该)每秒加载一个 BufferedImage,然后将其显示在 JFrame 上.

在我添加计时器之前,整个事情运行得很完美。然而,图像仍在绘制中,并且 TimerTask 的方法 run() 仍在完全使用。但是,该图像没有出现在我的 JFrame 上。

public class Main implements ActionListener {

private static File load = null;
private static BufferedImage image = null;
private JFrame frame;
private JButton start;
private JLabel game_name;
private JLabel screen;
private ImageIcon icon;
private Asteroids aster = new Asteroids();
private static Main m = new Main();

protected static int height = 550;
protected static int width = 800;
protected static int cd = 0;

/*
* Here the JFrame frame gets created and set up, and the main method
* does its job. I believe only the actionPerformed method to be
* important, so I removed this part from the post.
*
*/


private void addScreen() {
frame.add(screen);
}

@Override
public void actionPerformed(ActionEvent ae) {
// Cleaning the screen
start.setVisible(false);
game_name.setVisible(false);

// Creating the image
aster.spawn(5);

// creating the countdown timer
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// creating countdown
aster.initialScreen();

// Reading the image
load = new File(aster.filePath + aster.fileName + ".png");
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
try {
image = ImageIO.read(load);
} catch (IOException i) {
System.out.println("Reading has encountered an error: \n" + i); }

// Implementing the Image
screen = new JLabel();
screen.setSize(width, height);
icon = new ImageIcon(image);
screen.setIcon(icon);
m.addScreen();
System.out.println("roger");
}

}, 0, 1000);

} // end actionPerfomed()

} // end class

有人能发现错误吗?我真的不明白问题是什么,虽然我对计时器很陌生,所以如果我做的事情真的很愚蠢,请告诉我。

最佳答案

好吧,我以某种方式解决了这个问题:首先,我确实将 util.Timer 更改为 swing.Timer,但是,这本身并没有帮助。以前,我在桌面上绘制每个图像后将其保存,然后在应该显示时将其加载到屏幕上。我稍微研究了一下代码,发现我不需要保存它,但实际上可以通过将其设置为静态来直接引用它。当我这样做时,问题就消失了。这是我的意思的代码示例:

之前:

// In the drawing class
save = new File(aster.filePath + aster.fileName + ".png");
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
try {
ImageIO.write(image, "png", save);
} catch (IOException i) {
System.out.println("Writing has encountered an error: \n" + i); }

// In the displaying class
load = new File(aster.filePath + aster.fileName + ".png");
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
try {
image = ImageIO.read(load);
} catch (IOException i) {
System.out.println("Reading has encountered an error: \n" + i); }
ImageIcon icon = new ImageIcon(image);
displayingJLabel.setIcon(icon);
frame.add(displayingJLabel);

之后:

// One static declaration and initialization of an BufferedImage in the drawing class
public static BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Drawing Code

// Accessing the image
ImageIcon icon = new ImageIcon(DrawingClass.image);
displayingJLabel.setIcon(icon);
frame.add(displayingJLabel);

本质上,一切都是一样的,只是跳过了整个保存和读取过程,并且我只使用一个 static BufferedImage,而不是多个本地 BufferedImage

我不确定为什么这会起作用,但我的猜测是整个读写过程花费的时间太长,以至于计时器每秒都会停止,而这种新方法要快得多。我还没有尝试再次使用 util.Timer 完成整个事情,但我认为 swing.Timer 在解决这个问题方面可能有其应有的份额。

关于java - 当我添加计时器时,图像将不再加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56956771/

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