上下跳动的小秋

作者在 2008-10-02 13:18:19 发布以下内容
//最近学习j2ee,做了一些小的实验,放到这里,希望大家给提出宝贵意见,我将努力完善.
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.perseus.parser.ColorParser;

public class ballfei extends MIDlet implements CommandListener{
 private Display display;
 private myCanvas mc;//定义画布;
 private xiancheng p;//定义线程;
 private int x=0;    //定义横坐标;
 private int y=0;    //定义从坐标
 private int flag=0; //控制小球的移动方向;
 private int flag1=0;//控制线程的开始结束;
 private Command stop;//暂停按钮;
 private Command goon;//继续按钮;
 public ballfei() {
  stop=new Command("暂停",Command.SCREEN,1 );
  goon=new Command("继续",Command.SCREEN,1);
  display=Display.getDisplay(this);
   
    
   
    
  // TODO Auto-generated constructor stub
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
 }
 protected void pauseApp() {
  // TODO Auto-generated method stub
 }
 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  mc=new myCanvas();
  mc.addCommand(stop);
  display.setCurrent(mc);
  
  mc.setCommandListener(this);
  p=new xiancheng();
  p.start();
 }
 class xiancheng extends Thread
 {
  //Thread a;
  public void run()
  {
   
   while(true)
    {
    if(flag1==1)break;
    if(x>=mc.getHeight()-30)
             {
      flag=1;
             }
    else if(x<=0) flag=0;
    if(flag==1)
     x=x-3;
    else if(flag==0)
     x=x+3;
    mc.repaint();
   try {
    sleep(10);
    }
   catch (InterruptedException e) {
    
    e.printStackTrace();
       }
   }
   
   
  }
 }
 class myCanvas extends Canvas
 {
  
  public void paint(Graphics g)
  {  
   g.setColor(255,255,255);
   g.fillRect(0, 0, this.getWidth(), this.getHeight());
      g.setColor(255,0,0);
      g.fillArc(100,x,30,30,0,360);
        }
 }
 public void commandAction(Command c, Displayable b) {
  if(c==stop)
      {
       flag1=1;
       mc.removeCommand(stop);
       mc.addCommand(goon);
         }
   if(c==goon)
           {
    flag1=0;
    mc.removeCommand(goon);
       mc.addCommand(stop);
       p=new xiancheng();
       p.start();
      
            } 
   
  // TODO Auto-generated method stub
  
  
 }
}
 
 
j2ee | 阅读 1810 次
文章评论,共0条
游客请输入验证码
最新评论