Unlike desktop or stand-alone application, applet does not require the main method. The execution of the applet does not begin at the main method. Rather, Applet class provides different methods serving as the basic mechanism of executing an applet application on a web browser or applet viewer. These methods are init(), start(), paint(), stop(), and destroy(). When an applet begins, the init() methods is called. You can use this method to initialize variables. The init() method is called once. The start() method is called after the init() method. This method is called every time the applet's HTML page displays on the web browser. When the user leaves the HTML page that contains applet, the applet stops by calling the stop() method. When he/she comes back to that page, the start() method will be called again to resume the applet. The destroy() method will be called after the stop method to free up resources used by applet from memory. The paint method is called each time the applet needs to redraw its output. For example, the applet draw its output when it starts and it is minimized and restored.
Create an applet
To create an applet application, you will extend the Applet class in java.applet package. The following example code is a simple applet application to display an image and a button that can be clicked to change the background color of the applet's window. You need an image file called gmaptr.png.import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
/*
<applet code="AppletApp" width=600 height=300>
</applet>
*/
public class AppletApp extends Applet implements ActionListener{
public void init(){
setSize(600,300);
setBackground(Color.LIGHT_GRAY);
Button bt=new Button("Change Color");
bt.setBackground(Color.PINK);
bt.addActionListener(this);
add(bt);
}
public void paint(Graphics g){
try {
MediaTracker mt=new MediaTracker(this);
URL imgurl=new URL(getCodeBase().toString()+"gmaptr.png");
Image img=getImage(imgurl);
mt.addImage(img,0);
mt.waitForID(0);
g.drawImage(img,10, 10,this);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
showStatus("There are one button and one image on the applet window.");
}
public void actionPerformed(ActionEvent e){
Color c=getBackground();
if(c==Color.BLACK)
setBackground(Color.BLUE);
else
setBackground(Color.BLACK);
}
}
You need to create AppletApp.class file from the source code above. This can be done by compiling the source code with the javac command from command prompt.
Embed the applet in HTML file
In order to execute the applet on a web browser, you need to create an HTML file that includes the applet. You can include the applet within the HTML document by using the <applet> tag. Below is an example HTML file.<!DOCTYPE html>
<html>
<head> <title>Applet Test</title>
</head>
<body>
<applet code="AppletApp" width=600 height=300>
</applet>
</body>
</html>
After you create the HTML file that contains applet, you can execute the applet by using the appletviewer tool that comes with JDK. The command line below will execute the AppletApp application in the appletviewer from command prompt.
D:\sitedemo\AppletApp\AppletTest\bin>appletviewer AppletApp.html
If you are Eclipse user, you can execute the applet in the appletviewer from Eclipse by clicking the Run menu item.
Besides running the applet from command prompt and Eclipse, you can run the applet in a web browser that supports Java. You need to copy the AppleAppt.class, AppletApp.html, and other related files to a directory of your web server. Then run the HTML file as you do with other HTML, php, or jsp pages. In my machine i have Chrome browser and WampServer installed.