首页 arrow J2ME开发 arrow 新手上路 arrow 如何在屏幕上显示未知尺寸的图片
如何在屏幕上显示未知尺寸的图片
Author Author: Wupei | Date Date: 2009-08-22 | View Count View: 2430 | Section & Category J2ME开发 - 新手上路 | Digg Digg: 4

下边的代码说明了当你不知道图片尺寸大小的情况下,如何在屏幕上显示图片

class DrawImageCanvas extends Canvas {
 
    static Image image;
    int count;
 
    public void paint(Graphics g) {
 
        int width = getWidth();
        int height = getHeight();
 
        // 将背景设为黑色
        g.setColor(0);
        g.fillRect(0, 0, width, height);
 
        // 加载图片
        if (image == null) {
 
            try {
 
                image = Image.createImage("resources/earth.png");
 
            } catch (IOException ex) {
 
                g.setColor(0xffffff);
                g.drawString("加载图片出错!", 0, 0, Graphics.TOP |
                             Graphics.LEFT);
                return;
            }
        }
 
        switch (count % 3) {
 
        case 0:
 
            // 将图片放在左上角
            g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
            break;
 
        case 1:
 
            // 将图片放在右下角
            g.drawImage(image, width, height, Graphics.BOTTOM |
                        Graphics.RIGHT);
            break;
 
        case 2:
 
            // 居中
            g.drawImage(image, width / 2, height / 2, Graphics.VCENTER |
                        Graphics.HCENTER);
        }
 
        count++;
    }
 
}

更多阅读:

 

尚无评论发表

我要发表评论

登录菜单

最新文章

本月热门

订阅本站

RSS 0.91 RSS 1.0 RSS 2.0 ATOM 0.3 OPML