为什么这不起作用?我创建了一个动画循环的方法,两个图片称为animation(), 我想把它放在paint方法中。我该怎么做?
import java.applet.*;
import java.awt.*;
public class Slot_Machine extends Applet
{
Image back;
int coin=10;
// Place instance variables here
public void init ()
{
back= getImage(getDocumentBase(),"Images/Background/Slot Machine.png");
// Place the body of the initialization method here
} // init method
public void animation(Graphics g)
{
Image Title1,Title2;
Title1= getImage(getDocumentBase(),"Images/Title Animation/Title 1.png");
Title2= getImage(getDocumentBase(),"Images/Title Animation/Title 2.png");
while(true){
g.drawImage(Title2,200,0,this);
{ try { Thread.currentThread().sleep(2000); }
catch ( Exception e ) { } }
g.drawImage(Title1,200,0,this);
{ try { Thread.currentThread().sleep(2000); }
catch ( Exception e ) { } }
}//end while(true) loop
}//end animation
public void paint (Graphics g)
{
g.drawImage(back,0,0,this);
animation(); //FROM THE METHOD ABOVE, WHY DOESNT THIS WORK? HOW DO I FIX THIS?
String coins = String.valueOf(coin);
g.setColor(Color.white);
g.setFont(new Font("Impact",Font.PLAIN,30));
g.drawString(coins,405,350);
// Place the body of the drawing method here
} // paint method
}
答案 0 :(得分:1)
你的animation()
方法中有一个无限循环,这意味着paint()
永远不会返回,这导致线程尝试启动applet挂起,等待paint()
到返回。
如果要在后台运行永久循环动画,请尝试在另一个线程中执行此操作。看看ExecutorService
。