简体   繁体   English

为什么图像没有显示?

[英]Why are the images not being displayed?

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));

}
}

There are no errors!, the program runs, it displays the text, but not the images. 没有错误!,程序运行,它显示文本,但不显示图像。 Which means that loadImages() works, it must be a mistake in my paint method. 这意味着loadImages()有效,在我的paint方法中一定是个错误。 What am I doing wrong!?!? 我究竟做错了什么!?!?

I don't see what is wrong with my path: 我没有看到我的路径有什么问题: 在此输入图像描述

If you take a look at the ImageIcon source you will notice that the ImageIcon(String) constructor calls another constructor 如果您查看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);
}

and .getImage() .getImage()

public Image getImage() {
     return image;
}

If it fails to load an image that image will simply be null without throwing any errors. 如果无法加载图像,则图像将为空而不会丢失任何错误。 Your code fails (silently) to load the image (check this with a System.out.println(image) most likely because of an incorrect filepath. 您的代码无法(静默地)加载图像(使用System.out.println(image)检查这很可能是因为文件路径不正确)。

Edit to your comments: I prefer ImageIO to load my files feeding it an inputstream. 编辑你的评论:我更喜欢ImageIO加载我的文件,为它提供输入流。 It is more verbose and has the added benefit of letting me load files from within jars. 它更冗长,还有让我从jar中加载文件的额外好处。 Change 更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM