如何添加ZoomImage方法
import java.util.Random;import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
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 {
final int length = getHeight();
int[] stars = new int[length];
final int size = 10;
int x = 0;
int y = 0;
int srcW;
int srcH;
Image image;
public Image ZoomImage(Image src, int desW, int desH) {
Image desImg = getImage("/images/java.jpg");
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;
}
public DrawPanel() {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < length; ++i) {
stars[i] = -1;
}
Random generator = new Random();
while (true) {
for (int i = length - 1; i > 0; --i) {
stars[i] = stars[i - 1];
}
stars[0] = generator.nextInt() % getWidth();
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
}).start();
}
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 255, 255);
for (int y = 0; y < getHeight(); ++y) {
int x = stars[y];
if (x != -1)
g.drawLine(x, y, x, y);
}
Image image = getImage("/images/java.jpg");
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
g.drawImage(image, getWidth() / 2 - imageWidth / 2 - x, getHeight() / 2
- imageHeight / 2 - y, Graphics.TOP | Graphics.LEFT);
}
protected void keyRepeated(int keyCode) {
switch (getGameAction(keyCode)) {
case Canvas.LEFT:
x += 5;
break;
case Canvas.RIGHT:
x -= 5;
break;
case Canvas.UP:
y += 5;
break;
case Canvas.DOWN:
y -= 5;
break;
case Canvas.FIRE :
//我需要点击这个按钮 图片缩放 zoomimage方法是添加在这里吗?怎么添加?
break;
}
repaint();
}
protected void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case Canvas.LEFT:
x += 2;
break;
case Canvas.RIGHT:
x -= 2;
break;
case Canvas.UP:
y += 2;
break;
case Canvas.DOWN:
y -= 2;
break;
}
}
public Image getImage(String imagePath) {
Image image = null;
try {
image = Image.createImage(imagePath);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return image;
}
}