• PUDN用户
    了解作者
  • Java
    开发工具
  • 62KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 17
    下载次数
  • 2009-11-23 10:05
    上传日期
this is calculator j2me java
Calculator.rar
内容介绍
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; /** * * @author LeVietHung_fit */ class CalCanvas extends Canvas implements CommandListener{ private int gap; private int button_w;// width of button private int button_h;// height of button private int current=0;// current coordinate private int rect_width;// widh of operand box private int X;// coordinate of below rectangle private int Y; private String result=""; private String str=""; private String opcode1=""; private String opcode3=""; private String opcode=""; private boolean op1_ready=false; private boolean op3_ready=false; // private boolean press_opcode=false; private boolean press_enter=false; private Command cmExit; private point2D[] A=new point2D[9]; private String[] Operator=new String[9]; private Calculator midlet; public CalCanvas(Calculator midlet){ this.midlet=midlet; cmExit=new Command("Turn off", Command.EXIT,1); addCommand(cmExit); setCommandListener(this); } protected void paint(Graphics g){ int w=getWidth(); int h=getHeight(); gap=h/40; rect_width=h/12; //System.out.println(w); //System.out.println(h); g.setColor(0x000000); g.fillRect(0, 0, w, h); g.setColor(0xccffcc); g.fillRoundRect(1, 1, w-2,4*rect_width+3*gap,10, 10); g.setColor(0x99ff99); g.fillRect(2,gap, w-4,rect_width); g.fillRect(2,2*rect_width+gap,w-4,rect_width); g.fillRect(2,3*rect_width+2*gap,w-4,rect_width); g.setColor(0x000000); draw_keyboard(g,w,h); g.drawString(opcode1, w-1,gap ,Graphics.RIGHT|Graphics.TOP); g.drawString(opcode,1, rect_width+gap,Graphics.LEFT|Graphics.TOP ); if(result!=""){ g.drawString("=", 1, 3*rect_width+2*gap,Graphics.LEFT|Graphics.TOP); g.drawString(result,w-1,3*rect_width+2*gap,Graphics.RIGHT|Graphics.TOP); } if(op3_ready==false){ g.drawString(opcode3,w-1 ,2*rect_width+gap,Graphics.RIGHT|Graphics.TOP); } g.setColor(0x33ff00); g.fillRoundRect(A[current].x+1,A[current].y+1, button_w-1,button_h-1, 20,20); g.setColor(000000); g.drawString(Operator[current],A[current].x+button_w/2, A[current].y+button_h/2+1,Graphics.HCENTER|Graphics.BASELINE); } public void commandAction(Command c,Displayable s){ if(c==cmExit){ midlet.exitMidlet(); } } public boolean is_number(int key){ int[] a={KEY_NUM0,KEY_NUM1,KEY_NUM2,KEY_NUM3,KEY_NUM4,KEY_NUM5,KEY_NUM6,KEY_NUM7, KEY_NUM8,KEY_NUM9}; for(int i=0;i<10;i++){ if(key==a[i]){ System.out.println("hung"); return true; } } return false; } public void keyPressed(int keycode){ int key= getGameAction(keycode); String str_enter=getKeyName(keycode); // System.out.println(str_enter); if(is_number(keycode)&&press_enter==true){ press_enter=false; str=""; } switch(keycode){ case KEY_NUM0: str+="0"; repaint(); break; case KEY_NUM1: str+="1"; repaint(); break; case KEY_NUM2: str+="2"; repaint(); break; case KEY_NUM3: str+="3"; repaint(); break; case KEY_NUM4: str+="4"; repaint(); break; case KEY_NUM5: str+="5"; repaint(); break; case KEY_NUM6: str+="6"; repaint(); break; case KEY_NUM7: str+="7"; repaint(); break; case KEY_NUM8: str+="8"; repaint(); break; case KEY_NUM9: str+="9"; repaint(); break; } switch(key){ case UP: if(current>2)current-=3; else current=current+6; repaint(); return; // break; case DOWN: if(current<6)current+=3; else current-=6; repaint(); return; // break; case LEFT: if(current==0)current=8; else current-=1; repaint(); return; // break; case RIGHT: if(current==8)current=0; else current+=1; repaint(); return; //break; } if(str_enter.equals("SELECT")||str_enter.equals("Select")){ if(current==0||current==1||current==3||current==4 ){ opcode=Operator[current]; op1_ready=true; if(press_enter==false){ str=""; opcode3=""; repaint(); return; } if(press_enter==true){ opcode1=result; str=""; repaint(); return; } } if(current==5){ if(opcode1==""||opcode==""||opcode==""){ Alert alert=new Alert("fatal error", "Some field is empty. Try again", null, AlertType.ERROR); midlet.getDisplay().setCurrent(alert); return; } Calculate cal=new Calculate(opcode1,opcode3,opcode,midlet); op1_ready=false; press_enter=true; result=""; if(opcode.equals("+")){ result+=cal.add(); repaint(); return; } if(opcode.equals("-")){ result+=cal.sub(); repaint(); return; } if(opcode.equals("*")){ result+=cal.mul(); repaint(); return; } if(opcode.equals("/")){ result+=cal.div(); repaint(); return;} } if(current==6){ str+="."; repaint();} if(current==7){ if(str==""||str.indexOf("-")==-1)str="-"+str; else str=str.substring(1); if(press_enter==true){ press_enter=false; str="-"; } repaint(); } if(current==8){ str=""; opcode1=""; opcode=""; opcode3=""; result=""; press_enter=false; op1_ready=false; op3_ready=false; repaint(); return; } if(current==2){ if(press_enter==true){ Alert alert=new Alert("Error","Not permit",null, AlertType.ERROR); midlet.getDisplay().setCurrent(alert); } if(str.length()!=0){ str=str.substring(0,str.length()-1); repaint(); } } } if(op1_ready==false)opcode1=str; if(op1_ready==true&&op3_ready==false) opcode3=str; } public void draw_keyboard(Graphics g,int width,int height){ int w1=width-2; int h1=height-(4*rect_width+4*gap)-2; X=1; Y=4*rect_width+4*gap; button_w=(w1-4*gap)/3; button_h=(h1-4*gap)/3; g.setColor(0xccffcc); g.fillRoundRect(X+2,Y , w1-4, h1,10,10); A[0]=new point2D(X+gap,Y+gap); A[1]=new point2D(X+2*gap+button_w,Y+gap); A[2]=new point2D(X+3*gap+2*button_w,Y+gap); A[3]=new point2D(X+gap,Y+2*gap+button_h); A[4]=new point2D(X+2*gap+button_w,Y+2*gap+button_h); A[5]=new point2D(X+3*gap+2*button_w,Y+2*gap+button_h); A[6]=new point2D(X+gap,Y+3*gap+2*button_h); A[7]=new point2D(X+2*gap+button_w,Y+3*gap+2*button_h); A[8]=new point2D(X+3*gap+2*button_w,Y+3*gap+2*button_h); Operator[0]="+"; Operator[1]="-"; Operator[2]="Del"; Operator[3]="*"; Operator[4]="/"; Operator[5]="="; Operator[6]="."; Operator[7]="+/-"; Operator[8]="AC"; g.setColor(0x000000); /*1*/ g.drawRoundRect(X+gap, Y+gap, button_w,button_h,20,20); g.drawString("+", X+gap+button_w/2,Y+gap+button_h/2+2,Graphics.HCENTER|Graphics.BASELINE); /*2*/ g.drawRoundRect(X+button_w+2*gap,Y+gap, button_w, button_h, 20, 20); g
评论
    相关推荐
    • calculator.rar
      J2ME开发的一个实用软件——计算器。实现了windows系统自带计算器的基本计算功能,界面很舒服,并实现了键盘映射功能。这是我给自己的手机定做的,现在共享给大家。
    • Calculator.rar
      这是一个J2ME计算器程序,实现了在手机上的计算器功能
    • Calculator.rar
      The Calculator is built by J2ME
    • Calculator.rar
      This is about how to make calculator in j2me. so we can run the calculator in handphones
    • Calculator.zip
      It is a calculator designed in j2me MIDLET 1.0 it performsall operations of a calculator
    • java-full-tutorial
      Java基础 Java是基于类的,面向对象的编程语言,旨在具有尽可能少的实现依赖关系。 Java是一种高级编程语言,最初由Sun Microsystems开发并于1995年发布。 为什么要学习Java编程? 面向对象-在Java中,一切都是...
    • Java手机计算器源码.rar
      JAVA代码编写的运行在手机上的计算器程序,简单型的计算器,不是Android版的,纯J2me的,运行的话需要相关的J2me环境。
    • j2me-otp:J2ME OTP 计算器
      这里有一个 jotp 到 J2ME 的端口,以便它可以在支持 Java 的手机上使用。 我这样做是为了我可以从不安全的网吧安全地连接到我们的 ssh 服务器,人们可能会在那里尝试嗅探我的密码。 基本上,它只是 Harry Mantakos ...
    • MIDP Calculator-开源
      适用于手机和MIDP设备的科学Java计算器
    • ZxingDemo.rar
      android上使用ZXing识别条形码和二维码,代码简单易懂,相信能帮助读者迅速掌握相关的开发