Lab#5-Introduction to java applet

advertisement
Object Oriented
Programming (OOP)
LAB # 5
TA. Maram
& TA. Mubaraka
TA. Kholood
& TA. Aamal
Introduction to java applet
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).
Totouse
JApplet,
extends allows•us
inherit
the you have to import
javax.swing.JApplet
capabilitieswe
of class
JApplet.
Define the variables
•To paint or draw a rectangle or any
need. Why aren’t they
thing else, you must import
written inside init
java.awt.Graphics
function?
•To ask user to insert numbers, you
must import java.util.Scanner.
Ask user to insert each
variable in (init)
function.
Define paint method to
drawRect takes 4
draw a rectangle.
parameters. The two start
points, the width and the
height of the rectangle.
Testing#1
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
function?
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
javax.swing.JApplet
•To paint or draw a String or any thing
else, you must import
java.awt.Graphics
•To ask user to insert numbers using
JOptionPane , you must import
javax.swing.JOptionPane
Ask user to insert each
variable using
JoptionPane.
The first string we want
to draw. It contains all
Find the smallest
number
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
draw.
Use drawString to draw the
three texts we stored
before.
drawString takes three
parameters. The string to
be displayed and it’s
position.
Testing…..
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 !!
Download