j2me学习笔记【13】——创建矩形框、圆角矩形以及填充颜色小例子

    可以通过标记左上角和右下角的坐标来定义矩形的尺寸。在画布上可以绘制4中矩形,分别是矩形框、填充矩形、圆角矩形框、圆角填充矩形。
    可以调用drawRect()方法创建矩形框,调用fillRect()方法填充矩形。需要4个参数,前两个是矩形的左上角坐标(x1,y1),后两个参数是的宽度和高度(x2,y2)。
    必须在绘制举行前,使用setColor()方法设定用于绘制矩形的颜色。否则,就使用图形环境的当前颜色既绘制矩形的边框又填充矩形的内部,这依赖于要绘制的矩形的类型。
    生成一个圆角矩形和生成一个直角矩形的方法类似,只是必须指定用于兴城圆角的圆弧的水平直径和垂直直径。水平直径称为圆弧的宽度,垂直直径称为圆弧的高度。
    直径代表角的尖锐程度,即直径越小角越尖。水平直径和垂直直径都被定义成整数类型。
    可以分别调用drawRoundRect()方法和fillRoundRect()方法生成圆角矩形。这两个方法都需要6个参数。前4个参数标识了矩形的左上角坐标和右下角坐标。第5和第6个参数是代表角的水平直径和垂直直径的整数。

package mtk; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class RectangleExample extends MIDlet { private Display display; private MyCanvas1 canvas; public RectangleExample() { display=Display.getDisplay(this); canvas=new MyCanvas1(this); } protected void destroyApp(boolean arg0){ } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display.setCurrent(canvas); } public void exitMIDlet(){ destroyApp(true); notifyDestroyed(); } } class MyCanvas1 extends Canvas implements CommandListener{ private Command CMD_EXIT=new Command("退出",Command.EXIT,1); private RectangleExample rectangleExample; public MyCanvas1(RectangleExample rectangleExample) { this.rectangleExample=rectangleExample; addCommand(CMD_EXIT); setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255,255,255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255,0,0); graphics.drawRect(2, 2, 20, 20); graphics.drawRoundRect(20, 20, 60, 60, 15, 45); } public void commandAction(Command c, Displayable d) { if(c==CMD_EXIT){ rectangleExample.exitMIDlet(); } } }

 

颜色填充

package mtk; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class FilledRectangleExample extends MIDlet { private Display display; private MyCanvas2 canvas; public FilledRectangleExample() { display=Display.getDisplay(this); canvas=new MyCanvas2(this); } protected void destroyApp(boolean arg0){ } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display.setCurrent(canvas); } public void exitMIDlet(){ destroyApp(true); notifyDestroyed(); } } class MyCanvas2 extends Canvas implements CommandListener{ private Command CMD_EXIT=new Command("退出",Command.EXIT,1); private FilledRectangleExample rectangleExample; public MyCanvas2(FilledRectangleExample rectangleExample) { this.rectangleExample=rectangleExample; addCommand(CMD_EXIT); setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255,255,255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255,0,0); graphics.fillRect(2, 2, 20, 20); graphics.fillRoundRect(20, 20, 60, 60, 15, 45); } public void commandAction(Command c, Displayable d) { if(c==CMD_EXIT){ rectangleExample.exitMIDlet(); } } }

 

你可能感兴趣的:(c,cmd,command,Class,图形,j2me)