我的J2ME編程練習(7)——Canvas2

來源:互聯網
上載者:User
編程   
/*
 * 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.關於這一點,應該這樣理解:
既然使用了低級介面,那麼就意味著螢幕上的一切都歸你管,你有最大的權利。但你也有相應的義務,清除螢幕的工作自然也要你來做

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。