Programming
The/*
* Canvaslet.java
*
* Created on April 18, 2005, 9:41
*/
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 ("Large", 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, displayable D) {
int I=alist.getselectedindex ();
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 (alist);
}
}
}
}
The process of writing this program is not smooth, hehe, after all, is a novice ah.
1.Font
I've never been familiar with the fonts in canvas. Through this program, I learned that to get a Font object, you need to pass font f = font. GetFont (int face, int style, int size) to get.
2. General wording of the midlet containing canvas
Write the MIDlet first, and then write an inner class that inherits from canvas to implement the public void paint (Graphics g) {} method inside the class. The Paint method is automatically invoked at display time.
3. In subclasses of Canvas, command and Commandlistener interfaces can be used to implement functions such as back up.