2012年6月12日 星期二

Java Game Development (11)-Completing the Animation


import java.awt.*;

import javax.swing.ImageIcon;




public class apples{
public static void main(String[] args){
DisplayMode displaymode = new DisplayMode(1280,800,16, DisplayMode.REFRESH_RATE_UNKNOWN);
apples a = new apples();
a.run(displaymode);
}
private Screen screen;
private Image bg;
private Animation ani;
//Computer加載圖片到Java和添加場景
public void loadPics(){
bg = new ImageIcon("C\\baby.jpg").getImage();
Image face = new ImageIcon("C\\baby1.png").getImage();
Image face1 = new ImageIcon("C\\baby2.png").getImage();
ani = new Animation();
ani.addScene(face, 250);
ani.addScene(face1, 250);
}
//要運
public void run(DisplayMode dm){
screen = new Screen();
try{
screen.setFullScreen(dm, new Frame());
loadPics();
movieLoop();
}finally{
screen.restoreScreen();
}
}

//主要動畫循環
public void movieLoop(){
long startingTime = System.currentTimeMillis(); 獲取當前時間
long cumTime = startingTime;
while(cumTime - startingTime < 5000){
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed; 動畫的運行時間
ani.update(timePassed);
Graphics g =screen.getFullScreenWindow().getGraphics();
draw(g);
g.dispose();
try{
Thread.sleep(20);
}catch(Exception ex){}
}
}

//draw method
public void draw(Graphics g){
g.drawImage(bg, 0, 0, null);
g.drawImage(ani.getImage(), 600,400, null);
}
}

沒有留言:

張貼留言