📄 airplane.java
字号:
// 程序:火焰中的飞机
// 范例文件:Airplane.java
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
abstract class SuperSprite
{
int X,Y,width,height;
boolean visible,active;
abstract public void paintSprite(Graphics g);
abstract public void updateState();
public int getX()
{
return X;
}
public int getY()
{
return Y;
}
public void setLocation(int X,int Y)
{
this.X = X;
this.Y = Y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public void setSize(int width,int height)
{
this.width = width;
this.height = height;
}
public boolean canVisible()
{
return visible;
}
public void setVisible(boolean v)
{
visible = v;
}
public boolean canMove()
{
return active;
}
public void setMove(boolean m)
{
active = m;
}
}
class ImageSprite extends SuperSprite
{
Image SpriteImage;
Applet Game;
public ImageSprite(Image SpriteImage,Applet Game)
{
this.SpriteImage = SpriteImage;
this.Game = Game;
setLocation(0,0);
setVisible(true);
setMove(true);
}
public void updateState()
{
}
public void paintSprite(Graphics g)
{
if(visible == true)
g.drawImage(SpriteImage,X,Y,Game);
}
}
class BulletSprite extends ImageSprite
{
int AppletWidth,AppletHeight;
public BulletSprite(Image bullet,Applet Game,int AppletWidth,
int AppletHeight)
{
super(bullet,Game);
this.AppletWidth = AppletWidth;
this.AppletHeight = AppletHeight;
setVisible(false);
setMove(false);
}
public void updateState()
{
if(active == true)
{
if(X > AppletWidth)
{
setVisible(false);
setMove(false);
}
else
X = X + 10;
}
}
public void paintSprite(Graphics g)
{
if(visible == true)
g.drawImage(SpriteImage,X,Y,Game);
}
}
public class Airplane extends Applet
implements Runnable,MouseListener,MouseMotionListener
{
int AppletWidth,AppletHeight,ImageWidth,ImageHeight,
currentImage,mouseX,mouseY,X,Y;
Image background[],airplane,bullet,OffScreen;
Random R;
Thread newThread;
Graphics drawOffScreen;
MediaTracker MT;
ImageSprite airplaneSprite,BulletSprite;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
background = new Image[5];
MT = new MediaTracker(this);
R = new Random();
currentImage = 0;
AppletWidth = getSize().width;
AppletHeight = getSize().height;
for(int i=0;i<5;i++)
{
background[i] = getImage(getDocumentBase(),"Images/thunder" +
i + ".gif");
MT.addImage(background[i],0);
}
airplane = getImage(getDocumentBase(),"Images/airplane.gif");
bullet = getImage(getDocumentBase(),"Images/bullet.gif");
MT.addImage(airplane,0);
MT.addImage(bullet,0);
try
{
MT.waitForAll();
}
catch(InterruptedException E){ }
ImageWidth = airplane.getWidth(this);
ImageHeight = airplane.getHeight(this);
airplaneSprite = new ImageSprite(airplane,this);
BulletSprite = new BulletSprite(bullet,this,AppletWidth,
AppletHeight);
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start()
{
newThread = new Thread(this);
newThread.start();
}
public void stop()
{
newThread = null;
}
public void paint(Graphics g)
{
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
drawOffScreen.drawImage(background[currentImage],0,0,this);
airplaneSprite.paintSprite(drawOffScreen);
BulletSprite.paintSprite(drawOffScreen);
g.drawImage(OffScreen,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while(newThread != null)
{
repaint();
try
{
Thread.sleep(33);
}
catch(InterruptedException E){ }
if(R.nextInt(10) < 1)
currentImage = R.nextInt(5);
BulletSprite.updateState();
}
}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
if(mouseX < ImageWidth / 2)
X = ImageWidth / 2;
else if(mouseX > AppletWidth - (ImageWidth / 2))
X = AppletWidth - (ImageWidth / 2);
else
X = mouseX;
if(mouseY < ImageHeight / 2)
Y = ImageHeight / 2;
else if(mouseY > AppletHeight - (ImageHeight / 2))
Y = AppletHeight - (ImageHeight / 2);
else
Y = mouseY;
airplaneSprite.setLocation(X - (ImageWidth / 2),Y - (ImageHeight / 2));
}
public void mousePressed(MouseEvent e)
{
if(BulletSprite.canVisible() == false &&
BulletSprite.canMove() == false)
{
BulletSprite.setLocation(X + (ImageWidth / 2),Y);
BulletSprite.setVisible(true);
BulletSprite.setMove(true);
}
}
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
if(mouseX < ImageWidth / 2)
X = ImageWidth / 2;
else if(mouseX > AppletWidth - (ImageWidth / 2))
X = AppletWidth - (ImageWidth / 2);
else
X = mouseX;
if(mouseY < ImageHeight / 2)
Y = ImageHeight / 2;
else if(mouseY > AppletHeight - (ImageHeight / 2))
Y = AppletHeight - (ImageHeight / 2);
else
Y = mouseY;
airplaneSprite.setLocation(X - (ImageWidth / 2),Y - (ImageHeight / 2));
}
public void mouseDragged(MouseEvent e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -