My J2ME programming exercises (7) - canvas2

xiaoxiao2021-03-05  40

/ * * Canvas2let.java * * Created on April 19, 2005, 5:27 pm * /

Import javax.microedition.midlet. *; import javax.microedition.lcdui. *;

/ ** * * @author Administrator * @version * / public class Canvas2let extends MIDlet implements CommandListener {private ChoiceGroup faceChoice; private ChoiceGroup styleChoice; private ChoiceGroup sizeChoice; private Command okCommand; private Command exitCommand; private Command backCommand; private TextBox faceTextBox; private TextBox styleTextBox; private TextBox sizeTextBox; private Form choiceForm; private Display aDisplay; private MyCanvas canvas; int face; int size; int style; public Canvas2let () {choiceForm = new Form ( "Select Font"); faceChoice = new ChoiceGroup ( " Appearance ", choice.exclusive); Facechoice.Append (" PrOPORTIONAL ", NULL); Facechoice.Append (" Monospace ", NULL); Facechoice.Append (" System ", NULL); Stylechoice = New ChoiceGroup (" style ", Choice.multiple; Stylechoice.Append ("plain", null); Stylechoice.Append ("bold", null); STYLECHOICE.APPEND ( "Italic", null; STYLECHOICE.APPEND ("underlined", null); sizechoice = new choffloup; sizechoice.Append ("small", null); Sizechoice.Append ("Medium" , NULL); Sizechoice.Append ("Large", NULL); EXITCOMMAND = New Command ("Exit", Command.exit, 1); OKCommand = New Command ("OK", Command.ok, 1); Backcommand = New Command ("Back", Command.back, 2); Choiceform.Append (FaceChoice); Choiceform.Append (Sizechoice); Choiceform.Append (Sizechoice);

choiceForm.addCommand (exitCommand); choiceForm.addCommand (okCommand); choiceForm.setCommandListener (this); canvas = new MyCanvas ();} public void startApp () {aDisplay = Display.getDisplay (this); aDisplay.setCurrent (choiceForm) ;} public void pauseApp () {} public void destroyApp (boolean unconditional) {} public void commandAction (Command c, Displayable d) {if (c == exitCommand) {destroyApp (false); notifyDestroyed ();} else {/ / select the face and size face = faceChoice.getSelectedIndex (); switch (face) {case 0: face = Font.FACE_PROPORTIONAL; break; case 1: face = Font.FACE_MONOSPACE; break; case 2: face = Font.FACE_SYSTEM; Break;} size = sizechoice.getSelectedIndex (); switch (size) {case 0: size = font.size_small; break; case 1: size = font .Size_medium; break; case 2: size = font.size_large; break;} // select the style boolean [] stylelect = new boolean [4]; for (int i = 0; i <4; i ) {IF (STYLELECHOICE .isselected (i) && i == 0) {style | = font.style_plain;} else if (stylechoice.isselected (i) && i == 1) {style | = font.style_bold;} else f (STYLETOICE.ISselected (i ) && i == 2) {style | = font.style_italic;

} Else if (styleChoice.isSelected (i) && i == 3) {style | = Font.STYLE_UNDERLINED;}} // canvas = new MyCanvas (); aDisplay.setCurrent (canvas);}} class MyCanvas extends Canvas implements CommandListener { Public mycanvas () {addcommand (backcommand); setcommandlistener (this);} public void Paint (graphics g) {g.setcolor (0xfffff); g.fillRect (0, 0, getWidth (), GetHeight ()); g. SetColor (0); string s = "Hello World"; font f = font.getfont (face, style, size); g.setfont (f); g.drawstring (s, 150, 250, graphics.right | graphics.bottom) } Public void command (Command C, Displayable D) {if (c == backcommand) {adjisplay.setcurrent (choiceform);}} }} This program is mainly to deepen the understanding of the Font of Canvas. 1. MIDlet class constructor should be public canvas2Let () without public void cancas2let (). Because the added void will not return the object of this class, it is not reflected after the program is running. This is a lesson.

2. In Canvas, you need to use the code

G.SetColor (0xfffffff); G.FillRect (0, 0, getWidth (), getHeight ());

To clear the previous interface. Then use G.SetColor (0); set the painting color.

3. About this, you should understand this:

Since you use a low-level interface, you mean that everything on the screen is in your tube, you have the greatest right. But you also have the corresponding obligations, and the work of clearing the screen is naturally going to do

转载请注明原文地址:https://www.9cbs.com/read-33506.html

New Post(0)