Download presentation
Presentation is loading. Please wait.
Published byGerard Morris Modified over 9 years ago
1
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal
2
Introduction to java applet
3
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.
4
Applet viewer Running applet in web browser
5
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.
6
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
7
Is it necessary to use all applet’s methods ?
8
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.
9
import allows us to use predefined classes (allowing us to use applets and graphics, Scanner in this case). To use JApplet, you have to import javax.swing.JApplet To paint or draw a rectangle or any thing else, you must import java.awt.Graphics To ask user to insert numbers, you must import java.util.Scanner. extends allows us to inherit the capabilities of class JApplet. Define the variables we need. Why aren’t they written inside init function? Ask user to insert each variable in (init) function. Define paint method to draw a rectangle. drawRect takes 4 parameters. The two start points, the width and the height of the rectangle.
10
Testing#1
11
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.
12
import allows us to use predefined classes (allowing us to use applets and graphics, JOptionPane in this case). 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 extends allows us to inherit the capabilities of class JApplet. Define the variables we need. Why aren’t they written inside init function? Ask user to insert each variable using JoptionPane. The first string we want to draw. It contains all five numbers. Find the smallest number of the five integers. The second value we want to paint.
13
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.
14
Testing…..
15
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].
17
The End !!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.