| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 823 人关注过本帖
标题:如何添加ZoomImage方法
只看楼主 加入收藏
NCNIIT
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-3-18
收藏
 问题点数:0 回复次数:0 
如何添加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;
    }

}
搜索更多相关主题的帖子: ZoomImage 
2009-11-16 19:38
快速回复:如何添加ZoomImage方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029212 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved