?? birdfly.java
字號:
package birdfly;import java.awt.*;import java.awt.event.*;import java.applet.*;/** * <p>Title: 飛行的大鳥</p> * <p>Description: 在沙漠中飛行的大鳥</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京師范大學(xué)計算機系</p> * @author 孫一林 * @version 1.0 */public class BirdFly extends MultiThreadApplet { boolean isStandalone = false; Image offScreenImg; //存放備用屏幕的內(nèi)容 Graphics offScreenG; //備用屏幕的繪圖環(huán)境 MediaTracker tracker; //媒體跟蹤器 Image walkerImgs[]=new Image[4]; //存放大鳥走路姿勢圖像 Image currentImg; //當前放映的大鳥動作圖像 int xpos,ypos=0; //大鳥動作圖像顯示的位置 int walk_step=20; //大鳥圖像每次移動的距離 int delay = 250; //每幀時延(毫秒) Image bgImage; //存放草原背景圖像 int applet_width,applet_height; int birdImg_width; //大鳥圖像寬度 //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public BirdFly() { } //Initialize the applet public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { tracker = new MediaTracker(this); for (int i=0;i<walkerImgs.length;i++){ //獲取大鳥動作圖像 walkerImgs[i]=getImage(getCodeBase(),"images/bird"+i+".gif"); tracker.addImage(walkerImgs[i], 0); //列入0組跟蹤范圍 } bgImage = getImage(getCodeBase(),"images/"+"bg.gif"); //獲取草原背景圖像 tracker.addImage(bgImage, 0); //列入0組跟蹤范圍 applet_width = getSize().width; applet_height = getSize().height; try { offScreenImg = createImage (applet_width,applet_height);//創(chuàng)建備用屏幕 offScreenG = offScreenImg.getGraphics ();//獲取備用屏幕的繪圖環(huán)境 } catch (Exception e) { offScreenG = null; //若出錯,就置備用屏幕的繪圖上下文環(huán)境為null } } public void run(){ try{ tracker.waitForID(0); //等待0組中所有圖像的到達 } catch(InterruptedException e){ return; } birdImg_width=walkerImgs[0].getWidth(this); int i=0; while(true){ for(xpos=-birdImg_width;xpos<=applet_width;xpos+=walk_step){//計算位置 currentImg=walkerImgs[i]; repaint(); i=(i+1)%walkerImgs.length; //計算下一幀是哪幅圖像 try{ Thread.sleep(delay); } catch(InterruptedException e){ } } } } public void paint(Graphics g){ g.drawImage(bgImage,0,0,this); g.drawImage(currentImg,xpos,ypos,this); } public void update(Graphics g){ if (offScreenG!=null) { //如果備用屏幕創(chuàng)建成功 paint(offScreenG); g.drawImage(offScreenImg,0,0,this); //將備用屏幕內(nèi)容畫到當前屏幕來 } else{ paint(g); } } //Get Applet information public String getAppletInfo() { return "飛行的大鳥"; } //Get parameter info public String[][] getParameterInfo() { return null; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -