You are on page 1of 26

1

METTU UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
Java programming Course
Chapter-2
Java Applet

By: Naol G.
1
What is an Applet?
• There are two types of java program:
– Java Application
– Java Applet
• Java application is a standalone program that can be executed using java interpreter.
• An applet is a java program that is intended not to be run on its own,
– but rather to be embedded inside another application (into a html document and executed
on web browser.)
• It runs inside the web browser and works at client side.
• An applet is embedded in an HTML page using the APPLET.
• Applets are used to make the web site more dynamic and entertaining

2
Java Applet vs. Application

Applications Applets
Applications have the main( ) method Applets don’t have the main ( ) method
Java application does not require an external viewer program. Java applets require external viewer programs, so to use an
This means that you can execute a java application directly applet; you need web browser or an applet viewer.
using the java interpreter.
Application can access the local file system and resources. Applet cannot access the local file system and resources
Java application generally refers to an application that is Applets are designed to be embedded within an HTML page.
designed stand alone use. (i.e.) designed for use on the world wide web (www)

3
Types of Applet
• There are two types of the applet -
– Applets based on the AWT(Abstract Window Toolkit) package by extending
its Applet class.
– Applets based on the Swing package by extending its JApplet class

4
Applet class
• The Applet class must be the superclass of any applet that is
– To be embedded in a Web page or
– To be viewed by the Java Applet Viewer.
• Applet class provides several useful methods to give you full control over the execution
of an applet.
• Applet class is a part of java.applet package.
– Note: This if we want to use awt based applet types.
• Two packages are required to be imported:
– import java.awt.*; //provides windows and GUI components
– import java.applet.Applet; // provides several useful methods

5
Life cycle of an Applet
• When an applet begins, the following methods are called, in this
sequence:
 init( )
 start( )
 paint( )

• When an applet is terminated, the following sequence of method calls


takes place:
 stop( )
 destroy( )

6
Life cycle...
• init( ) :
– The init( ) method is the first method to be called.
– This is where you should initialize variables.
– This method is called only once during the run time of your applet.
• start( ) :
– The start( ) method is called after init( ).
– It is also called to restart an applet after it has been stopped
– The start( ) is called each time an applet’s HTML document is displayed onscreen.
– So, if a user leaves a web page and comes back, the applet resumes execution
at start( ).

7
Life cycle...
• paint( ) :
– The paint( ) method is called each time an applet’s output must be redrawn.
– This situation can occur for several reasons.
• For example, the window in which the applet is running may be overwritten by another window and then
uncovered.
• Or the applet window may be minimized and then restored.
– paint( ) is also called when the applet begins execution.
– Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
– The paint( ) method has one parameter of type Graphics.
• It’s prototype is:
– public void paint(Graphics g)
• where g is an object reference of class Graphic.

8
Life cycle...
• stop( ) :
– The stop( ) method is called when a web browser leaves the HTML document
containing the applet—when it goes to another page.
– When stop( ) is called, the applet is probably running.
– You should use stop( ) to suspend threads that don’t need to run when the
applet is not visible.
– You can restart them when start( ) is called if the user returns to the page.

9
Life cycle...
• destroy() :
– The destroy() method is called when the environment determines that your
applet needs to be removed completely from memory.
– This will free up any resources the applet may be using.
– The stop() method is always called before destroy().

10
How to run an applet?
• There are two standard ways in which you can run an applet :
– Using a Java-compatible web browser.
– Using the standard tool, applet-viewer.
• An applet viewer executes your applet in a window.
• This is generally the fastest and easiest way to test your applet.

11
How to run…
• Using java enabled web browser :
– To execute an applet in a web browser we have to write a short HTML text file that contains
a tag that loads the applet. We can use APPLET or OBJECT tag for this purpose.
– Using APPLET, here is the HTML file that executes AppletDemo:

• The code, width and height are required applet tag’s attributes.
– The code will take .class name as value
– The width and height specify the dimensions of the display area used by the applet.
• After you create this html file, you can use web browser to execute the applet.

12
How to run…
• Using appletviewer :
– This is the easiest way to run an applet.
– To execute AppletDemo with an applet viewer, you may also execute the HTML file shown
earlier.
• For example, if the preceding HTML file is saved with
MyApplet.html, then the following command line will run AppletDemo:

13
Simple Applet program Example: Demo
Applet menu bar
AppletDemo.java Output

Body/object displaying area


Applet status bar 14
Life cycle of an Applet: Demo

15
Output of above program
When an applet begins

When an applet window minimized:

16
Output…
When an applet window re-maximized and repainted:

When an applet window terminated

17
Graphics class in Applet
• The Graphics class is the abstract base class for all graphics contexts that allow an
application to draw onto components that are realized on applets.
– It’s needed to be imported as java.awt.Graphics;
• Graphics methods:
– Some commonly used methods in the Graphics class are:
• abstract void drawString(String strng, int a, int b);
– draws the specified string.
• abstract void drawLine(int a1, int b1, int a2, int b2);
– draws a line.
• void drawRect(int a, int b, int wdth, int hgt);
– draws a rectangle with the specified width and height.

18
Graphics methods…
– abstract void fillRect(int a, int b, int wdth, int hgt);
• fills a rectangle with the default color and specified width and height.
– abstract void drawOval(int a, int b, int wdth, int hgt);
• draws an oval.
– abstract void fillOval(int a, int b, int wdth, int hgt)
• fills an oval with the default color.
– abstract void setColor(Color clr);
• sets the Graphics current color to the specified color.
– abstract void drawArc(int a, int b, int wdth, int hgt, int startAngle, intarcAngle);
• draws a circular or elliptical arc.

19
Examples: drawString()
Source code: AppletDemo.java
Output:

Compile and Execute:

• String drawn at: X = 100, Y = 100


• X increases from left-to-right
• Y increase s from top-to-bottom

20
Examples: drawLine()
Source code: AppletDemo.java

Output:

21
Examples: drawRect() and fillRect()
Source code: AppletDemo.java

Output:
• If width == height,
it will be square.

• Arrow shows code line and its


output

22
Examples: drawOval() and fillOval()

• If width == height,
it will be Circle.

• Arrow shows code line and its


output

23
Examples: drawImage()

Note:
• getCodeBase() used to find location of file
• getImage() method used since Image is Interface(can’t be instantiated)
• this keyword show current class in which image loaded.
24
Lab exercises (will have individual evaluation: 5%)
• Write java Applet that:
1. Change the background color of applet’s window.
2. Draw string “Hello Applet World” on x = 10, y = 60 location.
3. Draw smiley face, with background color of pink.
4. Draw Line and fill Rectangle, Oval, Arc, Round-Rectangle on different location.
5. Draw home looks like this:

25
.

• Before Chapter-3, we will continue with:


• AWT and Swing API Introduction

26

You might also like