我需要在我的for循环结束之前等待再次启动,但我已经搜索过,并且没有一个方法适用于我的程序。程序将图像从一侧移动到另一侧。 for循环看起来像这样:
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
for (int finalX = xSize - 128; finalX == 0; finalX-=50)
{
for (int i = 0; i < 11; i += 1)
{
int Min = 64;
int Max = ySize - 200;
int finalY = Min + (int)(Math.random() * ((Max - Min) + 1));
g.drawImage(image3, finalX, finalY, imageWidth, imageHeight, this);
<b>//wait here</b>
}
}
是的,我已导入所有内容。我尝试了try/catch
,异常等,但没有一个可以工作。我如何做到这一点,以便我的程序仍然可以工作,它会等待?
答案 0 :(得分:2)
之前已经回答过:Java Delay/Wait
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
答案 1 :(得分:2)
由于这一行:
g.drawImage(image3, finalX, finalY, imageWidth, imageHeight, this);
这看起来是一个Swing或AWT GUI,因为您正在使用Graphics对象进行绘制(关于您应该为我们提供的问题的一个重要细节),如果是这样,这里已经在这里和其他地方进行了很好的讨论。 :
Thread.sleep(...)
paintComponent(Graphics g)
覆盖中进行绘制。super.paintComponent(Graphics g)
。您的实施细节将全部取决于您的问题的详细信息,您可能希望传递给我们的其他信息。有关如何使用Swing Timers的一般性评论,请查看Swing Timer Tutorial。
修改强>
例如......
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class TimerEg extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final String ICON_PATH = "https://duke.kenai.com/iconSized/duke4.gif";
private static final int TIMER_DELAY = 400;
private BufferedImage imageSprite = null;
private Random random = new Random();
private int finalX;
private int finalY;
public TimerEg() throws IOException {
URL imgUrl = new URL(ICON_PATH);
imageSprite = ImageIO.read(imgUrl);
int maxX = PREF_W - imageSprite.getWidth();
int maxY = PREF_H - imageSprite.getHeight();
new Timer(TIMER_DELAY, new TimerListener(this, maxX, maxY)).start();
}
public void setSpriteLocation(int x, int y) {
finalX = x;
finalY = y;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (imageSprite != null) {
g.drawImage(imageSprite, finalX, finalY, this);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class TimerListener implements ActionListener {
private TimerEg timerEg;
private int maxX;
private int maxY;
private TimerListener(TimerEg timerEg, int maxX, int maxY) {
this.timerEg = timerEg;
this.maxX = maxX;
this.maxY = maxY;
}
@Override
public void actionPerformed(ActionEvent e) {
int currentX = random.nextInt(maxX);
int currentY = random.nextInt(maxY);
timerEg.setSpriteLocation(currentX, currentY);
}
}
private static void createAndShowGui() {
TimerEg mainPanel = null;
try {
mainPanel = new TimerEg();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
JFrame frame = new JFrame("TimerEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
它显示了一个在GUI上随机移动的精灵,如下所示: