0
爲什麼不能正常工作?我創建了一個動畫循環的方法,這個動畫循環叫做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
}
1)爲什麼要編寫一個小程序?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)爲什麼選擇AWT而不是Swing?看到我對[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 –
不要阻塞EDT(Event Dispatch Thread) - 當發生這種情況時,GUI將「凍結」。而不是調用'Thread.sleep(n)'實現一個Swing'Timer'來重複執行任務,或者一個'SwingWorker'執行長時間運行的任務。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –