An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web-page and runs in the context of the browser. The best support isn’t a browser, but the standalone program appletviewer. In general you want to write applets that can be run with any browser. An applet is a Panel that allows interaction with a Java program. All applet classes must be declared as public. Applets do not begin execution at main( ) method, in fact most applets don’t have a main( ) method. Applet is compiled in the same way as a normal application. To execute an applet in a web browser, you need to write a HTML file that contains the appropriate applet tag

<applet code=AppletWorld.class width="200" height="200"> </applet>

Hello World Applet Example

Html File:

<Title>Java Example</Title>
<Applet Code="MyApplet.class" width=200 Height=100>

Java File:

import javax.swing.JApplet;
import java.awt.Graphics;
public class HelloWorld extends JApplet{
public void paint(Graphics g) {
		g.drawString("Hello world!", 5, 15);

Life Cycle of an Applet

  • Init(): This method is intended for whatever initialization is needed for your applet.
  • Start(): This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
  • Stop(): This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
  • Destroy(): This method is only called when the browser shuts down normally. Thus, the applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once.

public void paint(Graphics g) almost always needed. Any painting you want to do should be done here, or in a method you call from here. Painting that you do in other methods may or may not happen. Don’t call this method. It’s called automatically. Call repaint( ) instead. Applets inherit the paint method from the Abstract Window Toolkit (AWT) Container class.

JApplet is like a JFrame. It already has a panel. You can access its panel with JApplet.getContentPane( ).

Example 1:

import javax.swing.*;
class hello extends JApplet {
	public void init(){
		JButton b = new JButton(“press me”);

Example 2:

import java.awt.*;
import java.applet.*;
public class DrawExample extends Applet  {
	// The colors you will use
Color redColor; Color weirdColor; Color bgColor;
	public void init() {
	redColor =;
		weirdColor = new Color(60,60,122);
		bgColor =;
 public void stop()
public void paint(Graphics g)
g.drawString("Shapes and Colors",80,20);

	// change colors again

	// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
	g.drawLine(140,140,160,160); // Draw a line (int x1, int y1, int x2, int y2)

Example 3:

import java.awt.*;
import java.applet.*;
public class myd extends Applet
    private Image img;

	public void init()       {
         img = getImage(getCodeBase(),"img.gif");
     public void paint(Graphics g)       {

Java Application versus Java Applet

Click here to read about difference between Java Application and Applet

Tagged with: JAVAJAVA GUIProgramming

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.