我的J2ME編程練習(7)——Canvas2 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 我的J2ME編程練習(7)——Canvas2 (http://www.webasp.net/article/18/17913.htm) |
| -- 作者:未知 -- 發佈日期: 2005-04-25 |
|
/* * Canvas2let.java * * Created on 2005年4月19日, 下午5:27 */ 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("外觀",Choice.EXCLUSIVE); faceChoice.append("PROPORTIONAL",null); faceChoice.append("MONOSPACE",null); faceChoice.append("SYSTEM",null); styleChoice=new ChoiceGroup("風格",Choice.MULTIPLE); styleChoice.append("PLAIN",null); styleChoice.append("BOLD",null); styleChoice.append("ITALIC",null); styleChoice.append("UNDERLINED",null); sizeChoice=new ChoiceGroup("大小",Choice.EXCLUSIVE); sizeChoice.append("SMALL",null); sizeChoice.append("MEDIUM",null); sizeChoice.append("LARGE",null); exitCommand=new Command("退出",Command.EXIT,1); okCommand=new Command("確定",Command.OK,1); backCommand=new Command("後退",Command.BACK,2); choiceForm.append(faceChoice); choiceForm.append(styleChoice); 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[] styleSelect =new boolean[4]; for(int i=0;i<4;i++){ if (styleChoice.isSelected(i)&&i==0){ style |= Font.STYLE_PLAIN; } else if (styleChoice.isSelected(i)&&i==1){ style |=Font.STYLE_BOLD; } else if (styleChoice.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(0xFFFFFF); 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 commandAction(Command c ,Displayable d){ if (c==backCommand){ aDisplay.setCurrent(choiceForm); } } } } 這個程序主要是加深對Canvas的Font的認識。 1.MIDlet類的構造函數應該是public Canvas2let()而不應該是public void Cancas2let()。因為加了void就不會返回該類的對象,導致程序運行後沒有反映。這是一個教訓。 2.在Canvas中,需要使用代碼 g.setColor(0xFFFFFF); g.fillRect(0,0,getWidth(),getHeight()); 來清除上一個界面。然後再使用 g.setColor(0);來設置繪畫顏色。 3.關於這一點,應該這樣理解: 既然使用了低級界面,那麼就意味著屏幕上的一切都歸你管,你有最大的權利。但你也有相應的義務,清除屏幕的工作自然也要你來做 |
| webasp.net |