Applet 中的图像未显示在网页中
我正在尝试在基于 Web 的应用程序中使用的 Java 小程序上显示 JPEG 图像和移动点。但是,当我运行小程序时,它工作正常,但是当我从 JSP 页面显示小程序时,我得到了移动点,但没有得到 JPEG 图像。
JPEG 需要存放在特定的文件夹中吗?
这是我用来在屏幕上绘制图片和移动点的两种方法。
public class mapplet extends Applet implements Runnable {
int x_pos = 10;
int y_pos = 100;
int radius = 20;
Image img, img2;
Graphics gr;
URL base;
MediaTracker m;
@Override
public void init() {
mt = new MediaTracker(this);
try {
//getDocumentbase gets the applet path.
base = getCodeBase();
img = getImage(base, "picture.jpg");
m.addImage(img, 1);
m.waitForAll();
} catch (InterruptedException ex) {
Logger.getLogger(movement.class.getName()).log(Level.SEVERE, null, ex);
}
public void paint (Graphics g) {
g.drawImage(img, 0, 0, this);
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
下面代码一是从jsp页面调用
<applet archive="mapplet.jar" code="myapplets/mapplet.class" width=350 height=200>
</applet>
jar文件和图片与jsp页面在同一个文件夹下,在web部分还有一个文件夹,里面有小程序的class和image的内容应用。小程序加载正常,但图片不显示。我认为造成问题的不是代码,而是图片的位置。
谢谢
I am trying to display a JPEG image and a moving dot on a Java applet which I am using on a web based application. However, when I run the applet it works fine, but when I display the applet from the JSP page, I get the moving dot but not the JPEG image.
Is there a specific folder where the JPEG needs to be?
These are the 2 methods i use for drawing the picture and the moving dot on the screen.
public class mapplet extends Applet implements Runnable {
int x_pos = 10;
int y_pos = 100;
int radius = 20;
Image img, img2;
Graphics gr;
URL base;
MediaTracker m;
@Override
public void init() {
mt = new MediaTracker(this);
try {
//getDocumentbase gets the applet path.
base = getCodeBase();
img = getImage(base, "picture.jpg");
m.addImage(img, 1);
m.waitForAll();
} catch (InterruptedException ex) {
Logger.getLogger(movement.class.getName()).log(Level.SEVERE, null, ex);
}
public void paint (Graphics g) {
g.drawImage(img, 0, 0, this);
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
The code one below is the call from the jsp page
<applet archive="mapplet.jar" code="myapplets/mapplet.class" width=350 height=200>
</applet>
The jar file and the picture are in the same folder as the jsp page, and there is also a folder containing the contents of the class and image of the applet in the web section of the application. The applet loads fine however the picture doesn't display. I think it's not the code but the location of the picture that is causing a problem.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,图像应该与源代码位于同一文件夹中。我建议创建一个名为 images 的文件夹,将所有图像放入其中,然后将“picture.jpg”更改为“\images\picture.jpg”。检查您的网站目录,查看图像是否与源代码位于同一文件夹中。
Yes, the image should be in the same folder as the source code. I would recommend to do a folder called images and inside it put all your images and just change "picture.jpg" to "\images\picture.jpg". Check your website directory to see if the image is in the same folder as the source code.
一些评论可能会让您更清楚。
不。
getDocumentBase()
提供了网页的路径。但这既不在这里也不在那里,因为这个小程序实际上调用....它提供了代码库。除非在 applet 元素中指定了
codebase
参数,否则代码库默认为网页的目录。由于 applet 元素未声明codebase
,因此base
将与文档库具有相同的 URL。顺便说一句:在 applet 元素中
.. 应该是 ..
并且由于 Java 类名的常见命名法是
EachWordUpperCase
,因此应该更改类名。小程序是否声明?
顺便说一句(2):
这意味着什么?请提供从服务器根目录到所使用的所有资源(例如 HTML/JSP、Jar 文件和图像)的完整路径。
Some comments that might make it more clear for you.
No.
getDocumentBase()
provides the path to the web page. But that is neither here, nor there, since this applet actually calls....which provides the codebase. The codebase defaults to the directory of the web page, unless a
codebase
parameter is specified in the applet element. Since the applet element declares nocodebase
, thebase
will be the same URL as the document base.BTW: In the applet element
..should be..
And since common nomenclature for Java class names is
EachWordUpperCase
, the class name should be changed.Doe the applet declare?
BTW (2):
What does that mean? Please provide complete paths from the root of the server to all resources (e.g. the HTML/JSP, Jar file & image) used.
请检查您的路径..或者要解决该问题,请将您的文件放入一个文件夹中并将其与 JAR 文件一起打包。并提供 jar 中图像文件的相对路径。
喜欢:
让你的小程序名称为 demoapp 包中的 myapp.class
然后将文件放入 demoapp 内的“images”目录中,并在所有 java 代码中提供相对于该路径的路径。
不要忘记在 jar 构建中包含图像文件。
please check your path ..or to resolve it please put your files inside a folder and pack it with JAR file.and provide the relative path of image files inside your jar.
Like:
let your applet name is myapp.class in package demoapp
then put your files in dir "images" inside demoapp and provide path relative to this path in all your java code.
dont forget to include image files in jar build.