我的J2ME编程练习(7)——Canvas2

类别:Java 点击:0 评论:0 推荐:

/*
 * 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.关于这一点,应该这样理解:

既然使用了低级界面,那么就意味着屏幕上的一切都归你管,你有最大的权利。但你也有相应的义务,清除屏幕的工作自然也要你来做


本文地址:http://com.8s8s.com/it/it10148.htm