Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting Images To Load On Web?

807588Jul 1 2009 — edited Jul 2 2009
Hi all,

I have a question about how to get images to load in an applet once it is uploaded to a website. When I run the applet in JGrasp it works fine but if I try to open the applet in a web browser (via an html file on the hard drive) nothing loads. If I take the code out that sets the background image the application loads fine in the web browser.

The image is in the same directory as the class file and html file. Is there a way I can get this method to work (loading off the hard drive and also off the website when it's uploaded) or do I have to use a jar file? I have spent some time searching for tutorials and demos but haven't been able to get the jar file to work either. I'm fairly new to programming and most of the examples I found were way over my head.

Any help in getting this problem solved will be greatly appreciated.

Here is a short sample program to illustrate the problem: For some reason this sample program only loads in IE and not in Firefox.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test extends JApplet implements ActionListener
{ Container mainWindow;

public void init()
{ ImagePanel panel = new ImagePanel(new ImageIcon("Background.jpg").getImage());
setContentPane(panel);

mainWindow = getContentPane();
mainWindow.setLayout(new BorderLayout());

JLabel label = new JLabel("TESTING");
mainWindow.add(label, BorderLayout.CENTER);

setSize(1000, 500);
setVisible(true);
mainWindow.validate();
}

public void actionPerformed(ActionEvent e) { }
}

class ImagePanel extends JPanel
{ private Image img;

public ImagePanel(String img)
{ this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img)
{ this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}

public void paintComponent(Graphics g)
{ g.drawImage(img, 0, 0, null);
}
}


CODE FOR HTML FILE:

<html><body>
<CENTER>
<applet code="Test.class" width="1000" height="500"></applet>
</CENTER>=
</body></html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2009
Added on Jul 1 2009
22 comments
192 views