View Full Version : How is a JApplet uploaded to a web browser

01-23-2008, 01:04 PM
Hi All,

I'm new to Java and have been struggling to upload my JApplet to a webpage HTML document. It seems that I am missing some code in my HTML document because when I test my JApplet with eclipse appletviewer, it works 100%

Please have a look at my Applet code and HTML code below. Any help will be appreciated.

Best Regards

Java File

package applet;

import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;

* @author Sibusiso
* This class represents a JApplet
* that displays a picture and an
* error message if the picture is
* not loaded properly.
public class MyApplet extends JApplet
* This method initiates the
* JApplet
public void init()

* inner class to override JPanel
* paintComponent() to paint an
* image
JPanel panel = new JPanel()

public void paintComponent(Graphics g)
Image picture = getImage();

if(picture == null)
g.drawString("Image File " +
"could not be found",50,50);
}//end paintComponent()

};// end inner class


}//end init()

* Reads the image from a file and displays
* an error message if image file is not
* found
* @return an Image object if the image file
* is found. A null value otherwise
private Image getImage()
Image img = null;

img = ImageIO.read(new File("img1.jpeg"));
catch(IOException e)
System.out.println("\nImage not found");

return img;

}// end getImage()

}//end class

HTML code


<title> MyApplet </title>

<p class = normal align = "center">
<applet code = "MyApplet.class" width = 300 height = 300>


02-01-2008, 11:34 AM
u have to use "codebase" attribute in applet tag in .html file, in that codebase attribute u have to give the full path of that applet.