...

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



Sibusiso
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
Sibusiso


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()
{
setSize(300,300);

/**
* 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);
else
g.drawImage(picture,0,0,300,300,
null);
}//end paintComponent()

};// end inner class

setContentPane(panel);

}//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;

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

return img;

}// end getImage()



}//end class





HTML code



<html>

<head>
<title> MyApplet </title>
</head>

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

</applet>
</p>
</body>
</html>

bhargavkumar_b
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.

cheers
BhargavKumar.B



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum