/ * * Canvaslet.java * * CREATED ON April 18, 2005, 9:41 pm * /
Import javax.microedition.midlet. *; import javax.microedition.lcdui. *;
/ ** * * @author Administrator * @version * / public class Canvaslet extends MIDlet implements CommandListener {private Display aDisplay; private MyCanvas myCanvas; private Command okCommand; private Command exitCommand; private List aList; private Font f; public Canvaslet () { Okcommand = New Command ("OK", Command.ok, 1); EXITCOMMAND = New Command ("EXIT", Command.exit, 1); Alist = New List ("font", choice.exclusive; alist.Append "Small", null; alist.append ("medium", null); alist.append ("lad", null); mycanvas = new mycanvas (); alist.addcommand (OkCommand); alist.addcommand (EXITCOMMAND); aList.setCommandListener (this);} public void startApp () {aDisplay = Display.getDisplay (this); aDisplay.setCurrent (aList);} public void pauseApp () {} public void destroyApp (boolean unconditional) {} public void commandAction (Command C, Displaya BLE D) {INT i = alist.getSelectedIndIndex (); if (c == exitcommand) {destroyApp (false); notifyDestroyed ();} else if (c == okcommand); {if (i == 0) {Mycanvas .setsize (font.size_small);} else if (i == 1) {mycanvas.setsize (font.size_medium);} else if (i == 2) {mycanvas.setsize (font.size_large);} adisplay.setcurrent (Mycanvas);}} Class Mycanvas Extends Canvas Implements CommandListener {Private Command EXITCMD;
Public mycanvas () {EXITCMD = New Command ("Back", Command.back, 1); addcommand (exitcmd); setcommandlistener (this);} private void setsize (int size) {f = font.getfont (font.face_system, Font.style_bold, size);} public void Paint (graphics g) {string s = "hello"; g.setcolor (0x00808080); g.setfont (f); g.drawstring (s, getwidth () / 2, getHeight () / 2, graphics.top|graphics.left); g.drawline (0,0,100,100);} public void CommandAction (Command C, Displayable D) {if (c == exitcmd) {adisplay.setcurrent (aliist); }}}} The programming process of this program is very uncomfortable, huh, huh, it is a novice after all.
Font
For the fonts in Canvas, I have never been familiar with it. Through this programming, I learned that you have to get a font object, you need to get Font F = Font.GetFont (int frame, int style, int size).
2. General Write MiDlet with Canvas
First write the MIDlet, then write an internal class inherited from Canvas, implement the public void Paint (Graphics G) {} method internally in this class. The PAINT method is automatically called when Display.
3. In Canvas's subclasses, you can use the Command and CommandListener interface to implement back and other functions.