gpt4 book ai didi

java - 为什么不显示图像?

转载 作者:行者123 更新时间:2023-11-29 05:39:49 25 4
gpt4 key购买 nike

包主;

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

最佳答案

如果您查看 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;
}

如果加载图片失败,该图片将简单地为 null 而不会抛出任何错误。您的代码无法(静默地)加载图像(使用 System.out.println(image) 进行检查很可能是因为文件路径不正确。

编辑您的评论:我更喜欢 ImageIO 来加载我的文件,并为其提供输入流。它更冗长,并且有让我从 jars 中加载文件的额外好处。改变

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

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

关于java - 为什么不显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com