COS240 Object-Oriented Languages

skatechildrenΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

184 εμφανίσεις

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

1

COS240

Object
-
Oriented Languages


AUBG, COS dept, Spring semester 2012



Reference books:


Budd A.,
An Introduction to Object
-
Oriented
Programming, Addison
-
Wesley Publ. Com., 3
rd

ed. 2003.


Booch Grady

et al, Object
-
Oriented Analysis and
Design with Applications, Addison
-
Wesley Publ. Com.,
3
rd

ed. 2007.




Course lecturer: Assoc. Prof. Stoyan Bonev, PhD

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

2

COS240 O
-
O Languages

AUBG, COS dept

Lecture 20

Title:

Intro to Java Applets


Reference: COS240 Syllabus, Malik, ch 12

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

3

Lecture Contents:


Learn about applets


Applet methods


Skeleton of a Java applet


Differences Between Applets and GUI
Applications


Converting a GUI Application to an Applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

4

Java Programming: From Problem Analysis to Program Design, 4e

4

Applets


Applet: a Java program that is embedded within a Web
page (i.e. HTML document) and executed by a Web
browser


Java programs called from within another application,
Frequently run from a Web page


Display as rectangular area


Can respond to user
-
initiated events


Behaviors come from Java class named
JApplet


Create an applet by extending the
class

JApplet


class

JApplet

contained in package
javax.swing


Java applet refers to Java little application.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

5

Java Programming: From Problem Analysis to Program Design, 4e

5

Inheritance Hierarchy of GUI Classes

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

6

Java Programming: From Problem Analysis to Program Design, 4e

6

Members of
class

JApplet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

7

Java Programming: From Problem Analysis to Program Design, 4e

7

Members of
class

Japplet
(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

8

Java Programming: From Problem Analysis to Program Design, 4e

8

Applets (continued)


No
main()

method


Methods
init(void)
,
start(void)
,
paint(Graphics
g)

guaranteed to be invoked in sequence


Methods
init()
,
start()

have no parameters


Method’s
paint()

formal parameter allows the
developer to use abstract class
Graphics

without
actually creating a
Graphics

object


To develop an applet:


Override one or all of the methods above.


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

9

Java Programming: From Problem Analysis to Program Design, 4e

9

Applet Methods



init()

Method


Initializes variables


Gets data from user


Places various GUI components



paint()

Method


Performs output


For example draws various items, including strings, in
the content pane of the applet.



init(), paint() m
ethods need to share
common data items, so these data items are the
data members of the applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

10

Java Programming: From Problem Analysis to Program Design, 4e

10

Skeleton of a Java Applet

import java.awt.Graphics;

import javax.swing.JApplet;


public class WelcomeApplet extends JApplet

{


}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

11

Java Programming: From Problem Analysis to Program Design, 4e

11

Applet Displaying Welcome Message

Problem: create an applet to display welcome message.

Analysis:



No initialization required: no need of

init()
method



What is must: to override the method

paint()
to draw the
welcome message.



Sometimes when you override a method, it is a good idea to
invoke the corresponding method of the parent class. Whenever you
override the

paint()
method, the first Java stmt is


super.paint(g);



To display the string that contains the welcome message, the
drawString()

method of the
Graphics

class is to be used
(details in previous lecture on Graphics)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

12

Java Programming: From Problem Analysis to Program Design, 4e

12

Applet Displaying Welcome
Message

//Welcome Applet



import java.awt.Graphics;

import javax.swing.JApplet;


public class WelcomeApplet extends JApplet

{


public void paint(Graphics g)


{


super.paint(g); //Line 1


g.drawString(
"
Welcome to Java Programming
"
,30,30);

}

}


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

13

Java Programming: From Problem Analysis to Program Design, 4e

13

Applet Displaying Welcome
Message

Procedure:



As with an application, an applet is compiled to
produce

.class
file.



Once the
.class

file is created, it is to be
placed in a Web page to run the applet



For example, a file with extension
.html
, say
welcome.html

as that shown on the next slide
located in the same folder where the
.class

applet file resides

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

14

Java Programming: From Problem Analysis to Program Design, 4e

14

HTML to Run Applet

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

15

Java Programming: From Problem Analysis to Program Design, 4e

15

How to run Applet


Once the HTML file is created, there are
different ways to run the applet


Open the
.html

file with a Web browser

OR


In case you use JDK, you can run an utility with
a command line like


appletviewer Welcome.html

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

16

Java Programming: From Problem Analysis to Program Design, 4e

16

More attractive applets


Ways to make applets more attractive are to
vary the font type and the color scheme

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

17

Java Programming: From Problem Analysis to Program Design, 4e

17

class Font


Shows text in different fonts


Contained in package java.awt


For more details, see previous lecture on
Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

18

Java Programming: From Problem Analysis to Program Design, 4e

18

Constructors and Methods of the
class Font
(continued
)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

19

Java Programming: From Problem Analysis to Program Design, 4e

19

Constructors and Methods of the
class

Font
(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

20

Java Programming: From Problem Analysis to Program Design, 4e

20

class

Color


Shows text in different colors


Changes background color of component


Contained in package
java.awt


For more details, see previous lecture on
Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

21

Java Programming: From Problem Analysis to Program Design, 4e

21

Constants Defined in the
class
Color

(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

22

Java Programming: From Problem Analysis to Program Design, 4e

22

Constants Defined in the
class
Color

(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

23

Java Programming: From Problem Analysis to Program Design, 4e

23

class

Graphics


Provides methods for drawing items such as
lines, ovals, and rectangles on the screen


Contains methods to set the properties of
graphic elements including clipping area,
fonts, and colors


Contained in the package
java.awt


For more details, see previous lecture on
Graphics

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

24

Java Programming: From Problem Analysis to Program Design, 4e

24

Constructors and Methods of the
class

Graphics
(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

25

Java Programming: From Problem Analysis to Program Design, 4e

25

Constructors and Methods of
the
class

Graphics
(continued)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

26

More Demo programs


Java applet file

WelcomeApplet.java



Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

27

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

28

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

29

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

30

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

31

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

32

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java



Java applet file

FontsDisplayed.java



Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

33

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java



Java applet file

FontsDisplayed.java



Java applet file

ColorsDisplayed.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

34

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java



Java applet file

FontsDisplayed.java



Java applet file

ColorsDisplayed.java



Java applet file

FreeDrawApplet.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

35

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java



Java applet file

FontsDisplayed.java



Java applet file

ColorsDisplayed.java



Java applet file

FreeDrawApplet.java



Java applet file

OneChar.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

36

More Demo programs


Java applet file

WelcomeApplet.java



Java applet file

GrandWelcome.java



Java applet file

GrandWelcomeLine.java



Java applet file

GrandWelcomeCheckBox.java



Java applet file

GrandWelcomeRButton.java



Java applet file

GrandWelcomeFinal.java



Java applet file

FontsDisplayed.java



Java applet file

ColorsDisplayed.java



Java applet file

FreeDrawApplet.java



Java applet file

OneChar.java



Java applet file

OvalRectApplet.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

37

Java Programming: From Problem Analysis to Program Design, 4e

37

Differences Between Applets and
GUI Applications


Applets


Class extends
JApplet


No main method


Uses
init

method


Displayed by HTML


Sets title in HTML


Size set in HTML


Applet closes when
HTML doc closes


GUI applications


class extends
JFrame


Invokes main method


Uses constructors


Uses method
setVisible


Uses
setTitle

method


Uses method
setSize


Closes with
Exit

button

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

38

Java Programming: From Problem Analysis to Program Design, 4e

38

Converting a GUI Application to
an Applet


Change
JFrame

to
JApplet



Change constructor to method
init


Remove method calls such as
setVisible
,
setTitle
,
setSize


Remove the method
main



If applicable, remove
Exit

button and all
code associated with it (e.g., action listener)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

39

Java Programming: From Problem Analysis to Program Design, 4e

39

Converting a GUI Application to
an Applet



Java application file

TempConversion.java




Java applet file

TempConvertApplet.java


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

40

Exercises


Demo programs


Malik, chap 12



More programming exercises


Malik, chap 12, pp xxx

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

41









Thank You

for

Your attention!