Lab#5-Introduction to java applet

Object Oriented
Programming (OOP)
LAB # 5
TA. Maram
& TA. Mubaraka
TA. Kholood
& TA. Aamal
Introduction to java applet
 Program that runs in
appletviewer (test utility for applets)
Web browser (IE, Communicator)
 A main() method is not invoked on an applet, and an applet
class will not define main().
 Applets are designed to be embedded within an HTML page.
 When a user views an HTML page that contains an applet, the
code for the applet is downloaded to the user's machine.
Applet viewer
Running applet in web browser
Life Cycle of an Applet:
There are four methods when using applet:
init : This method is intended for whatever initialization
is needed for an applet.
start : This method is automatically called after init
method. It is also called whenever user returns to the
page containing the applet after visiting other pages.
stop : This method is automatically called whenever the
user moves away from the page containing applets.
This method can be used to stop an animation.
destroy : This method is only called when the browser
shuts down normally.
Life Cycle of an Applet:
 browser visits page containing an applet
 browser calls init on that applet, once
 browser calls start on that applet
 browser goes away from that page
 browser calls stop on that applet
 browser comes back to that page
 browser calls start again on that applet
 browser shuts down
 browser calls destroy on the applet, once
Is it necessary to use all
applet’s methods ?
Exercise #1
Write an applet that allows the user to
input values for the arguments required by
method drawRect, then draws a rectangle
using the four input values.
import allows us to use predefined
classes (allowing us to use applets and
graphics, Scanner in this case).
extends allows•us
the you have to import
of class
Define the variables
•To paint or draw a rectangle or any
need. Why aren’t they
thing else, you must import
written inside init
•To ask user to insert numbers, you
must import java.util.Scanner.
Ask user to insert each
variable in (init)
Define paint method to
drawRect takes 4
draw a rectangle.
parameters. The two start
points, the width and the
height of the rectangle.
Exercise #2
Write an applet that reads five integers
and determines and prints the largest and
smallest integers in the group. Draw the
results on the applet. Use JOptionPane to
insert each values of the five integers.
Define the variables we
need. Why aren’t they
written inside init
import allows us to use predefined
classes (allowing us to use applets and
graphics, JOptionPane in this case).
extends allows us to inherit the
capabilities of class JApplet. • To use JApplet, you have to import
•To paint or draw a String or any thing
else, you must import
•To ask user to insert numbers using
JOptionPane , you must import
Ask user to insert each
variable using
The first string we want
to draw. It contains all
Find the smallest
five numbers.
of the five integers.
The second value we want
to paint.
Find the largest number of
the five integers.
The third text we want to
Use drawString to draw the
three texts we stored
drawString takes three
parameters. The string to
be displayed and it’s
Exercise #3
Write an applet that calculates the squares and
cubes of the numbers from 0 to 10 and draws the
resulting values in table format as shown below.
[Note: This program does not require any input
from the user].
The End !!

similar documents