J2ME做的簡單計算器



代碼如下:import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;

public class Calculate extends MIDlet {
private Display display;//定義Display對象
private MyCanvas can;//定義一個MyCanvas實例

public Calculate() {
  
  MyCanvas can=new MyCanvas();//初始化
  
  
  
  
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
  display=Display.getDisplay(this);
  MyCanvas can=new MyCanvas();
  display.setCurrent(can);
}

  
  

public class MyCanvas extends Canvas{
  private Image img[]=new Image[25];//定義一個圖片數組
        int curX,curY,flag;//curX,curY分別是圖片的x,y座標,flag是一個標誌
        float num,t;
        String str="",str1="",str2="";
        boolean nam=false;//按鈕被點擊時繪製圖片的標誌
  MyCanvas(){
       super();
    
     try {
      img[0]=Image.createImage("/0.png");
      img[1]=Image.createImage("/1.png");
      img[2]=Image.createImage("/2.png");
      img[3]=Image.createImage("/3.png");
      img[4]=Image.createImage("/4.png");
      img[5]=Image.createImage("/5.png");
      img[6]=Image.createImage("/6.png");
      img[7]=Image.createImage("/7.png");
      img[8]=Image.createImage("/8.png");
      img[9]=Image.createImage("/9.png");
      img[10]=Image.createImage("/10.png");
      img[11]=Image.createImage("/11.png");
      img[12]=Image.createImage("/-.png");
      img[13]=Image.createImage("/+.png");
      img[14]=Image.createImage("/c.png");
      img[15]=Image.createImage("/12.png");
      img[16]=Image.createImage("/x.png");
      img[17]=Image.createImage("/20.png");
      img[18]=Image.createImage("/21.png");
      img[19]=Image.createImage("/24.png");      
      img[20]=Image.createImage("/23.png");
    
      curX=10;curY=50;
     } catch (IOException e) {
    
      e.printStackTrace();
     }
    
    
  
  }
  protected void paint(Graphics g) {
   g.setColor(100,100,100);
   g.fillRect(0, 0, getWidth(), getHeight());
   g.setColor(0x00FFFFFF);
   g.fillRoundRect(10,10,getWidth()-20,25,20,20);
   g.drawImage(img[1],10 ,50, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[2],50 ,50, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[3],90 ,50, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[17],170,50,Graphics.LEFT|Graphics.TOP);
   g.drawImage(img[4],10 ,90, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[5],50 ,90, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[6],90 ,90, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[7],10 ,130, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[8],50 ,130, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[9],90 ,130, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[0],10 ,170, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[15],50 ,170, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[11],90 ,170, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[13],130 ,50, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[12],130 ,90, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[16],130 ,130, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[10],130,170, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[18], 170, 90,  Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[20], 170, 130, Graphics.LEFT | Graphics.TOP);
   g.drawImage(img[19], 170, 170,  Graphics.LEFT | Graphics.TOP);
   if(nam){
    g.setColor(250, 0, 0);
    g.fillRect(curX,curY,33,33);
  
   }
   g.setColor(255, 0, 0);
   g.drawRect(curX,curY,33,33);
   g.setColor(0, 0, 0);
   Font font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
   g.setFont(font);
   //防止字符溢出
   if(str.length()<=27){
    g.drawString(str, getWidth()-10, 10, Graphics.TOP|Graphics.RIGHT);
   }
   else{
    int n;
       n=str.length()-27;
    str=str.substring(0, str.length()-n);
   }
  
   this.setFullScreenMode(true);//設置全屏
  }
  protected void keyPressed(int keyCode){
   switch(keyCode){
   case KEY_NUM0:
    nam=true;
    curX=10;curY=170;
    str+="0";
    repaint();
    break;
   case KEY_NUM1:
    curX=10;curY=50;
    nam=true;
    str+=1;
    repaint();
    break;
   case KEY_NUM2:
    curX=50;curY=50;
    nam=true;
    str+="2";
    repaint();
    break;
   case KEY_NUM3:
    curX=90;curY=50;
    nam=true;
    str+="3";
    repaint();
    break;
   case KEY_NUM4:
    curX=10;curY=90;
    nam=true;
    str+="4";
    repaint();
    break;
   case KEY_NUM5:
    curX=50;curY=90;
    nam=true;
    str+="5";
    repaint();
    break;
   case KEY_NUM6:
    curX=90;curY=90;
    nam=true;
    str+="6";
    repaint();
    break;
   case KEY_NUM7:
    curX=10;curY=130;
    nam=true;
    str+="7";
    repaint();
    break;
   case KEY_NUM8:
    curX=50;curY=130;
    nam=true;
    str+="8";
    repaint();
    break;
   case KEY_NUM9:
    curX=90;curY=130;
    nam=true;
    str+="9";
    repaint();
    break;
  
   }
   switch(getGameAction(keyCode)){
  
   case UP:
    if(curY<=50)
    {
    }
    else doUp();
    break;
   case LEFT:
    if(curX<=20){
    
    }
    else doLeft();
    break;
   case RIGHT:
    if(curX>=170){
  
    }
    else doRight();
    break;
   case DOWN:
    if(curY>=170){
    
    }
    else doDown();
    break;
   }
   if((curX==10&&curY==50)&&getGameAction(keyCode)==FIRE){
    nam=true;
    
    str+="1";
    repaint();
    
   }
   if((curX==50&&curY==50)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="2";
    repaint();
   }
   if((curX==90&&curY==50)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="3";
    repaint();
   }
   if((curX==10&&curY==90)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="4";
    repaint();
   }
   if((curX==50&&curY==90)&&getGameAction(keyCode)==FIRE){
    
    nam=true;
    str+="5";
    repaint();
   }
   if((curX==90&&curY==90)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="6";
    repaint();
   }
   if((curX==10&&curY==130)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="7";
    repaint();
   }
   if((curX==50&&curY==130)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="8";
    repaint();
   }
   if((curX==90&&curY==130)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="9";
    repaint();
   }
   if((curX==10&&curY==170)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str+="0";
    repaint();
    
    
   }
   if((curX==50&&curY==170)&&getGameAction(keyCode)==FIRE){
    if(str==""){
     nam=true;
    
     repaint();
    }
    else{nam=true;
     str+=".";
     repaint();
    }
    
   }
   if((curX==170&&curY==50)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str="";
    repaint();
   }
   if((curX==170&&curY==90)&&getGameAction(keyCode)==FIRE){
    nam=true;
    str=str.substring(0, str.length()-1);
    repaint();
   }
   if((curX==170&&curY==130)&&getGameAction(keyCode)==FIRE){
    nam=true;
    String str1,str2;
    str1=str;
    num=Float.parseFloat(str1)*Float.parseFloat(str1);
    str2=Float.toString(num);
    str=str2;
    repaint();
   }
   if((curX==170&&curY==170)&&getGameAction(keyCode)==FIRE){
    
    try {
     destroyApp(true);
    } catch (MIDletStateChangeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  
   if((curX==130&&curY==50)&&getGameAction(keyCode)==FIRE){
    str1=str;
    str="";
    nam=true;
    repaint();
    if(str==""){
     if((curX==10&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="1";
     }
     if((curX==50&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="2";
      
     }
     if((curX==90&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="3";
      
     }
     if((curX==10&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="4";
      
     }
     if((curX==50&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="5";
      
     }
     if((curX==90&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="6";
      
     }
     if((curX==10&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="7";
      
     }
     if((curX==50&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="8";
      
     }
     if((curX==90&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="9";
      
     }
     if((curX==10&&curY==170)&&getGameAction(keyCode)==FIRE){
      
       str+="0";
      
      
      
     }
     if((curX==50&&curY==170)&&getGameAction(keyCode)==FIRE){
      if(str==""){
      
      }
      else{
       str+=".";
      
      }
      
     }
    
    }
    flag=1;
    
    
   }
   if((curX==130&&curY==90)&&getGameAction(keyCode)==FIRE){
    str1=str;
    str="";
    nam=true;
    repaint();
    if(str==""){
     if((curX==10&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="1";
     }
     if((curX==50&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="2";
      
     }
     if((curX==90&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="3";
      
     }
     if((curX==10&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="4";
      
     }
     if((curX==50&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="5";
      
     }
     if((curX==90&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="6";
      
     }
     if((curX==10&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="7";
      
     }
     if((curX==50&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="8";
      
     }
     if((curX==90&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="9";
      
     }
     if((curX==10&&curY==170)&&getGameAction(keyCode)==FIRE){
      
       str+="0";
      
      
      
     }
     if((curX==50&&curY==170)&&getGameAction(keyCode)==FIRE){
      if(str==""){
      
      }
      else{
       str+=".";
      
      }
      
     }
    
    }
    flag=2;
    
    
   }
   if((curX==130&&curY==130)&&getGameAction(keyCode)==FIRE){
    str1=str;
    str="";
    nam=true;
    repaint();
    if(str==""){
     if((curX==10&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="1";
     }
     if((curX==50&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="2";
      
     }
     if((curX==90&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="3";
      
     }
     if((curX==10&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="4";
      
     }
     if((curX==50&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="5";
      
     }
     if((curX==90&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="6";
      
     }
     if((curX==10&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="7";
      
     }
     if((curX==50&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="8";
      
     }
     if((curX==90&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="9";
      
     }
     if((curX==10&&curY==170)&&getGameAction(keyCode)==FIRE){
      
       str+="0";
      
      
      
     }
     if((curX==50&&curY==170)&&getGameAction(keyCode)==FIRE){
      if(str==""){
      
      }
      else{
       str+=".";
      
      }
      
     }
    
    }
    flag=3;
    
    
   }
   if((curX==130&&curY==170)&&getGameAction(keyCode)==FIRE){
    str1=str;
    str="";
    nam=true;
    repaint();
    if(str==""){
     if((curX==10&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="1";
     }
     if((curX==50&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="2";
      
     }
     if((curX==90&&curY==50)&&getGameAction(keyCode)==FIRE){
      str+="3";
      
     }
     if((curX==10&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="4";
      
     }
     if((curX==50&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="5";
      
     }
     if((curX==90&&curY==90)&&getGameAction(keyCode)==FIRE){
      str+="6";
      
     }
     if((curX==10&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="7";
      
     }
     if((curX==50&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="8";
      
     }
     if((curX==90&&curY==130)&&getGameAction(keyCode)==FIRE){
      str+="9";
      
     }
     if((curX==10&&curY==170)&&getGameAction(keyCode)==FIRE){
      
       str+="0";
      
      
      
     }
     if((curX==50&&curY==170)&&getGameAction(keyCode)==FIRE){
      if(str==""){
      
      }
      else{
       str+=".";
      
      }
      
     }
    
    }
    flag=4;
    
    
   }
   if((curX==90&&curY==170)&&getGameAction(keyCode)==FIRE){
    if(flag==1){
    num=Float.parseFloat(str1)+Float.parseFloat(str);
    str2=Float.toString(num);
    }
    if(flag==2){
     num=Float.parseFloat(str1)-Float.parseFloat(str);
     str2=Float.toString(num);
    }
    if(flag==3){
     num=Float.parseFloat(str1)*Float.parseFloat(str);
     str2=Float.toString(num);
    }
    if(flag==4){
     num=Float.parseFloat(str1)/Float.parseFloat(str);
     str2=Float.toString(num);
    }
    flag=0;
    nam=true;
    str=str2;
    repaint();
   }
  
  
  }
  private void doDown() {
   curY+=40;
   this.repaint();
   this.serviceRepaints();
  
  
  }
  private void doRight() {
   curX+=40;
   this.repaint();
   this.serviceRepaints();
  }
  private void doLeft() {
   curX-=40;
   this.repaint();
   this.serviceRepaints();
  
  }
  private void doUp() {
   curY-=40;
   this.repaint();
   this.serviceRepaints();
  
  }
  protected void keyRepeated(int keyCode){
   keyPressed(keyCode);
  }
  protected void keyReleased(int keyCode){
   nam=false;
   repaint();
  
  }
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章