J2ME Application Example-a simple calculator implementation (with source code)

Source: Internet
Author: User

J2ME Application Example-a simple calculator implementation (with source code)

<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Using the low-level user interface to make a simple calculator based on the Nokia S60 series, we hope to help. This article only contains the program code, if you need picture files and source code please send me an e-mail: cqucyf@263.net

The source code is as follows

Calcmidlet.java

Package My.calc;

Import javax.microedition.midlet.*;

Import javax.microedition.lcdui.*;

public class Calcmidlet extends MIDlet {

private static Calcmidlet instance;

Private Calccanvas calc = new Calccanvas ();

/** Constructor */

Public Calcmidlet () {

instance = this;

}

/** Main Method */

public void startApp () {

Display.getdisplay (This). Setcurrent (Calc);

}

/** Handle Pausing the MIDlet * *

public void Pauseapp () {

}

/** Handle destroying the MIDlet * *

public void Destroyapp (Boolean unconditional) {

}

/** Quit the MIDlet * *

public static void Quitapp () {

Instance.destroyapp (TRUE);

Instance.notifydestroyed ();

instance = null;

}

}

Calccanvas.java

Package My.calc;

Import Com.nokia.mid.ui.FullCanvas;

Import javax.microedition.lcdui.*;

Import java.io.*;

/**

* Calculator interface

* <p>title: Nokia S60-based calculators </p>

* <p>description: </p>

* <p>copyright:copyright (c) 2005</p>

* <p>company: </p>

* Email:cqucyf@263.net

* @version 1.0

*/

public class Calccanvas extends fullcanvas{

/** Exit button */

Private Command Cmdexit;

/** Title */

Private String title = "Calculator";

/** Font */

Private Font F;

/** the number shown */

Private long result = 0;

/** Focus Number */

private int num = 0;

/** Focus Coordinates */

private int focusx = 5;

private int focusy = 52;

/** button Width interval */

private int intervalwidth = 2;

private int intervalheight = 2;

/** Picture Start coordinates */

private int x = 10;

private int y = 70;

/** Button Size */

private int buttonwidth = 37;

private int buttonheight = 28;

/** picture File */

Private image image = null;

/** the contents of the storage control represented by */

Private char[] Info = {' 7 ', ' 8 ', ' 9 ', '/',

' 4 ', ' 5 ', ' 6 ', ' * ',

' 1 ', ' 2 ', ' 3 ', '-',

' 0 ', '? ', ' = ', ' + '

};

The first number in the/** operation */

Private long firstnum = 0;

The second number in the/** operation */

Private long secondnum = 0;

/** whether the first number is entered, true indicates that it has been entered, false means not entered */

Private Boolean flag = false;

/** Store the subscript for the pressed operation button */

private int func = 0;

/** Default Construction Method */

Public Calccanvas () {

try{

Image = Image.createimage ("/calc.png");

SYSTEM.OUT.PRINTLN (x);

}catch (IOException e) {

Ignore

System.out.println (e);

}

}

/**

* Draw

* @param g graphic Object

*/

public void Paint (Graphics g) {

/** Clear Current Display */

G.setcolor (255,255,255);

G.fillrect (0,0,getwidth (), getheight ());

Restore System Colors

G.setcolor (0,0,0);

f = G.getfont ();

Draw Title

g.DrawString (Title,getwidth ()/2,f.getheight ()/2,graphics.top| Graphics.hcenter);

Draw a text box

G.drawrect (3,24,172,20);

Draw a number

String temp = long.tostring (result);

g.DrawString (Temp,getwidth ()-3,30,graphics.top| Graphics.right);

Draw Panel

G.drawimage (image,2,50,graphics.top| Graphics.left);

Draw Focus

Drawfocus (G,num);

}

/**

* Event Handling

* @param keycode button value

*/

public void keypressed (int keycode) {

System.out.println (KeyCode);

Move to the left and move to the next line if you move to the right edge

if (keycode = = Key_left_arrow) {

Determine if the last one is reached

if (num = = 0) {

num = 15;

}else{

num--;

}

}

Move to the right and move to the end of the previous line if you move to the left edge

if (keycode = = Key_right_arrow) {

Determine if the first one is reached

if (num = = 15) {

num = 0;

}else{

num++;

}

}

Move up, or move to the upper boundary, to the lower boundary corresponding to the position

if (keycode = = Key_up_arrow) {

Determines whether to move to the upper boundary and, if so, to the corresponding position on the lower boundary

if (num-4 < 0) {

num = + num-3;

}else{

num = num-4;

}

}

Move down, and move to the upper boundary if you move to the lower boundary

if (keycode = = Key_down_arrow) {

Determines whether the lower boundary is reached, and if so, moves to the corresponding position on the upper boundary

if (num + 4 > 15) {

num = num + 3-15;

}else{

num = num + 4;

}

}

OK button

if (keycode = =-5) {

Action depending on the selection button

Action ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.