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.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class Exec extends MIDlet {
private Display display;
public Exec() {
display = Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(new DrawPanel());
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class DrawPanel extends Canvas implements CommandListener {
final int step = 3;
final int zoomRatio = 10;
int currentX = 0;
int currentY = 0;
Image image;
Image originalImage;
Command command = new Command("恢复", Command.ITEM, 1);
public DrawPanel() {
image = getImage("/images/java.jpg");
originalImage = image;
addCommand(command);
setCommandListener(this);
}
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
g.drawImage(image, getWidth() / 2 - imageWidth / 2 - currentX,
getHeight() / 2 - imageHeight / 2 - currentY, Graphics.TOP
| Graphics.LEFT);
}
public Image getImage(String imagePath) {
Image image = null;
try {
image = Image.createImage(imagePath);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return image;
}
public void commandAction(Command com, Displayable disp) {
currentX = 0;
currentY = 0;
image = originalImage;
repaint();
}
protected void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case Canvas.UP:
currentY -= step;
break;
case Canvas.DOWN:
currentY += step;
break;
case Canvas.LEFT:
currentX -= step;
break;
case Canvas.RIGHT:
currentX += step;
break;
case Canvas.FIRE:
image = ZoomImage(originalImage, image.getWidth() + zoomRatio, image
.getHeight()
+ zoomRatio);
break;
}
repaint();
}
protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}
public Image ZoomImage(Image src, int desW, int desH) {
Image desImg = null;
int srcW = src.getWidth(); // 原始图像宽
int srcH = src.getHeight(); // 原始图像高
int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// 计算插值表
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for (int i = 0; i <= distance; i++) { /* 垂直方向 */
tabY[db] = sb;
tems += srcH;
temd += desH;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for (int i = 0; i <= distance; i++) { /* 水平方向 */
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
// 生成放大缩小后图形像素buf
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for (int i = 0; i < desH; i++) {
if (oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
} else {
dx = 0;
for (int j = 0; j < desW; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
// 生成图片
desImg = Image.createRGBImage(desBuf, desW, desH, false);
return desImg;
}
}
[此贴子已经被作者于2010-12-12 18:52:18编辑过]