Applet Introduction

GUI Introduction -


GUI Applications or Windows applications can build by using Applet and Frames.

Applet -

Applets are used to create GUI applications. These are java applications that are embedded and executed in web browser over internet.
Applets are automatically installed and run on client machine as part of web pages in which they are embedded.
An example of an applet can be a simple loan calculator that can be embedded with a web page so that any client accessing the web page can calculate their loan.

Frame -

Frame represents a top level windows. In contrast with Applet, Frame windows have border and title, whereas Applet does not have.

Creating Applets -

There are two ways of creating applets -

1- Using AWT (Abstract Window Toolkit) package -

AWT provides support for window based GUI. These types of applets make use of Abstract Window Toolkit package to provide the GUI. Here Applets interact with user by suing different classes provided by AWT package.

2- Using Swing package -

These types of Applets are subclasses of JApplet class defined in Swing package. These applets make use of Swing classes to provide GUI to interact with user. Swing based applets are easier to create and offer more interactive interface than AWT based Applets. So these days Swing based Applets are often used. JApplet inherits the Applet class, so all features of Applet are also available in JApplet.

In these tutorials we will create Applets using AWT, this method of creating Applets has been available since java was first created. And once you are familiar with AWT based Applets, you can easily create Swing based Applets too.

AWT and Applet class Hierarchy -

Applet Life Cycle -

An AWT based Applet has the following life cycle -

Note - repaint() and update() are not the part of Applet life cycle.
When an Applet runs these methods executes in the following sequence -

1- public void init()-

This is the first method to be called when Applet start execution. As the name indicates, this method is used for initialization task (for example -initializing variables). And it runs only once during Applet entire life cycle.

2- public void start()-

This is the second method called during Applet execution. It is used to start Applet. It can run multiple times. It runs every time whenever user visits a web page containing the Applet.

3- public void paint(Graphics g)-

This is the 3rd method to be called and is used to produce Applet output on screen. This method called every time whenever an Applet output to be redrawn on screen. This method has one parameter of type Graphics. Graphics class provides several methods to draw graphical content on Applet.

4- public void stop()-

This is the 4th method to be called. It is used to stop Applet. It is called automatically whenever a user minimizes or moves off the web page containing Applet code. At this stage you can restart an Applet by calling start() method.

5- public void destroy()-

This method is called after stop() method. And is used to completely destroy an Applet from memory ( for example- whenever user close the web browser).

Note- paint() method belongs to java.awt.Component class and remaining 4 methods belong to java.applet.Applet class.

Repaint and update methods-

Creating Applet -

All applets are the subclass of Applet class defined in java.applet package.
There are several methods for creating Applets -

First Method -

Open the Notepad and type the following code-
import java.applet.Applet;  
import java.awt.Graphics;  
public class Test extends Applet
 {  
public void paint(Graphics g){  
g.drawString("This is the first applet", 200, 200);  
  }  
 }

  • The first two lines of code are import statements and are used to import Applet and Graphics classes in our program.
  • Next line declares a class named Test that inherits Applet class. Note that the class is public and in case of Applet it is compulsory to declare a class as public because this class is to be accessed by the code that is outside the program.
  • Next line declare a paint() method that is an overridden method of AWT Component class and is used to draw something on Applet.
  • Inside paint() method a drawstring() method is called by Graphics class reference. It is a method of Graphics class and has the following general form-
        drawstring(String text, int x, int y) 
    
It will draw a text String on Applet screen starting with x, y location. Here x and y indicates the distance from top left corner of Applet window. At top left corner the values of x and y will be 0 and 0.
Save the file by Test.java name and compile it as follows -
c:\> javac Test.java

Again Open a new Notepad and type the following code -

<html>  
<body >  
<applet code="Test.class" width="300" height="300" > 
</applet >  
</body >
</html >

Save it by AppDemo.html.
Now open this file in any web browser and output of applet will be displayed in a 300 by 300 area.
Or in command prompt use the appletviewer tool to run the above file-
c:\> appletviewer AppDemo.html 

Second Method -

Using Applet Viewer we can easily test our Java Applets.
Open the notepad and write the following code -
import java.applet.Applet;  
import java.awt.Graphics;  
public class Test extends Applet
 {  
public void paint(Graphics g){  
g.drawString("This is the first applet", 200, 200);  
  }  
 }
/* 
< applet code="Test.class" width="300" height="300" > 
< /applet > 
*/ 

Save it by Test.java and compile and run as follows-
c:\>javac Test.java
c:\>appletviewer Test.java

Third Method -

The easiest method of creating and testing applets is by using any IDE such as Eclipse, Netbeans, TextPad etc.
For now we are using TextPad Text Editor.
You can download it by clicking on this link
Simply download and install it.It has nearly same GUI as Notepad. SImply open it and write the following code-
import java.applet.Applet;  
import java.awt.Graphics;  
public class Test extends Applet
 {  
public void paint(Graphics g){  
g.drawString("This is the first applet", 200, 200);  
  }  
 }
        
Save it by Test.java.
you can compile the file by pressing ctrl plus 1(ctrl + 1) and run it by pressing ctrl plus 3(ctrl + 3).

Changing Background and Foreground color of Applets -

Component class have two methods for setting the background and foreground colors -
public void setBackground(Color c) 
public void setForeground(Color c) 
Where c specifies the color.
Java has a predefined class named Color that provides us several constant values for colors for example - red, green, blue, pink, yellow etc.
Uppercase versions of these constants are also valid.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   public void init()
   {
        setBackground(Color.red);
        setForeground(Color.GREEN);
   }
    public void paint(Graphics g)
    {
        g.drawString("Hello Welcome to Applet", 100, 100);
    }  
}

Note that It is good practice to set the background and foreground in init() method of Applet. However we can also set them in paint() method too.
Save the file with Test.java. And compile and run it and it will produce the following output-