Home Java Tutorial Applet in Java

Applet in Java

by anupmaurya
4 minutes read

In this tutorial, you’ll learn about Applet in Java, Advantages of Applets, Lifecycle of Java Applet, Graphics in Applet along with examples and more.

Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Advantages of Applets

  1. It takes very less response time as it works on the client side.
  2. It can be run on any browser which has JVM running in it.

Lifecycle of Java Applet

Following are the stages in Applet

  1. Applet is initialized.
  2. Applet is started
  3. Applet is painted.
  4. Applet is stopped.
  5. Applet is destroyed.

A Simple Applet

import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
  public void paint(Graphics g)
    {
      g.drawString("A simple Applet", 40, 40);
    }
}

Every Applet application must import two packages – java.awt and java.applet.

java.awt.* imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT. The AWT contains support for a window-based, graphical user interface. java.applet.* imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet class.

Applet class

Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips.

An Applet Skeleton

import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet
{
 public void init()
 {
  //initialization
 }
 public void start ()
 {
  //start or resume execution
 }
 public void stop()
 {
  //suspend execution
 {
 public void destroy()
 {
  //perform shutdown activity
 }
 public void paint (Graphics g)
 {
  //display the content of window
 }
}

Most applets override these four methods. These four methods forms Applet lifecycle.

  1. public void init(): It is also the first method to be called. This is where variable are initialized. It is invoked only once.
  2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
  3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
  4. public void destroy(): is used to destroy the Applet. It is invoked only once.It is called when your applet needs to be removed completely from memory.

Note: The stop() method is always called before destroy() method.

Graphics in Applet

In Applet, java.awt.Graphicsclass provides methods for using graphics.

Below are the Methods of the Graphics class.

Sr No.MethodsDescription
1public abstract void drawString(String str, int x, int y)Used to draw specified string.
2public void drawRect(int x, int y, int width, int height)Used to draw a rectangle of specified width and height.
3public abstract void fillRect(int x, int y, int width, int height)Used to draw a rectangle with a default colourof specified width and height.
4public abstract void drawOval(int x, int y, int width, int height)Used to draw oval of specified width and height.
5public abstract void fillOval(int x, int y, int width, int height)Used to draw oval with a default colour of specified width and height.
6public abstract void drawLine(int x1, int y1, int x2, int y2)Used for drawing lines between the point (x1, x1) and (x2, y2).
7public abstract booleandrawImage(Image img, int x, int y, ImageObserver observer)Used for drawing a specified image.
8public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle)Used for drawing a circular arc.
9public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle)Used for filling circular arc.
10public abstract void setColor(Color c)Used to set a colour to the object.
11public abstract void setFont(Font font)Used to set font.

Example

  
import java.applet.Applet;  
import java.awt.*;  
public class GraphicsDemo1 extends Applet
{    
  public void paint(Graphics g)
  {  
    g.setColor(Color.black);  
    g.drawString("Welcome to studytonight",50, 50); 
    g.setColor(Color.blue);  
    g.fillOval(170,200,30,30);  
    g.drawArc(90,150,30,30,30,270);  
    g.fillArc(270,150,30,30,0,180);  
    g.drawLine(21,31,20,300);  
    g.drawRect(70,100,30,30);  
    g.fillRect(170,100,30,30);  
    g.drawOval(70,200,30,30);  
  }  
}
  

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.