如何在Applet中显示图像?
我有一个图像,我想在小程序中显示它,问题是图像不会显示。我的代码有问题吗?
谢谢...
这是我的代码:
import java.applet.Applet;
import java.awt.*;
public class LastAirBender extends Applet
{
Image aang;
public void init()
{
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
public void paint(Graphics g)
{
g.drawImage(aang, 100, 100, this);
}
}
I have an image and I want to display it in the applet, The problem is the image wont display. Is there something wrong with my code?
Thanks...
Here's my code :
import java.applet.Applet;
import java.awt.*;
public class LastAirBender extends Applet
{
Image aang;
public void init()
{
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
public void paint(Graphics g)
{
g.drawImage(aang, 100, 100, this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑你做错了什么,这应该很简单:
HTML/applet 元素的内容是什么?图片的名称是什么?图像与 HTML 位于同一目录中吗?
更新 1
第二行(已更改)代码将尝试加载与 HTML 位于同一目录中的
images.jpg
文件。当然,您可能需要添加一个 MediaTracker 来跟踪图像的加载,因为 Applet.getImage() 方法立即返回(现在),但加载是异步的(之后)。
更新 2
尝试这个精确的实验:
将此源另存为
${path.to.current.code.and.image}/FirstAirBender.java
。然后转到提示符并编译代码,然后使用源名称作为参数调用小程序查看器。
您应该在小程序中看到您的图像,从左上角开始以 100x100 的尺寸绘制。
I suspect you are doing something wrong, and that should be just plain:
What is the content of HTML/applet element? What is the name of the image? Is the image in the same directory as the HTML?
Update 1
The 2nd (changed) line of code will try to load the
images.jpg
file in the same directory as the HTML.Of course, you might need to add a
MediaTracker
to track the loading of the image, since theApplet.getImage()
method returns immediately (now), but loads asynchronously (later).Update 2
Try this exact experiment:
Save this source as
${path.to.current.code.and.image}/FirstAirBender.java
.Then go to the prompt and compile the code then call applet viewer using the source name as argument.
You should see your image in the applet, painted at 100x100 from the top-left.
1) 我们生活在 21 世纪,那么请使用 JApplet 而不是 Applet
2) for Icon/ImageIcon 最好寻找 JLabel
3)请问什么是
getImage(getDocumentBase(), getParameter("images.jpg"));
我将等待类似的内容
1) we living .. in 21century, then please JApplet instead of Applet
2) for Icon/ImageIcon would be better to look for JLabel
3) please what's
getImage(getDocumentBase(), getParameter("images.jpg"));
there I'll be awaiting something like as
嗯,上面的答案是正确的。这是我用来显示图像的代码。希望它有帮助:
在上面的代码中,我们创建一个图像类对象并从代码库指定的位置获取图像。然后使用drawImage方法绘制图像。有兴趣了解 getCodeBase() 和 getDocumentBase() 方法的价值的人可以在 Paint 方法中添加以下代码。它们实际上是项目文件夹中 src 文件夹的位置:-
还有一点需要注意:- 确保 src 文件夹中的图像和类没有相同的名称。这导致我的图像无法显示。
Well , above answers are correct. This is the code I used to display image. Hope it helps:
In above code, we create an image class object and get image from location specified by codebase. Then plot the image using drawImage method. Those who are interested in knowing value of getCodeBase() and getDocumentBase() methods can add following code in paint method. They are actually location of src folder in your project folder:-
One more point to note:- Make sure images and classes don't have same name in src folder. This was preventing my image to be displayed.