1、显示、移动图像:
1)image=Image.createImage(70,70);
使用createImage创建动态图像对象,即在创建了一个存储图像的内存块,但目前这个内存块并无图像
2) Graphics graphics=image.getGraphics();
graphics.setColor(255,0,0);
graphics.fillArc(10,10,60,50,180,180);
在图像内存块中绘制一个圆弧,注意这仅是图像实例,并没有在屏幕显示出来。
3)public void paint(Graphics g) {
if (image!=null){
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.translate(45, 45);
g.drawImage(image, 0, 0, Graphics.VCENTER|Graphics.HCENTER);
}
}
完成把图像显示出来。
g.translate(45, 45);
完成对画布左上角坐标(0,0)所代表的实际坐标的重新赋值,进行坐标转换。此方法仅影响调用之后所建文本和图像的位置。
drawImage完成显示图像功能
2、图
3、代码
MIDPCanvas.java
package test;
import javax.microedition.lcdui.*;
public class MIDPCanvas extends Canvas implements CommandListener{
private Midlet midlet;
private Command cmd_exit;
private Image image=null;
publicMIDPCanvas(Midlet midlet) {
this.midlet=midlet;
cmd_exit=new Command("退出", Command.EXIT, 1);
try {
image=Image.createImage(70,70);
Graphics graphics=image.getGraphics();
graphics.setColor(255,0,0);
graphics.fillArc(10,10,60,50,180,180);
// Set up this canvas to listen to command events
setCommandListener(this);
// Add the Exit command
addCommand(cmd_exit);
} catch(Exception e) {
e.printStackTrace();
}
}
public voidpaint(Graphics g) {
if (image!=null){
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.translate(45, 45);
g.drawImage(image, 0, 0, Graphics.VCENTER|Graphics.HCENTER);
}
}
protected void keyPressed(int keyCode) {
}
protected void keyReleased(int keyCode) {
}
protected void keyRepeated(int keyCode) {
}
protected void pointerDragged(int x, int y){
}
protected void pointerPressed(int x, int y){
}
protected void pointerReleased(int x, int y){
}
public voidcommandAction(Command command, Displayable displayable) {
if (command==cmd_exit){
midlet.exitMidlet();
}
}
}
Midlet.java
package test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet {
privateMIDPCanvas canvas;
privateDisplay display;
publicMidlet(){
canvas=new MIDPCanvas(this);
display=Display.getDisplay(this);
}
public voidstartApp() {
display.setCurrent(canvas);
}
publicvoid pauseApp() {
}
public voidexitMidlet(){
destroyApp(true);
notifyDestroyed();
}
publicvoid destroyApp(boolean unconditional) {
}
}