简体   繁体   中英

How do I include images on my java applet on a website?

I was able to embed my java applet onto a website using the following HTML code.

<applet name = "Project" code = "Project.class" archive="Project.jar"    
codebase="https://project.googlecode.com/svn/trunk/" width=800 height = 800>
</applet>

I kept my code on the google code database and used the TortoiseSVN to place my jar file and other files into that database including my image files.

However my image files didn't show up. I already signed my jar and all other class files, but I didn't sign my images. I didn't think I needed to. When I open the website, all other components of my program work, except for the images. None of the images show up.

In my appletviewer, the images do show up, and I am using the following code to get and show the image.

 class SwingPanel extends JPanel{
    Image bg;
    SwingPanel(){
        bg = Toolkit.getDefaultToolkit().getImage("spacebg.jpg");
    }
  }
 //In another class
  public void paintComponent(Graphics g){//PaintComponent
            super.paintComponent(g);//Super
            g.drawImage(bg,0,0,800,800,this);
    }

I have another image as well which uses the similar structure as shown above.

How can I get the images to show up on the website?

If the images are inside your .jar file, you can access them using the getResource method of Class or ClassLoader :

bg = Toolkit.getDefaultToolkit().getImage(
    SwingPanel.class.getResource("spacebg.jpg"));

The above code looks for spacebg.jpg in the same package that contains the SwingPanel class.

If you are deploying the image outside of your .jar file, and placing it in the same directory as the .jar file on the web server, you can create a URL relative to the applet's location :

bg = Toolkit.getDefaultToolkit().getImage(
    new URL(applet.getCodeBase(), "spacebg.jpg"));

You must include full web address in a resource location string.

Instead of this:

bg = Toolkit.getDefaultToolkit().getImage("spacebg.jpg");

You must use:

bg = Toolkit.getDefaultToolkit().getImage("http://location-of-image/spacebg.jpg");

Or if it is in the jarfile:

You must use:

bg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("package-of-class/spacebg.jpg").getFile());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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