📄 imagemidlet.java
字号:
package com.j2medev.chapter3.image;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImageMidlet extends MIDlet implements CommandListener{
private Image img = null;
private Display display = null;
private List main = new List("Image",List.IMPLICIT);
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command backCommand = new Command("back",Command.BACK, 2);
public ImageMidlet(){
if(display == null){
display = Display.getDisplay(this);
try{
img = Image.createImage("/baby.png");
}catch(IOException ex){
ex.printStackTrace();
}
//创建缩略图
main.append("Lovely Baby",createThumbnail(img, 40));
main.addCommand(exitCommand);
main.setCommandListener(this);
}
}
public void startApp() {
display.setCurrent(main);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
//借助Clipping的概念实现图片缩放,精度有限
private Image createThumbnail(Image image,int width) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = width;
int thumbHeight = -1;
if (thumbHeight == -1)
//计算小图片高度
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
//创建缩略图
Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
}
}
Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == List.SELECT_COMMAND){
//显示图片
ImageCanvas canvas = new ImageCanvas(img);
canvas.addCommand(backCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}else if(cmd == backCommand){
display.setCurrent(main);
}else if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -