object oriented programming (oop) lab # 5 ta. maram & ta. mubaraka ta. kholood & ta. aamal

17
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Upload: gerard-morris

Post on 22-Dec-2015

244 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Object Oriented Programming (OOP)

LAB # 5TA. Maram & TA. Mubaraka

TA. Kholood & TA. Aamal

Page 2: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Introduction to java applet

Page 3: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 4: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Applet viewer Running applet in web browser

Page 5: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Life Cycle of an Applet:There are four methods when using applet: init : This method is intended for whatever initializationis needed for an applet.start : This method is automatically called after initmethod. It is also called whenever user returns to thepage containing the applet after visiting other pages.stop : This method is automatically called whenever theuser moves away from the page containing applets.This method can be used to stop an animation.destroy : This method is only called when the browsershuts down normally.

Page 6: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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

Page 7: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Is it necessary to use all applet’s methods ?

Page 8: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 9: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 10: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Testing#1

Page 11: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 12: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 13: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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.

Page 14: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Testing ..…

Page 15: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

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].

Page 16: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal
Page 17: Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

The End !!