Java .drawImage:如何“取消绘制”或删除图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17867537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java .drawImage : How do I "unDraw" or delete a image?
提问by Saucymeatman
I need a certain image to be redrawn at different locations constantly as the program runs. So I set up a while loop that should move an image across the screen, but it just redraws the image on top of itself over and over again. What am I doing wrong? Is there a way to delete the old image before drawing it in a new location?
我需要在程序运行时不断在不同位置重绘某个图像。所以我设置了一个 while 循环,它应该在屏幕上移动图像,但它只是一遍又一遍地重绘图像。我究竟做错了什么?有没有办法在将旧图像绘制到新位置之前将其删除?
JFrame frame = buildFrame();
final BufferedImage image = ImageIO.read(new File("BeachRoad_double_size.png"));
JPanel pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int num = 0;
boolean fluff = true;
while (fluff == true) {
num = num + 1;
g.drawImage(image, num, 0, null);
if (num == 105) {
fluff = false;
}
}
}
};
frame.add(pane);
回答by camickr
You can't code a loop in the paintComponent() method. The code will execute so fast that the image will only be painted in the final position, which in your case should be with an x position of 105.
您不能在paintComponent() 方法中编写循环代码。代码将执行得如此之快,以至于图像只会在最终位置绘制,在您的情况下,该位置的 x 位置应为 105。
Instead you need to use a Swing Timer to schedule the animation every 100 milliseconds or so. Then when the timer fires you update the x position and invoke repaint() on the panel. Read the Swing tutorial on Using Swing Timersfor more information.
相反,您需要使用 Swing Timer 每隔 100 毫秒左右安排一次动画。然后,当计时器触发时,您更新 x 位置并调用面板上的 repaint()。阅读有关使用 Swing 计时器的 Swing 教程以获取更多信息。
回答by tbodt
Putting a while loop inside a paintComponent
method is not the way to do it. Instead, there should be some setup like the following:
在paintComponent
方法中放置一个 while 循环不是这样做的方法。相反,应该有如下设置:
...
final int num = 0;
final JPanel pane;
Timer timer = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
num++;
pane.repaint();
}
});
pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, num, 0, null);
}
});
timer.start();
This will move the image ever 10 milliseconds, as specified in the Timer constructor.
这将按照 Timer 构造函数中的规定每 10 毫秒移动一次图像。
回答by Undefined
This is a common issue people starting out in animation have, as I did. You can't 'remove an image' from the screen. However, you can repaint the entire screen, then redraw your image at a new location.
这是刚开始接触动画的人们普遍存在的问题,就像我一样。您无法从屏幕上“删除图像”。但是,您可以重新绘制整个屏幕,然后在新位置重新绘制图像。
In psuedocode:
在伪代码中:
while (condition)
background(white); //or whatever color your background is
drawImage(x,y);
The code above clears the screen so it's safe for you to redraw your image. This effectively 'deletes' your image.
上面的代码清除了屏幕,因此您可以安全地重新绘制图像。这有效地“删除”了您的图像。
Edit: I didn't read your code, I just addressed your question. So other answers that fix your code are probably better than mine.
编辑:我没有阅读您的代码,我只是解决了您的问题。因此,修复您的代码的其他答案可能比我的要好。