如何在屏幕上显示未知尺寸的图片

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

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++;
    }

}