当前位置: 代码迷 >> J2ME >> J2ME作图工具类Graphics
  详细解决方案

J2ME作图工具类Graphics

热度:4708   发布时间:2013-02-25 21:31:46.0
J2ME绘制工具类Graphics

Graphics是很重要的类在J2ME,它有9个静态属性: BASELINE BOTTOMHCENTER LEFTRIGHTSOLIDDOTTED TOPVCENTER,其中三个BASELINEDTTED是不能用来修饰文本的,他的绘制方法的参数记得当时学的时候疑惑了半天,我觉得我们在看它的参数的时候先想要是自己去设计这个方法自己会怎么样,再写个代码把各种放置参数都测试一下,就一幕了然了.下面是我的理解吧.

1,弄清出坐标:MIDP中的坐标的y方向与我们平时的相反,它的y轴的方向是向下的.

2,弄清那些静态属性是不可以组合的,:RIGHTLEFT显然不能组合,这些组合我们不要想得太复杂,常识判断就够了

3,弄清各个静态属性的使用范围,那些可以修饰图片,那些可以修饰文本等等

4,要把绘制的对象想像成一个矩形,下面是我提供的入门级别的例子.

package datuu.srk.demo.canvas;

<o:p>?</o:p>

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import java.io.IOException;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.Displayable;

<o:p>?</o:p>

public class ImageGraphicsDemo extends MIDlet {

??? private static ImageGraphicsDemo instance;

??? private static ImageGraphicsDemoCanvas displayImageGraphicsDemo;

??? public ImageGraphicsDemo() {

??????? instance = this;

??????? displayImageGraphicsDemo = new ImageGraphicsDemoCanvas();

??? }

??? protected void startApp() throws MIDletStateChangeException {

??????? Display.getDisplay(instance).setCurrent(displayImageGraphicsDemo);

??? }

<o:p>?</o:p>

??? protected void pauseApp() {

??? }

<o:p>?</o:p>

??? protected void destroyApp(boolean _boolean) throws

??????????? MIDletStateChangeException {

??? }

??? public static void? quitApp() {

??????? try {

??????????? instance.destroyApp(false);

??????? } catch(MIDletStateChangeException e) {

??????????? e.printStackTrace();

??????? }

??????? instance.notifyDestroyed();

??? }

}

class ImageGraphicsDemoCanvas extends Canvas implements CommandListener {

??? private Image image;

??? private static final int ADJUST =Graphics.HCENTER |Graphics.TOP??? ; //测试时候要改变的值

??? public ImageGraphicsDemoCanvas() {

??????? init();

??? }

??? protected? void paint(Graphics g) {

??????? g.setColor(0,255,0);

??????? int x = image.getWidth();

??????? int y = image.getHeight();

??????? System.out.println("现在的图片坐标是:("+x+","+y+")");

??????? g.drawImage(image,x,y,ADJUST);

??? }

??? public void init() {

??????? try {

??????????? image = Image.createImage("/dmusic.png");

??????? } catch(IOException e) {

??????????? System.out.println("打开图片出现异常");

??????????? e.printStackTrace();

??????? }

??????? this.addCommand(new Command("退出",Command.EXIT ,1));

??????? this.setCommandListener(this);

??? }

<o:p>?</o:p>

??? public void commandAction(Command command, Displayable displayable) {

??????? if(command.getCommandType() == Command.EXIT ) {

??????????? ImageGraphicsDemo.quitApp();

??????? }

??? }

}

<o:p>?</o:p>

下面是绘画线条的例子,注意要先设置所话的属性,再化

<o:p>?</o:p>

package datuu.srk.demo.canvas;

<o:p>?</o:p>

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.Displayable;

<o:p>?</o:p>

public class LineGraphicsDemo extends MIDlet {

??? private static LineGraphicsDemo instance;

??? private LineGraphicsDemoCanvas displayLineGraphicsDemoCanvas;

??? public LineGraphicsDemo() {

??????? instance = this;

??????? displayLineGraphicsDemoCanvas = new LineGraphicsDemoCanvas();

??? }

<o:p>?</o:p>

??? protected void startApp() throws MIDletStateChangeException {

??????? Display.getDisplay(instance).setCurrent(displayLineGraphicsDemoCanvas);

??? }

<o:p>?</o:p>

??? protected void pauseApp() {

??? }

<o:p>?</o:p>

??? protected void destroyApp(boolean _boolean) throws

??????????? MIDletStateChangeException {

??? }

<o:p>?</o:p>

??? public static void quitApp() {

??????? try {

??????????? instance.destroyApp(false);

??????? } catch (MIDletStateChangeException e) {

??????????? e.printStackTrace();

??????? }

??????? instance.notifyDestroyed();

??? }

}

<o:p>?</o:p>

<o:p>?</o:p>

class LineGraphicsDemoCanvas extends Canvas implements CommandListener {

??? private static final int ADJUST = Graphics.DOTTED | Graphics.HCENTER;

??? public LineGraphicsDemoCanvas() {

??????? this.addCommand(new Command("退出", Command.EXIT, 1));

??????? this.setCommandListener(this);

??? }

<o:p>?</o:p>

??? public void paint(Graphics g) {

<o:p>?</o:p>

??????? g.setColor(255, 255, 255);

??????? g.fillRect(0, 0, getWidth(), getHeight());

<o:p>?</o:p>

??????? g.setColor(255, 0, 0);

??? ????g.setStrokeStyle(Graphics.DOTTED); //虚线

??????? g.drawLine(10, 10, 100, 10);

<o:p>?</o:p>

??????? g.setColor(0, 255, 0);

??????? g.setStrokeStyle(Graphics.SOLID); //实线

??????? g.drawLine(10, 100, 100, 100);

<o:p>?</o:p>

??? }

<o:p>?</o:p>

??? public void commandAction(Command command, Displayable displayable) {

??????? if (command.getCommandType() == Command.EXIT) {

??????????? LineGraphicsDemo.quitApp();

??????? }

??? }

}