chapter 13 java awt – part ii (optional)

36
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Upload: amir-hart

Post on 31-Dec-2015

31 views

Category:

Documents


2 download

DESCRIPTION

Chapter 13 Java AWT – Part II (Optional). Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: discuss the use of Java in animating World Wide Web applications - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 13 Java AWT – Part  II (Optional)

Chapter 13Java AWT – Part II

(Optional)

Lecture Slides to Accompany

An Introduction to Computer Science Using Java (2nd Edition)

by

S.N. Kamin, D. Mickunas, E. Reingold

Page 2: Chapter 13 Java AWT – Part  II (Optional)

Chapter Preview

In this chapter we will:• discuss the use of Java in animating World Wide

Web applications• show how to write Java applets • introduce Java graphical components (e.g. Textfields,

Buttons, and Labels)• show how Java programs can be made to react to

user actions on Java graphical components• show how Java applications can be run from inside

web browsers

Page 3: Chapter 13 Java AWT – Part  II (Optional)

Frames

• A frame is a window with a title bar and a border• The Frame class is a subclass of Container class• Container class objects may have other

components (e.g. Buttons) added to them using the add method

• A typical Java GUI will create and display one or more frames

• To make a frame visible the message setVisbible(true) must be sent to the frame

Page 4: Chapter 13 Java AWT – Part  II (Optional)

Layout Managers

• Governs how components are arranged inside a Container object

• The Container method setLayout allows the programmer to specify which layout manager (e.g. FlowLayout) is desired for a particular container object

• The size of a Container object should be set explicitly by the programmer as well

• Can use pack() to set size to accommodate preferred sizes of components)

Page 5: Chapter 13 Java AWT – Part  II (Optional)

Frame Example

public class TwoButtons { Frame f; Button redButton, blueButton;

public TwoButttons() { f = new Frame(“Two Buttons Frame”); redButton = new Button(“Red”); blueButton = new Button(“Blue”); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButton); f.pack(); f.setVisible(true); }}

Page 6: Chapter 13 Java AWT – Part  II (Optional)
Page 7: Chapter 13 Java AWT – Part  II (Optional)

UML Notation

• The filled diamond represents composition• This shows that the class TwoButtons

contains a class Frame which contains a class Buttons

• A good UML diagram only shows the classes and associations that are important to understanding the architecture

• This UML diagram does not include super classes like Component or Container

Page 8: Chapter 13 Java AWT – Part  II (Optional)

Using Inheritance

• It is possible to use inheritance to allow TwoButtons to become a frame in its own right

• Note that the UML diagram will show TwoButtons as a subclass of Frame

• You will need to use super()to set the frame title

Page 9: Chapter 13 Java AWT – Part  II (Optional)
Page 10: Chapter 13 Java AWT – Part  II (Optional)

Frame Inheritance Example

public class TwoButtons extends Frame { Button redButton, blueButton;

public TwoButttons() { super(“Two Buttons Frame”); redButton = new Button(“Red”); blueButton = new Button(“Blue”); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButton); f.pack(); f.setVisible(true); }}

Page 11: Chapter 13 Java AWT – Part  II (Optional)

Other Simple Java Components

• Label– contains some text that can be set using a

constructor or the method setLabel

• TextField– a box into which the user can type text– text can be edited (backspace, delete, etc.)– text can be retrieved using getText()– text can be set using setText()

Page 12: Chapter 13 Java AWT – Part  II (Optional)

Java AWT Event Model

Including reactive program components involves:

1. Having the class header declare itself as implementing the ActionListener interface

2. Typically in the class constructor the class instance registers itself as being interested in listening for events from a newly created component

3. One method (e.g. actionPerformed) of the ActiveListener interface is defined

Page 13: Chapter 13 Java AWT – Part  II (Optional)

Frame Inheritance Examplepublic class classname extends Frame implements ActionListener { … Button buttonname; … public classname() { … buttonname = new Button(“button label”); … add(buttonname); … buttonname.ActionListener(this); … } … public void actionPerformed(ActionEvent e) { … what to do when button is pushed … }}

Page 14: Chapter 13 Java AWT – Part  II (Optional)
Page 15: Chapter 13 Java AWT – Part  II (Optional)

UML Notation for Button Program

• Abstract class indicated by italicizing the class name

• Implements association (concrete class) indicated by line with a closed arrowhead

• UML stereotype (constructors, accessor, interface implementation methods) indicated by enclosing a descriptive word inside guillements << >>

Page 16: Chapter 13 Java AWT – Part  II (Optional)

Closing the Window

• You must implement the WindowListener and its seven required methods windowActivated, windowClosed, windowClosing, windowDeactivated, windowIconified, windowDeiconified, windowOpened

• This is usually done in a programmer defined class like ClosableFrame and all classes needing these services would extend ClosableFrame

Page 17: Chapter 13 Java AWT – Part  II (Optional)
Page 18: Chapter 13 Java AWT – Part  II (Optional)

Using Conditionals with Reactive Components

public void actionPerformed(ActionEvent e) { double fahr, cent; // check for user input in tFahr TextField if (e.getSource() == tFahr) { fahr = new Double(tFahr.getText()),doubleValue(); cent = 5.0 * (fahr – 32.0) / 9.0; tCent.setText(cent + “”); // update tCent TextField } else { cent = new Double(tCent.getText()),doubleValue(); fahr = 9.0 * cent / 5.0 + 32.0; tFahr.setText(fahr + “”); }

Page 19: Chapter 13 Java AWT – Part  II (Optional)

Checkboxes• Used to allow user to select one or more items• Always has a label• Program often needs to react to the user selection(s)• Declaration example:

Checkbox powerBrakes = new Checkbox(“Power Brakes”);

Checkbox powerSteering = new Checkbox(“Power Steering”);

Checkbox ac = new Checkbox(“Air Conditioning”);

add(powerBrakes);

add(powerSteering);

add(ac);

Page 20: Chapter 13 Java AWT – Part  II (Optional)

Programming Checkboxes

• Program is declared with implements ItemListener

• Checkbox is registered by calling addItemListener

• Event is handled using itemStateChanged argument type ItemEvent

• The ItemEvent argument is used to tell which item triggered the event by calling getSource

Page 21: Chapter 13 Java AWT – Part  II (Optional)

Radio Buttons

• A group of Checkboxes in which only one item can be selected at a time

• Implemented using a Java CheckboxGroup• Items are declared initially as selected (true) or

unselected (false) • Example:

CheckboxGroup gender;Checkbox maleCheck = new Checkbox(“Male”, gender, true);Checkbox femaleCheck = new Checkbox(“Female”, gender, true);

Page 22: Chapter 13 Java AWT – Part  II (Optional)

Processing Radio Buttons

public void compute() {

boolean female =

(gender.getSelectedCheckbox() == femaleCheck);

if ((bodyMassIndex > 27.8) ||

(female && (bodyMassIndex > 27.3))

lHigh.setText(“This is considered high”);

else

1High.setText(“this is not considered high”);

}

Page 23: Chapter 13 Java AWT – Part  II (Optional)

Drawing in a Frame

• To draw in a Frame you need to the override the frame’s paint method:public void paint(Graphics g)

• Graphics objects are defined by the Java runtime system and are used for drawing operations

• The Frame should be considered to be a grid with upper left coordinates (0,0) and positive coordinates (x,y) for the lower right

Page 24: Chapter 13 Java AWT – Part  II (Optional)

Typical Drawing Codepublic class MyDrawing extends ClosableFrame { … public MyDrawing() { … } … public void paint(Graphics g) { … g.drawLine(…); … g.drawImage(…); … }}

Page 25: Chapter 13 Java AWT – Part  II (Optional)

Repaint and Update

• The paint method requires an argument and actionPerformed does not know what to supply when called

• The method repaint will clear the Frame and then call paint along with supplying the needed missing argument

• You can prevent clearing the Frame when using repaint by overriding the method update before calling repaint

Page 26: Chapter 13 Java AWT – Part  II (Optional)
Page 27: Chapter 13 Java AWT – Part  II (Optional)

Panel and Canvas Classes

• The Panel class is container subclass that is used to reserve a rectangular portion of a Frame to place other components

• The Canvas class is not a Container subclass, but does allow you to reserve a portion of a Frame to draw in

Page 28: Chapter 13 Java AWT – Part  II (Optional)

Comparing Layout ManagersFlowLayout

– Default frame layout– Components are placed on new line only when needed

GridLayout– Frame is declared as a grid of fixed size (e.g. two rows and

three columns)– Components are placed in the grid left to right and top to

bottom

BorderLayout– Frame is divided into north, south, east, west, and center– Components are placed by the programmer in the desired

location using the add method

Page 29: Chapter 13 Java AWT – Part  II (Optional)
Page 30: Chapter 13 Java AWT – Part  II (Optional)

Typical GUI Frame

Panel p1 = new Panel();

p1.setLayout(new GridLayout(2,1));

p1.add(component);

p1.add(component);

Panel p2 = new Panel();

p2.setLayout(…);

p2.add(component);

setLayout(new BorderLayout());

add(“North”, p1);

add(“Center”, p2);

Page 31: Chapter 13 Java AWT – Part  II (Optional)

Scrollbars

• The leftmost position of a horizontal scrollbar corresponds to some integer value and the rightmost position corresponds to a larger integer value

• Scrollbars can be displayed vertically• User movement options

– Clicking either end button causes bubble to move in unit increments

– Clicking the are between bubble and end button causes movement in 10 unit increments

– Clicking and dragging the bubble in the desired direction

Page 32: Chapter 13 Java AWT – Part  II (Optional)

Hypertext Markup LanguageHTML – Skeleton

<HTML><HEAD><TITLE>Page Title</TITLE></HEAD><BODY>

Text and graphics to display in browser

</BODY></HTML>

Page 33: Chapter 13 Java AWT – Part  II (Optional)

HTML Text Displays

• Displays a centered heading in browser window<CENTER>

<H1>Naploean I</H1>

</CENTER>

• To identify text paragraphs to be displayed<P>

In my spare time I like to do origami

and spend quiet evenings at home with my wife Josephine.

</P>

Page 34: Chapter 13 Java AWT – Part  II (Optional)

HTML Using Pictures and Links

• Displays a centered picture<CENTER>

<IMG SRC=“jojo.jpg”>

</CENTER>

• To display a text link to another web page<A HREF=“jojo.html”>Josephine</A>

• To display a picture link to another web page<A HREF=“jojo.html”><IMG SRC=“jojo.jpg”></A>

Page 35: Chapter 13 Java AWT – Part  II (Optional)

Applet Tag and HTML

• To include a Java applet as part of an HTML document<APPLET CODE=“BodyMass.class” WIDTH=0 HEIGHT=0></APPLET>

• Java applet shell for BodyMass.classimport java.applet.*;public class BodyMass extends Applet { … public_html void start() { new BodyMass(); }}

Page 36: Chapter 13 Java AWT – Part  II (Optional)

Transforming a Java Program into an Applet

1. Set the size you want the applet to be in the HTML file

2. Add the import statement to the Java code java.applet.*;

3. Change header from extends Frame to extends Applet

4. Change constructor heading to public void start();

5. Eliminate any Java code that is meaningless in applets