Understanding Applets: A Java Programming Concept, Lecture notes of Web Design and Development

An overview of applets, their inheritance hierarchy, and the life cycle methods in java. It includes examples of writing and executing applets using html and the appletviewer program. This information is useful for students in computer science courses focusing on java programming.

Typology: Lecture notes

2011/2012

Uploaded on 11/10/2012

taariq
taariq 🇵🇰

4.4

(16)

61 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Web Design & Development CS-506
- 233 -
Lecture 20
Applets
A small program written in Java and included in a HTML page.
It is independent of the operating system on which it runs
An applet is a Panel that allows interaction with a Java program
A applet is typically embedded in a Web page and can be run from a browser
You need special HTML in the Web page to tell the browser about the applet
For security reasons, applets run in a sandbox: they have no access to the client’s file
system
Applets Support
Most modern browsers support Java 1.4 if they have the appropriate plugin
Sun provides an application appletviewer to view applets without using browser.
In general you should try to write applets that can be run with any browser
What an Applet is?
You write an applet by extending the class Applet or JApplet
Applet is just a class like any other; you can even use it in applications if you want
When you write an applet, you are only writing part of a program
The browser supplies the main method
The genealogy of Applet
The following figure shows the inheritance hierarchy of the JApplet class. This
hierarchy determines much of what an applet can do and how, as you'll see on the next
few pages.
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
|
+----javax.swing.JApplet
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Applets: A Java Programming Concept and more Lecture notes Web Design and Development in PDF only on Docsity!

Lecture 20

Applets

ƒ A small program written in Java and included in a HTML page.

ƒ It is independent of the operating system on which it runs

ƒ An applet is a Panel that allows interaction with a Java program

ƒ A applet is typically embedded in a Web page and can be run from a browser

ƒ You need special HTML in the Web page to tell the browser about the applet

ƒ For security reasons, applets run in a sandbox: they have no access to the client’s file

system

Applets Support

ƒ Most modern browsers support Java 1.4 if they have the appropriate plugin

ƒ Sun provides an application appletviewer to view applets without using browser.

ƒ In general you should try to write applets that can be run with any browser

What an Applet is?

ƒ You write an applet by extending the class Applet or JApplet

ƒ Applet is just a class like any other; you can even use it in applications if you want

ƒ When you write an applet, you are only writing part of a program

ƒ The browser supplies the main method

The genealogy of Applet

The following figure shows the inheritance hierarchy of the JApplet class. This

hierarchy determines much of what an applet can do and how, as you'll see on the next

few pages.

java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet

Example Code 20.1: Writing a Simple Applet

Below is the source code for an applet called HelloApplet. This displays a “Hello

World” string. Note that no main method has been provided.

// File HelloApplet.java

//step 1: importing required packages import java.awt.; import javax.swing.;

// extending class from JApplet so that our class also becomes an //applet public class HelloApplet extends JApplet {

// overriding paint method public void paint(Graphics g) {

// write code here u want to display & draw by using // Graphics object g.drawString(“Hello World”, 30 , 30);

} // end class

After defining the HelloApplet.java, the next step is to write .html file. Below is

the source code of Test.html file. The Test.html contains the ordinary html code except

one.

Simple Applet

Applet Life Cycle Methods

When an applet is loaded, an instance of the applet's controlling class (an Applet

subclass) is created. After that an applet passes through some stages or methods, each of

them are build for specific purpose

An applet can react to major events in the following ways:

ƒ It can initialize itself.

ƒ It can start running.

ƒ It can stop running.

ƒ It can perform a final cleanup , in preparation for being unloaded

The applet’s life cycle methods are called in the specific order shown below. Not every

applet needs to override every one of these methods.

Let’s take a look on each method in detail and find out what they do

ƒ init( )

— Is called only once.

— The purpose of init( ) is to initialize the applet each time it's loaded (or reloaded).

— You can think of it as a constructor

init()

start()

stop()

destroy()

paint

ƒ start( )

— To start the applet's execution

— For example, when the applet's loaded or when the user revisits a page that

contains the applet

— start( ) is also called whenever the browser is maximized

ƒ paint( )

— paint( ) is called for the first time when the applet becomes visible

— Whenever applet needs to be repainted, paint( ) is called again

— Do all your painting in paint( ), or in a method that is called from paint( )

ƒ stop( )

— To stop the applet's execution, such as when the user leaves the applet's page or

quits the browser.

— stop( ) is also called whenever the browser is minimized

ƒ destroy( )

— Is called only once.

— To perform a final cleanup in preparation for unloading

The DemoTest.html file is using this applet. The code snippet of it given below:

Applet Life Cycle Methods

Example Code 20.3: Animated Java Word

Sample Output

The browser output of the program is given below:

Design Process

ƒ The Program in a single call of paint method

— Draws string “java” on 40 random locations

— For every drawing, it selects random font out of 4 different fonts

— For every drawing, it selects random color out of 256 * 256 * 256 RGB colors

ƒ Repaint is called after every 1000 ms.

ƒ After 10 calls to repaint, screen is cleared

ƒ chooseColor( )

This method will choose color randomly out of 256 * 256 * 256 possible colors. The

code snippet is given below:

// method chooseColor

public Color chooseColor() {

// choosing red color value randomly int r = (int) (Math.random() * 255);

// choosing green color value randomly int g = (int) (Math.random() * 255);

// choosing blue color value randomly int b = (int) (Math.random() * 255);

// constructing a color by providing R-G-B values Color c = new Color(r, g, b);

// returning color return c; }

ƒ chooseFont( )

This method will choose a Font for text (java) to be displayed out of 4 available fonts.

The code snippet is given below:

// method chooseFont

public Font chooseFont() {

// generating a random value that helps in choosing a font int fontChoice = (int) (Math.random() * 4) + 1;

// declaring font reference Font f = null;

// using switch based logic for selecting font switch (fontChoice) {

case 1: f = new Font("Serif", Font.BOLD + Font.ITALIC, 20); break;

case 2: f = new Font("SansSerif", Font.PLAIN, 17); break;

case 3: f = new Font("Monospaced", Font.ITALIC, 23); break;

case 4: f = new Font("Dialog", Font.ITALIC, 30); break;

} // end switch

// returns Font object return f;

} //end chooseFont

ƒ paint( )

The last method to be discussed here is paint( ). By overriding this method, we

will print string “java” on 40 random locations. For every drawing, it selects random

font out of 4 different fonts & random color out of 256 * 256 * 256 RGB colors.

Let’s see, how it happens:

// overriding method paint public void paint(Graphics g) {

// incrementing clear counter variable. clearCounter++;

// printing 40 “java” strings on different locations by // selcting random font & color for (int i = 1; i <= 40; i++) {

// choosing random color by calling chooseColor() method Color c = chooseColor();

// setting color g2.setColor(c);

// choosing random Font by calling chooseColor() method Font f = chooseFont(); g2.setFont(f);

// drawing string “java” by calling drawJava() method drawJava(g2);

} // end for loop

// overriding paint method – discussed above public void paint(Graphics g) {

clearCounter++;

Graphics2D g2 = (Graphics2D) g;

if (clearCounter == 10) { g2.clearRect(0, 0, 1000, 700); clearCounter = 0; }

for (int i = 1; i <= 40; i++) {

Color c = chooseColor(); g2.setColor(c);

Font f = chooseFont(); g2.setFont(f);

drawJava(g2); } }

// overriding actionPerformed()of ActionListener interface // called by Timer object public void actionPerformed(ActionEvent ae) {

repaint();

// chooseColor method – discussed above public Color chooseColor() {

int r = (int) (Math.random() * 255); int g = (int) (Math.random() * 255); int b = (int) (Math.random() * 255);

Color c = new Color(r, g, b); return c;

}

// chooseFont method – discussed above public Font chooseFont() {

int fontChoice = (int) (Math.random() * 4) + 1;

Font f = null;

switch (fontChoice) {

case 1: f = new Font("Serif", Font.BOLD + Font.ITALIC, 20); break;

case 2: f = new Font("SansSerif", Font.PLAIN, 17); break;

case 3: f = new Font("Monospaced", Font.ITALIC, 23); break;

case 4: f = new Font("Dialog", Font.ITALIC, 30); break;

return f; }

// drawJava() method – discussed above public void drawJava(Graphics2D g2) {

int x = (int) (Math.random() * 1000); int y = (int) (Math.random() * 700);

g2.drawString("java", x, y);

}

} // end class