如何在 Java 小程序中显示位图图像?
我很难弄清楚如何在 Java 小程序中显示图像(或 ImageIcon)。 以下是我的代码。 图片(test.bmp)确实存在并且位于 D 驱动器上,但是当我运行它时,我得到的小程序窗口中没有任何内容。 有人可以告诉我我缺少什么来使 ImageIcon 显示吗?
public class Form1 extends JApplet {
ImageIcon i;
public void init(){
i = new ImageIcon("D:\test.bmp");
}
public void paint(Graphics g){
i.paintIcon(this, g, 0, 0);
}
}
谢谢,史蒂夫。
I am having a hard time figuring out how to show an Image (or ImageIcon) in a Java applet. The following is my code. The picture (test.bmp) does exist and is on the D drive but when I run this I get the applet window with nothing in it. Can somebody tell me what I am missing to make the ImageIcon show?
public class Form1 extends JApplet {
ImageIcon i;
public void init(){
i = new ImageIcon("D:\test.bmp");
}
public void paint(Graphics g){
i.paintIcon(this, g, 0, 0);
}
}
Thanks, Steve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您从服务器运行小程序时,通过绝对本地文件路径引用图像可能不起作用。
使用 ImageIcon(URL 位置)构造函数,并让 URL 指向服务器上的图像资源。 使用 JApplet.getCodeBase() 确定您的小程序的来源并将文件名附加到它。编辑: ImageIO 支持 BMP,更改后的示例对我有用。
编辑 2:如果它仍然不显示图像,请尝试“../test.bmp”,因为当您从 Eclipse 运行小程序时,它具有
bin
目录作为代码库。编辑3:如果将 test.bmp 放入 jar 或类路径中,则可以使用相同的方式加载它,但替换
为
Referencing your image through an absolute local file path might not work when you run your applet from a server.
Use the ImageIcon (URL location) constructor andHave the URL point to the image resource on the server. Use the JApplet.getCodeBase() to determine where your applet originates and append the file name to it.Edit: ImageIO supports BMP and the changed sample works for me.
Edit 2: If it still doesn't display the image, try "../test.bmp" because when you run an applet from lets say Eclipse it has the
bin
directory as the codebase.Edit 3: If you put your test.bmp into the jar or on the classpath, you can load it using the same way but replacing
with
首先,将 \ 正确转义为 \\ 可能是个好主意。
编辑添加:您可能想学习您的语言(或库)的 Path.combine (或 File.join)等效项,这是一种获取文件路径部分列表并将它们与适合平台的路径分隔符组合的方法。 或者您可以用 Java 自己快速编写它,因为路径分隔符记录在 文件.path分隔符。
(我突然意识到,我不知道正斜杠在 ImageIcon 中始终有效,但请注意表明这一点的其他响应)。
此外,请确保您加载的是受支持的文件类型,例如 .png、.gif 或 .jpg。 JDK 1.5 可能支持 BMP
另外,如果您在 Applet 上下文中运行,则由于沙箱规则,您可能无法访问相关路径。 在这种情况下,请使其在与托管小程序的 HTML 文件相同的路径中可用(如果没记错的话,可能在 Jar 的路径中),并使用相对路径。
To start with, it's probably a good idea to correctly escape your \ as \\.
Edited to add: You probably want to learn your language's (or library's) equivalent of Path.combine (or File.join) which is a method for taking a list of file path parts and combining them with the platform-appropriate path separator. Or you can write it yourself quickly in Java, as the path separator is documented in File.pathSeparator.
(Off the top of my head, I didn't know that forward slash always works in ImageIcon, but note the other response that indicates this).
Additionally, make sure you are loading a supported file type, such as .png, .gif or .jpg. BMP may be supported in JDK 1.5
Also, if you are running in an Applet context, you may not have access to the path in question due to sandboxing rules. In that case, make it available in the same path as to the HTML file hosting the applet (possibly in the Jar's path, if memory serves me), and use a relative path.
不要制作一个被告知将自身绘制到图形上的 ImageIcon,而是尝试以下方式:
此外,您可能想尝试使用 .png 而不是 .bmp 文件。
Instead of making an ImageIcon that is told to draw itself onto a graphics, try it this way:
Also, you may want to try using .png instead of a .bmp file.
ImageJ 是一个开源应用程序/库,支持包括 BMP 在内的多种格式。
以下是使用 ImageJ 中的 BMPDecoder 的一些真实代码:
这是许可声明。
ImageJ is an Open Source application / library that has support for may formats including BMP.
Here is some real code using BMPDecoder from ImageJ:
Here is the license statement.
来自 Java 文档:
From the Java Docs:
java 不支持 bmp 文件。
您可能应该考虑取消其他格式,例如 .png、.gif 或 .jpg
如果您确实必须这样做,这里有一篇来自 Java World 的文章,解释了如何读取位图文件并解释它。
Java 技巧 43:如何用 Java 读取 8 位和 24 位 Microsoft Windows 位图来自 JavaWorld 的应用程序
我从未尝试过
bmp files are not supprted in java.
You should probably consider unsing other formats such as .png, .gif or .jpg
If you really HAVE to do it, here's an article from Java World that explains how to read the bit map file and interpret it.
Java Tip 43: How to read 8- and 24-bit Microsoft Windows bitmaps in Java applications from JavaWorld
I've never tried though