有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

小程序中的java图像未显示在网页中

我试图在一个Java小程序上显示一个JPEG图像和一个移动的点,我正在基于web的应用程序上使用它。然而,当我运行小程序时,它工作正常,但是当我从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部分还有一个包含类内容和小程序图像的文件夹。小程序加载良好,但图片不显示。我认为造成问题的不是代码而是图片的位置

谢谢


共 (2) 个答案

  1. # 1 楼答案

    一些可能让你更清楚的评论

    //getDocumentbase gets the applet path.
    

    否。getDocumentBase()提供指向网页的路径但这不在这里,也不在那里,因为这个小程序实际上调用

    base = getCodeBase();
    

    。。它提供了代码库。除非在applet元素中指定了codebase参数,否则代码库默认为网页的目录。由于applet元素声明nocodebase,因此base将是与文档库相同的URL


    顺便说一句:在applet元素中

    code="myapplets/mapplet.class"
    

    。。应该是

    code="myapplets.mapplet"
    

    而且,由于Java类名的通用术语是EachWordUpperCase,因此应该更改类名

    小程序声明了吗

    package myapplets;
    

    顺便说一句(2):

    there is also a folder containing the contents of the class and image of the applet in the web section of the application.

    这是什么意思?请提供从服务器根目录到使用的所有资源(例如HTML/JSP、Jar文件和图像)的完整路径

  2. # 2 楼答案

    是的,图像应该和源代码在同一个文件夹中。我建议你创建一个名为images的文件夹,把你所有的图片放在里面,然后把“picture.jpg”改成“\images\picture.jpg”。检查您的网站目录,查看图像是否与源代码位于同一文件夹中