Java Applet Basic
17-08-2017    862 times
Java Applet

Lifecycle of Java Applet

In the Java Applet, all the methods are called sequentially. The sequences of the methods are

  • init() : This method called ones in the applet life cycle. When an applet run then this method initialized required variables.
  • start() : This method automatically invoked after init(). In this method start all the required services and it calls the paint methods automatically.
  • paint() : This method receives a Graphics object as paint all the data in applet form.
  • stop(): This method used to stop all the running services of the Applet.
  • destroy() : This method destroys all the memory space which is allocated by the applet.

This is called life cycle of the applet.

Tutorial at home

Basic structure Of An Applet Program

import java.awt.*;
import java.applet.*;

/*<applet code=" BasicApplet " width=300 height=100></applet>*/

public class BasicApplet extends Applet
{
public void init() {}
public void start() {}
public void paint(Graphics g) {}
public void stop() {}
public void destroy() {}
}

All the function is mandatory to run an Applet but only required will be overridden by the user.

Basic program

import java.applet.*;
import java.awt.*;

/* <applet width=400 height=200 code="AppletOne.class"></applet> */

public class AppletOne extends Applet

{

public void paint(Graphics g)

{

g.drawString("Tutorial At Home",100,100);

}

}

Tutorial at home

To Run this program

  • Compile the program by javac AppletOne.java
  • Run program appletviewer AppletOne.java

Video Tutorials

Every program of applet should inherit from the super class Applet. We can also show the status bar in the window by using the method showStatus(). To runtime drawing in the applet window we must call the redraw() methods. To draw different shape in applet window we can also use the following methods.

Tutorial at home
Tutorial at home

drawLine()

Draw a line from first coordinate to last coordinate.

drawRect()

Draw a rectangle by left top coordinate and given width and height.

fillRect()

Draw a rectangle by left top coordinate and given width and height. And fill it by a specified color.

drawRoundRect()

Draw a rounded rectangle by left top coordinate and given width and height.

fillRoundRect()

Draw a rounded rectangle by left top coordinate and given width and height. And fill it by a specified color.

drawOval()

Draw a circle or Oval by left top coordinate and given width and height.

fillOval()

Draw a circle or Oval by left top coordinate and given width and height. And fill it by a specified color.

drawPolygon()

Draw a polygon by a collection of coordinate.

setForeground( )

Set the foreground color.

setBackground( )

Set the background color.

softetechnologies
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post