j2me学习笔记【14】——创建圆弧并填充颜色小例子

    技术2025-03-14  10

        圆弧式用于绘制圆、椭圆和其他曲线图形的曲线段。绘制圆弧的过程中,第一步是定义画布上会被圆弧覆盖的区域,这个区域使用矩形定义而不是圆弧的圆周。把这个区域当做一个盒子定义,在这个盒子中绘制角。同坐指定两组坐标定义矩形。一组是矩形左上角的坐标(x1,y1),另一组是矩形右下角的坐标(x2,y2)。 一旦定义了矩形,您必须定义用于绘制圆弧的终点。在一个钟表商,3点的位置是0,当您逆时针方向移动时,度数就会递增;12点的位置是90,9点是180,6点是270;当你顺时针方向移动时,度数就会递减。根据钟表这幅图,您可以选择圆弧绘制的起始角度。类似地,您也可以选择圆弧的终止角度。 可以调用drawArc()方法绘制圆弧线,调用fillArc()方法绘制填充的圆弧。需要6个整形参数。前两个包含矩形的左上角的坐标(x1,y1),接着的是矩形的宽和高(x2,y2),第5个是起始角度,最后一个参数是终止角度。

    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 ArcExample extends MIDlet { private Display display; private myCanvas3 canvas; public ArcExample() { display=Display.getDisplay(this); canvas=new myCanvas3(this); } protected void destroyApp(boolean arg0) { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display.setCurrent(canvas); } public void exitMIDlet(){ destroyApp(true); notifyDestroyed(); } } class myCanvas3 extends Canvas implements CommandListener{ private Command CMD_EXIT=new Command("退出",Command.EXIT,1); private ArcExample arcExample; public myCanvas3(ArcExample arcExample) { this.arcExample=arcExample; 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.drawArc(0, 0, getWidth(), getHeight(), 0, 270); graphics.fillArc(getWidth()/4, getHeight()/4, getWidth()/2, getHeight()/2, 0, 270); } public void commandAction(Command c, Displayable d) { if(c==CMD_EXIT){ arcExample.exitMIDlet(); } } }

    最新回复(0)