程序演示地址 http://jajinco.vicp.net/javagame/index.html import java.applet.*; import java.util.*; import java.awt.*; import java.awt.event.*; abstract class SuperSprite { int X,Y,width=40,height=40; 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;} public boolean mouseHit(int x,int y){ if(x>this.X && x<this.X+width && y>this.Y && y<this.Y+height){ return true; }else{ return false; } } } interface MoRenShuZhi{ int AppletWidth = 640; int AppletHeight = 480; } class DrawImage extends SuperSprite{ public void paintSprite(Graphics g) { g.drawRect(X,Y,X+40,Y+40); } public void updateState() { } } class DrawImageGuDing extends SuperSprite{ DrawImageGuDing(int x,int y){ setLocation(x,y); } public void paintSprite(Graphics g) { g.setColor(Color.black); g.drawRect(X,Y,40,40); } public void updateState(){ } } class DrawImageYiDong extends SuperSprite{ boolean pickedSprite; DrawImageYiDong(){} public void paintSprite(Graphics g){ if(canVisible()) g.fillRect(X,Y,40,40); } public void updateState(){ } public boolean getPickedSprite(){return pickedSprite;} public void setPickedSprite(boolean pickedSprite){ this.pickedSprite = pickedSprite; } } class ZiFang extends SuperSprite{ int SHENGMINGZHI = 1000; public void paintSprite(Graphics g){ if(canVisible()) g.fillRect(X,Y,40,40); } public void updateState(){ } public void setLocation(int X,int Y) { this.X = X-(X%40); this.Y = Y-(Y%40); } } class ZiDan extends SuperSprite{ int SX = 0; public void paintSprite(Graphics g) { if(canVisible()){ g.setColor(Color.green); g.fillRect(X,Y,10,10); if(SX==0)SX=getX(); } } public void updateState(){ this.X+=10; //try{Thread.sleep(33);}catch(Exception e){} if(this.X>640){ this.X=SX;//setVisible(false); } } } class DiFang extends SuperSprite{ int SHENGMINGZHI = 1000; public void paintSprite(Graphics g) {; } public void updateState(){ } } class BeiJing extends SuperSprite { boolean bianHuan = true; public void paintSprite(Graphics g){ if(bianHuan){ g.setColor(Color.red); g.fillRect(40,0,560,40); bianHuan = false; }else{ g.setColor(Color.green); g.fillRect(40,0,560,40); bianHuan = true; } g.setColor(Color.black);g.drawString("JAJ",5,10);g.drawString("abc",5,25); g.drawString(""+X,615,10);g.drawString(""+Y,615,25); } public void updateState(){ } } class TaFangCeShiProperty { static TaFangCeShi TaFangCeShiq; public static void setProperty(TaFangCeShi taFangCeShi) { TaFangCeShiq = taFangCeShi; TaFangCeShiq.resize(TaFangCeShiq.AppletWidth,TaFangCeShiq.AppletHeight); TaFangCeShiq.OffScreen = TaFangCeShiq.createImage(TaFangCeShiq.AppletWidth,TaFangCeShiq.AppletHeight); TaFangCeShiq.drawOffScreen = TaFangCeShiq.OffScreen.getGraphics(); TaFangCeShiq.beiJing = new BeiJing(); TaFangCeShiq.drawImageGuDing = new DrawImageGuDing(40,0); TaFangCeShiq.drawImageGuDing2 = new DrawImageGuDing(80,0); TaFangCeShiq.drawImageYiDong = new DrawImageYiDong(); TaFangCeShiq.ziFang = new ZiFang[100]; TaFangCeShiq.ziDan = new ZiDan[500]; for(int i = 0;i<TaFangCeShiq.ziFang.length;i++){ TaFangCeShiq.ziFang[i] = new ZiFang(); } for(int i = 0;i<TaFangCeShiq.ziDan.length;i++){ TaFangCeShiq.ziDan[i] = new ZiDan(); } } } public class TaFangCeShi extends Applet implements Runnable,MoRenShuZhi,MouseListener,MouseMotionListener { Thread tt; Image OffScreen; Graphics drawOffScreen; int mouseX; int mouseY; DrawImage drawImage; DrawImageGuDing drawImageGuDing,drawImageGuDing2; DrawImageYiDong drawImageYiDong; ZiFang ziFang[]; ZiDan ziDan[]; BeiJing beiJing; public void init(){ addMouseListener(this); addMouseMotionListener(this); TaFangCeShiProperty.setProperty(this); } public void start(){tt = new Thread(this);tt.start();} public void stop(){tt=null;} public void update(Graphics g){paint(g);} public void paint(Graphics g){ drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight); beiJing.paintSprite(drawOffScreen); drawImageGuDing.paintSprite(drawOffScreen); drawImageGuDing2.paintSprite(drawOffScreen); drawImageYiDong.paintSprite(drawOffScreen); for(int i = 0;i<ziFang.length;i++){ ziFang[i].paintSprite(drawOffScreen); } for(int i = 0;i<ziDan.length;i++){ ziDan[i].paintSprite(drawOffScreen); } g.drawImage(OffScreen,0,0,this); } public void run(){ while(tt !=null){ repaint(); try{Thread.sleep(33);}catch(Exception e){} if(drawImageGuDing.mouseHit(mouseX,mouseY)){ this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR )); drawImageYiDong.setPickedSprite(true); }else{ this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); drawImageYiDong.setPickedSprite(false); } for(int i = 0;i<ziDan.length;i++){ if(ziDan[i].canVisible()==true){ ziDan[i].updateState(); continue; } } } } public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){ if(drawImageYiDong.getPickedSprite()){ drawImageYiDong.setVisible(true); }else{ if(drawImageYiDong.canVisible()){ drawImageYiDong.setVisible(false); for(int i = 0;i<ziFang.length;i++){ if(ziFang[i].canVisible()==false){ ziFang[i].setVisible(true); ziFang[i].setLocation(mouseX-20, mouseY-20); for(int i2 = 0;i2<ziDan.length;i2++){ if(ziDan[i2].canVisible()==false){ ziDan[i2].setVisible(true); ziDan[i2].setLocation(ziFang[i].getX()+40,ziFang[i].getY()+20); break; } } break; } } } } } public void ziDanMove(ZiDan[] ziDan){ } public void mouseEntered(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseMoved(MouseEvent e){ beiJing.setLocation(e.getX(),e.getY()); drawImageYiDong.setLocation(e.getX()-20,e.getY()-20); mouseX=e.getX(); mouseY=e.getY(); } public void mouseDragged(MouseEvent e){} }