import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class T1 extends Applet implements Runnable,MouseListener,MouseMotionListener,ActionListener
{
Thread tt;
Image OffScreen,TuPian1,TuPian2,tu2,BJ1;
Graphics drawOffScreen;
int AppletWidth,AppletHeight,TuWidth,TuHeight,Tu2Width,Tu2Height;
PigSprite PS1,PS2;
SSTest1 BeiJing1;
Button b1 = new Button("重新开始");
MediaTracker MT;//这个很重要没有它,图像的长宽就不正确
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
resize(640,480);
AppletWidth = getSize().width;
AppletHeight = getSize().height;
MT = new MediaTracker(this);
TuPian1 = getImage(getDocumentBase(),"a1.GIF");
TuPian2 = getImage(getDocumentBase(),"a2.GIF");
BJ1 = getImage(getDocumentBase(),"bj1.GIF");
MT.addImage(TuPian1,0);
MT.addImage(TuPian2,0);
try{
MT.waitForAll();
}catch(Exception e){}
PS1 = new PigSprite(TuPian1,this,5);
PS2 = new PigSprite(TuPian2,this,2);
BeiJing1 = new SSTest1(BJ1,this);
TuWidth = TuPian1.getWidth(this);
System.out.println(TuWidth);
TuHeight = TuPian1.getHeight(this);
System.out.println(TuHeight);
Tu2Width = TuPian2.getWidth(this);
System.out.println(Tu2Width);
Tu2Height = TuPian2.getHeight(this);
System.out.println(Tu2Height);
PS1.setLocation(10,10);
PS2.setLocation(100,100);
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start(){
tt = new Thread(this);
tt.start();
}
public void stop(){tt=null;}
public void run()
{
while(tt != null)
{
repaint();
try
{
Thread.sleep(33);
//Thread.sleep(1000);
}catch(Exception e){}
PS1.updateState();
PS2.updateState();
//int
if(PS1.hit(PS2.getX(),PS2.getY(),TuWidth,TuHeight,Tu2Width,Tu2Height))
{
//PS2.setVisible(false);
}
if(PS2.hit(PS1.getX(),PS1.getY(),TuWidth,TuHeight,Tu2Width,Tu2Height))
{
//PS1.setVisible(false);
}
}
}
public void paint(Graphics g)
{
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
BeiJing1.paintSprite(drawOffScreen);
PS1.paintSprite(drawOffScreen);
PS2.paintSprite(drawOffScreen);
g.drawImage(OffScreen,0,0,this);
}
public void update(Graphics g){paint(g);}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void actionPerformed(ActionEvent e){}
}
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 PigSprite extends SuperSprite
{
int vx,vy;
Image img1;
Applet Game;
Random R;
Boolean showPig;
public PigSprite(Image img1,Applet Game,int speed)
{
this.img1 = img1;
this.Game = Game;
this.vx = speed;
this.vy = speed;
showPig = false;
setVisible(true);
setMove(true);
}
public void updateState()
{
if(active)
{
X += vx;
Y += vy;
}
if((X>640 || Y>480) || (X<0 || Y < 0))
{
showPig = false;
vx=-vx;vy=-vy;
}else{
showPig = true;
}
}
public void paintSprite(Graphics g)
{
if(visible)
{
if(showPig)
g.drawImage(img1,X,Y,Game);
}
}
public boolean hit(int X,int Y,int P_Width,int P_Height,int H_Width,int H_Height)
{
if(
(this.X + P_Width >= X) &&
//(this.X + 40 >= X) &&
(this.Y + P_Height >= Y) &&
//(this.Y + 40 >= Y) &&
(X + H_Width >= this.X) &&
//(X + 40 >= this.X) &&
(Y + H_Height >= this.Y) &&
//(Y + 40 >= this.Y) &&
showPig)
{
showPig = false;
//setVisible(false);
//setMove(false);
return true;
}else
return false;
}
}