简体   繁体   English

Applet中的图像未显示在网页中

[英]Images in Applet not showing in web page

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. 我正在尝试在基于Web的应用程序上使用的Java applet上显示JPEG图像和移动点。 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. 但是,当我运行该applet时,它运行良好,但是当我从JSP页面显示applet时,却得到了移动点,但没有JPEG图像。

Is there a specific folder where the JPEG needs to be? 是否需要JPEG的特定文件夹?

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 下面的代码一是来自jsp页面的调用

<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. jar文件和图片与jsp页面位于同一文件夹中,并且在应用程序的Web部分中还有一个包含类内容和applet图像的文件夹。 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 谢谢

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. 请检查您的路径..或解决该问题,请将您的文件放在文件夹中并与JAR文件一起打包。并在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. 像:让小程序名称是demoapp包中的myapp.class,然后将文件放在demoapp的dir“ images”目录中,并在所有Java代码中提供相对于该路径的路径。 dont forget to include image files in jar build. 不要忘记在jar构建中包括图像文件。

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". 我建议创建一个名为images的文件夹,并在其中放入所有图像,然后将“ picture.jpg”更改为“ \\ images \\ picture.jpg”。 Check your website directory to see if the image is in the same folder as the source code. 检查您的网站目录,以查看图像是否与源代码位于同一文件夹中。

Some comments that might make it more clear for you. 一些评论可能会让您更清楚。

//getDocumentbase gets the applet path.

No. getDocumentBase() provides the path to the web page. 不能getDocumentBase()提供网页的路径 But that is neither here, nor there, since this applet actually calls.. 但这既不是这里也不是那里,因为此小程序实际上是在调用。

base = getCodeBase();

..which provides the codebase. ..提供代码库。 The codebase defaults to the directory of the web page, unless a codebase parameter is specified in the applet element. 除非在applet元素中指定了codebase参数,否则codebase默认为网页目录。 Since the applet element declares no codebase , the base will be the same URL as the document base. 由于applet元素未声明任何codebase ,因此该base将与文档库具有相同的URL。


BTW: In the applet element BTW:在applet元素中

code="myapplets/mapplet.class"

..should be.. ..应该..

code="myapplets.mapplet"

And since common nomenclature for Java class names is EachWordUpperCase , the class name should be changed. 并且由于Java类名称的通用命名法是EachWordUpperCase ,因此应更改类名称。

Doe the applet declare? 小程序声明了吗?

package myapplets;

BTW (2): 顺便说一句(2):

there is also a folder containing the contents of the class and image of the applet in the web section of the application. 在应用程序的Web部分中还有一个包含类内容和小程序图像的文件夹。

What does that mean? 这意味着什么? Please provide complete paths from the root of the server to all resources (eg the HTML/JSP, Jar file & image) used. 请提供从服务器根目录到使用的所有资源(例如HTML / JSP,Jar文件和图像)的完整路径。

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

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