自由移动

来源:互联网 发布:巅峰乔丹身体数据 编辑:程序博客网 时间:2024/04/27 22:14

import javax.microedition.lcdui.*;

 public class Guomu extends Canvas implements Runnable
 {
   

 
    private final int width= 128;
    private final int height= 128;
    private Image ditu,renwu;
    public Guomu(){
     try{
       ditu=Image.createImage("/ditu.png");
       renwu=Image.createImage("/renwu.png");
       }catch(java.io.IOException e){}
     
    init();}
   
    private final void init(){
     x=0;
     y=0;
     position_x=0;
      position_y=0;     }
   
    //////////////////////////////////
    private static int x,y;
   
    private final int Map_w=256;
    private final int Map_h=256;
    private final int d=5;
   
       
    //////////////////////////////
   
    private final void move_to(int x1,int y1,int x2,int y2){
           int i=x1-x2;
           int j=y1-y2;
          
          int max =Math.abs(i)>=Math.abs(j)?Math.abs(i):Math.abs(j);
          int pingshu=max/d+1;
         
         move( i/pingshu,j/pingshu,pingshu);
                                                           }
                                                          
    ///////////////////////////////                                                       
   
    private static long oldtime=0;
    private final int jiange=50;
    private static int m;
   
   
    private final void move(int a,int b,int c){
          this.m=c;
          if(m<=0) return;
          long newtime =System.currentTimeMillis();
          if(newtime>=(oldtime+jiange)){
          oldtime=System.currentTimeMillis();
          m--;
          do_move(a,b);               
          repaint();
          move(a,b,m);}
        
         
                      }
         
   //////////////////////////////
  
   private static int position_x;
   private static int position_y;
  
   private final void do_move(int a,int b){
    
   if((x+position_x)>=0&&(x+position_x)<=102&&position_x<=0&&position_x>=(128-Map_w)&&a<=0)
           {
              if((x+position_x)<52&&position_x==0)           
              x-=a;           
              else if
              (position_x==(128-Map_w))
              x-=a;
              else
              {position_x+=a;
               x-=a;}
           } 
 if((x+position_x)>=0&&(x+position_x)<=102&&position_x<=0&&position_x>=(128-Map_w)&&a>0)
           {
              if((x+position_x)>52&&position_x==(128-Map_w))           
              x-=a;           
              else if
              (position_x==0)
             
              x-=a;
              else
              {position_x+=a;          
              x-=a;}
           }           
           if((y+position_y)>=0&&(y+position_y)<=102&&position_y<=0&&position_y>=(128-Map_h)&&b<=0)
           {
              if((y+position_y)<52&&position_y==0)             
              y-=b;
              else if
             (position_y==(128-Map_h))
              y-=b;
              else
              {position_y+=b;
               y-=b;}
           }  
         if((y+position_y)>=0&&(y+position_y)<=102&&position_y<=0&&position_y>=(128-Map_h)&&b>0)
           {
              if((y+position_y)>52&&position_y==(128-Map_h))             
              y-=b;
              else if
             (position_y==0)
             
              y-=b;
              else
              {position_y+=b;
              y-=b;}
           }  
             
         
                                           }
  ///////////////////////////////////
  public void paint(Graphics g){
    g.setColor(0xffffffff);
    g.fillRect(0,0,width,height);
    g.setClip(0,0,width,height);
    g.drawImage(ditu,position_x,position_y,0);
    g.drawImage(renwu,x+position_x,y+position_y,0);
    g.setColor(0x000000);
   // int[] polygonX={30,50,30,10};
   // int[] polygonY={10,30,50,30};
   // DirectGraphics dg=DirectUtils.getDirectGraphics(g);
   // dg.drawPolygon(polygonX,0,polygonY,0,polygonX.length,0);
  /* g.drawString(String.valueOf(x),0,0,0);
   g.drawString(String.valueOf(y),20,0,0);
   g.drawString(String.valueOf(position_x),40,0,0);
   g.drawString(String.valueOf(position_y),60,0,0);
   g.drawString(String.valueOf(x),1,1,0);
   g.drawString(String.valueOf(y),21,1,0);
   g.drawString(String.valueOf(position_x),41,1,0);
   g.drawString(String.valueOf(position_y),61,1,0);
  */ 
   }
   
  protected void keyPressed(int k){
    if(k==53)
       new Thread(this).start();
    if(k==50)
       t=1;
    if(k==52)
       t=2;
    if(k==54)
       t=3;
           }
  private static int t=0;         
  private final boolean kaishi=true;    
  public void run(){
   while(kaishi){
       try{
          switch(t){
        
          case 1: move_to(x,y,230,230);
          case 2: move_to(x,y,230,0);
          case 3: move_to(x,y,0,230);}
          Thread.sleep(100);
        } catch(Exception e){}
      }  }                                   
      }
         
           

原创粉丝点击