This section provides the source code for the Simple Test MIDlet example. For the complete Eclipse project ZIP file, see Forum Nokia.
The example includes the following classes:
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class SimpleTest extends MIDlet implements CommandListener { private HelloCanvas myCanvas; private Form myForm; private Gauge myGauge; private TextField textField; private Command backCommand = new Command("Back", Command.BACK, 1); private Command messageCommand = new Command("Message", Command.SCREEN,1); private Command displayCommand = new Command("Display Message", Command.SCREEN,1); private Command exitCommand = new Command("Exit", Command.EXIT, 1); private Command showCommand = new Command("Show Levels", Command.SCREEN, 1); public SimpleTest(){ myCanvas = new HelloCanvas(); myCanvas.addCommand(backCommand); myCanvas.addCommand(messageCommand); myForm = new Form("Gauge level"); myGauge = new Gauge("Value", true, 120, 10); textField = new TextField("Enter number", "", 3, TextField.NUMERIC); myForm.append(myGauge); myForm.append(textField); myForm.addCommand(showCommand); myForm.addCommand(displayCommand); myForm.addCommand(exitCommand); myCanvas.setCommandListener(this); myForm.setCommandListener(this); } public void startApp(){ Display.getDisplay(this).setCurrent(myForm); } public void pauseApp(){} public void destroyApp(boolean a) throws MIDletStateChangeException{} public void commandAction(Command c, Displayable d){ if (c == exitCommand){ notifyDestroyed(); } if (c == messageCommand){ myCanvas.newMessage(); //myCanvas.start(); } if (c == backCommand){ Display.getDisplay(this).setCurrent(myForm); } if (c== displayCommand){ Display.getDisplay(this).setCurrent(myCanvas); myCanvas.start(); } if (c==showCommand){ String valueString = textField.getString(); int value = 0; if (!valueString.equals("")) { value = Integer.parseInt(valueString); } myGauge.setValue(value); } } }
import javax.microedition.lcdui.*; public class HelloCanvas extends Canvas { boolean myCanvasTXT = true; public HelloCanvas() { } void start(){ repaint(); } public void newMessage(){ myCanvasTXT = !myCanvasTXT; repaint(); } public void paint(Graphics g){ int w = getWidth(); int h = getHeight(); g.setColor(0xffff00); g.fillRect(0, 0, w, h); //display the message if(myCanvasTXT){ Font font = g.getFont(); int fontHeight = font.getHeight(); int fontWidth = font.stringWidth("CANVAS FONT"); //set the text color g.setColor(0x00ff0000); g.setFont(font); //write the strings in the center of the screen g.drawString("CANVAS FONT !!!", (w-fontWidth)/2, (h-fontHeight)/2, Graphics.TOP | Graphics.LEFT); } } }