📄 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: 北京师范大学计算机系</p>
* @author 孙一林
* @version 1.0
*/
public class BirdFly extends MultiThreadApplet {
boolean isStandalone = false;
Image offScreenImg; //存放备用屏幕的内容
Graphics offScreenG; //备用屏幕的绘图环境
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);//创建备用屏幕
offScreenG = offScreenImg.getGraphics ();//获取备用屏幕的绘图环境
}
catch (Exception e) {
offScreenG = null; //若出错,就置备用屏幕的绘图上下文环境为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) { //如果备用屏幕创建成功
paint(offScreenG);
g.drawImage(offScreenImg,0,0,this); //将备用屏幕内容画到当前屏幕来
}
else{
paint(g);
}
}
//Get Applet information
public String getAppletInfo() {
return "飞行的大鸟";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -