【发布时间】:2016-02-24 19:12:26
【问题描述】:
这是我第一次尝试在java中使用另一个线程,有人可以告诉我如何让它工作吗?我已经阅读了有关它的其他主题,但我没有找到解决方案。
我想在另一个线程中绘制一个 gif(在随机位置和动画期间绘制)。
问题是第二个线程中的 drawImage() 没有做任何事情。我的计数器运行良好(它打印 1.. 2.. 3 ...),但没有绘制图像(或者我看不到它)。
条件一开始为假,然后在某一时刻为真(只创建一个新线程而没有更多),然后又为假。
if (condition) {
(new ThreadGif(this,g)).start();
}
但是,当我删除 paintComponent() 中的条件时,它会绘制一些东西,这意味着 drawImage() 可以工作。因此,当它创建许多新线程时,gif 的每个图像都在随机位置绘制,并且它会一次又一次地启动 gif(并且计数器仍然可以正常工作)。
这可能很好,但我不认为创建数千个新线程是答案:我只需要一个。而且,每个 gif 只需要一个随机位置,而不是 gif 的每张图像都有一个不同的位置。
我希望我已经足够清楚了。请帮助我了解如何使其工作:) 非常感谢。
这是我的两个课程的简化版本:
ThreadGif.java:
public class ThreadGif extends Thread {
Screen screen;
Graphics g;
boolean running = true;
public ThreadGif(Screen screen, Graphics g) {
this.g = g;
this.screen = screen;
}
public void run() {
int aleaX = new Random().nextInt(300)/100;
int aleaY = new Random().nextInt(300)/100;
int compt = 1;
while (running) {
g.drawImage(new ImageIcon("res/feu.gif").getImage(), screen.tailleCase*aleaX, screen.tailleCase*aleaY, screen.tailleCase*2, screen.tailleCase*2, screen);
System.out.println("thread " + compt);
compt++;
try {
Thread.sleep(sleepTime);
} catch(InterruptedException e) {
e.printStackTrace ();
}
}
}
}
屏幕.java:
public class Screen extends JPanel implements Runnable {
Thread thread = new Thread(this);
public Screen(Frame frame) {
this.frame = frame;
thread.start();
}
public void paintComponent(Graphics g) {
g.clearRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
if (condition) {
(new ThreadGif(this,g)).start();
}
}
public void run() {
while (running) {
repaint();
try {
Thread.sleep(sleepTime);
} catch(InterruptedException e) {
e.printStackTrace ();
}
}
System.exit(0);
}
}
【问题讨论】:
-
使用关闭 Swing 事件线程的 paintComponent Graphics 参数进行绘画闻起来像是线程灾难的邀请,不是吗?不应该反过来吗? -- 创建一个
SwingWorker<Void, Image>,它有自己的内部定时器,在 SwingWorker 中创建图像,通过发布/处理方法对将图像导出到 GUI,然后调用 repaint? -
还有这个:
g.drawImage(new ImageIcon("res/feu.gif").getImage(), ....);坏了。既然只读取一次并将其存储到变量中更简单、更经济,为什么还要一遍又一遍地重新读取同一个图像? -
我会搜索 SwingWork 谢谢。是的,我绝对同意,我正在改变它。
标签: java multithreading swing paint animated-gif