package com; import java.io.IOException; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.Sprite; /** * @author 徐彬 * @see 画布(Canvas) 画笔(Graphics)--> 画图形 */ public class CanvasTest extends MIDlet{ //这是J2ME手机程序主入口类 Display dis; private acanvas mc=new acanvas(this);//创建内部类acanvas的对象,并将自身对象传入。 public CanvasTest() { //构造函数 } /** * @author Administrator * 内部类 * 手机应用程序界面设计:Canvas类,相当于画布 */ class acanvas extends Canvas implements CommandListener //在这里需要实现CommandListener 事件监听接口 { //menu弹出菜单 将help和exit按钮包含进菜单中 private Command resumeCmd= new Command("", Command.SCREEN, 5); private Command help = new Command("帮助",Command.SCREEN,5); private Command back = new Command("回退",Command.SCREEN,2); private Command exit = new Command("退出",Command.SCREEN,5); private TextBox helpText= new TextBox("标题","帮助内容",200,0); public acanvas(CanvasTest canvasTest){ repaint();//加载页面组件 addCommand(resumeCmd); addCommand(help); addCommand(exit); helpText.addCommand(back);//将back回退按钮放入帮助页面(第2个页面)中 setCommandListener(this);//有效化按键事件 helpText.setCommandListener(this); } protected void paint(Graphics g) { try { //画图片 g.drawImage(Image.createImage("/image/bj.jpg"), 160, 0,Graphics.TOP|Graphics.HCENTER); //高级画图片~Sprite旋转图片 Image img=Image.createImage("/image/dns.JPG"); g.drawRegion(img, 0, 0, img.getWidth(), img.getHeight(), Sprite.TRANS_MIRROR_ROT90, 169, 200, Graphics.TOP|Graphics.HCENTER); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } g.setColor(200,33,134); //为下边的图形设置颜色 g.setStrokeStyle(Graphics.DOTTED); //设置为虚线 g.drawLine(19, 53, 45, 35); //划线 g.setStrokeStyle(Graphics.SOLID); //设置为实线 g.drawRect(59, 45, 45, 35); //方角矩形 g.drawRoundRect(120, 45, 45,35,10,10);//圆角矩形 g.setColor(240,33,234); //为下边的图形设置颜色 g.fillRoundRect(120, 45, 45,35,10,10);//填充圆角矩形 g.drawArc(19, 110, 20, 30, 0, 270); //画弧线 g.setColor(54, 34, 100); //为下边的图形设置颜色 g.fillArc(19, 110, 20, 30, 0, 270); //填充圆 g.fillTriangle(50, 110, 40, 150, 90, 150);//填充三角 g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE)); //设置字体 g.drawString("叼啊",110,110, Graphics.TOP|Graphics.LEFT); //划文本 g.drawString("fuck",this.getWidth()/2,this.getHeight()/2, Graphics.TOP|Graphics.HCENTER); } void goHelp(){ dis.setCurrent(helpText);//跳转到helpTest界面(第2个界面 ) } void goBack(){ dis.setCurrent(mc);//跳转到初始界面 } public void commandAction(Command command, Displayable displayable) { if(command == help){ //如果按键事件为help goHelp(); }else if(command == back){ //如果按键事件为back goBack(); }else if(command == exit){ //如果按键事件为exit try { destroyApp(false);//必要的两个退出方法 notifyDestroyed(); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } protected void startApp() throws MIDletStateChangeException { dis=Display.getDisplay(this); mc.setTitle("mycanvas"); dis.setCurrent(mc);//初始化界面,初始显示的是mc界面 } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } }