Java.problem 是当我开始添加新元素时,每次重新绘制都会停止一段时间,而计时器正在工作,并且元素最终会移动,当它重新绘制元素时就像一直在移动一样跳跃。还有抛出奇怪的异常。一些代码:
public void paintComponent(Graphics g)
{
g = (Graphics2D) g;
g.drawImage(img1, 0, 0,null);
Iterator<zombie> it = zombies.iterator();
while(it.hasNext())
{
if(it.next().heals <= 0)
{
it.remove();
}
else{
g.drawImage(it.next().img ,it.next().x , it.next().y , null);
}
}
}
public void actionPerformed(ActionEvent e)
{
Iterator<zombie> it = zombies.iterator();
while(it.hasNext())
{
it.next().move();
}
repaint();
}
这是生成新项目的新线程:
public void run()
{
Random rd = new Random();
while(true)
{
try {
Thread.sleep(1000 + rd.nextInt(2000));
if(rd.nextInt(4) == 1)
{
zombies.add(new ZombieLow(1000,rd.nextInt(500),1,20,300));
}
else
{
zombies.add(new ZombieHard(1000,rd.nextInt(500),1,15,700));
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
那是什么问题?如果您需要更多信息,请说我会给它。