欢迎光临
我们一直在努力

我的J2ME编程练习(7)——Canvas2-JSP教程,J2ME开发

建站超值云服务器,限时71元/月

  

/*

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

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 我的J2ME编程练习(7)——Canvas2-JSP教程,J2ME开发
分享到: 更多 (0)