为什么图像没有显示?

时间:2013-08-04 05:08:04

标签: java swing awt

package main;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class ImageTest extends JFrame {

public static void main(String[] args) {
    DisplayMode displayMode;

    if (args.length == 3) {
        displayMode = new DisplayMode(Integer.parseInt(args[0]),
                Integer.parseInt(args[1]), Integer.parseInt(args[2]),
                DisplayMode.REFRESH_RATE_UNKNOWN);
    } else {
        displayMode = new DisplayMode(800, 600, 16,
                DisplayMode.REFRESH_RATE_UNKNOWN);
    }

    ImageTest test = new ImageTest();
    test.run(displayMode);
}

private SimpleScreenManager screen;
private boolean imagesLoaded;

private Image bgImage;
private Image opaqueImage;
private Image transparentImage;
private Image translucentImage;
private Image antiAliasedImage;

private void run(DisplayMode displayMode) {
    setBackground(Color.blue);
    setForeground(Color.white);
    setFont(new Font("Dialog", Font.PLAIN, 24));
    imagesLoaded = false;
    screen = new SimpleScreenManager();
    try {
        screen.setFullScreen(displayMode, this);
        loadImages();

        try {
            Thread.sleep(10000);
        } catch (Exception e) {

        }
    } catch (Exception e) {

    } finally {
        screen.restoreScreen();
    }
}

private void loadImages() {
    bgImage = loadImage("/images/background.png");
    opaqueImage = loadImage("/images/opaque.png");
    transparentImage = loadImage("/images/transparent.png");
    translucentImage = loadImage("/images/translucent.png");
    antiAliasedImage = loadImage("/images/antialiased.png");
    imagesLoaded = true;
    repaint();
}

private Image loadImage(String fileName) {
    return new ImageIcon(fileName).getImage();

}

public void paint(Graphics g) {
    if (g instanceof Graphics2D) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

    g.drawImage(opaqueImage, 0, 0, this);

    if (imagesLoaded) {
        g.drawImage(bgImage, 0, 0, null);
        drawImage(g, opaqueImage, 0, 0, "Opaque");
        drawImage(g, transparentImage, 320, 0, "Transparent");
        drawImage(g, translucentImage, 0, 300, "Translucent");
        drawImage(g, antiAliasedImage, 320, 300,
                "Translucent (Anti-Aliased)");
    } else {
        g.drawString("Loading Images...", 5, 24);
    }
}

public void drawImage(Graphics g, Image image, int x, int y, String caption) {

    g.drawImage(image, x, y, this);
    g.drawString(caption, x + 5, y + 24 + image.getHeight(null));

}
}

没有错误!,程序运行,它显示文本,但不显示图像。这意味着loadImages()有效,在我的paint方法中一定是个错误。我做错了什么!?!?

我看不出路径有什么问题: enter image description here

1 个答案:

答案 0 :(得分:3)

如果您查看ImageIcon来源,您会注意到ImageIcon(String)构造函数调用另一个构造函数

ImageIcon(String filename, String description) {
    image = Toolkit.getDefaultToolkit().getImage(filename);
    if (image == null) {
        return;
   }
   this.filename = filename;
   this.description = description;
   loadImage(image);
}

.getImage()

public Image getImage() {
     return image;
}

如果无法加载图像,则图像将为空而不会丢失任何错误。您的代码无法(静默地)加载图像(由于文件路径不正确,最有可能使用System.out.println(image)进行检查。

修改您的评论:我更喜欢ImageIO加载我的文件,为其提供输入流。它更冗长,还有让我从jar中加载文件的额外好处。变化

private Image loadImage(String fileName) {
     return new ImageIcon(fileName).getImage();
}

private Image loadImage(String fileName) {
     return ImageIO.read(getClass().getResourceAsStream(fileName));
}