www.pudn.com > spritebullet.rar > ShowBullet.java


package spritebullet; 
 
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.game.*; 
 
public class ShowBullet 
    extends GameCanvas  implements Runnable,CommandListener { 
  Bullet bullet; 
  Graphics g; 
  int x, y, width; 
 
  public ShowBullet(int x, int y, int width) { 
    super(true); 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    addCommand(new Command("Exit", Command.EXIT, 1)); 
    setCommandListener(this); 
    g = this.getGraphics(); 
    bullet = new Bullet(); 
    Thread t = new Thread(this); 
    t.start(); 
  } 
 
  public void commandAction(Command command, Displayable displayable) { 
    if (command.getCommandType() == Command.EXIT) { 
      MainMIDlet.quitApp(); 
    } 
  } 
 
  public void input() { 
    if (getKeyStates() == FIRE_PRESSED) { 
      bullet.addBullet(getWidth()/2,getHeight()/2,32); 
    } 
  } 
 
  public void clear() { 
    g.setColor(0, 0, 0); 
    g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
  } 
 
  public void run() { 
    while (true) { 
      clear(); 
      input(); 
      bullet.DrawSelf(g); 
      this.flushGraphics(); 
      bullet.Move(); 
      try { 
        Thread.sleep(100); 
      } 
      catch (Exception e) {} 
    } 
  } 
}