27
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu /~mak

CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

Embed Size (px)

DESCRIPTION

SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 3 Anonymous Classes public class AnimalComparatorByHeight implements Comparator { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } Comparator comp = new AnimalComparatorByHeight(); Collections.sort(aList, comp);

Citation preview

Page 1: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

CS 151: Object-Oriented DesignOctober 1 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2013Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

2

Review Quiz #2

Page 3: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

3

Anonymous Classes

public class AnimalComparatorByHeight implements Comparator<Animal>{ public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; }}

Comparator<Animal> comp = new AnimalComparatorByHeight();Collections.sort(aList, comp);

Page 4: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

4

Anonymous Classes Notice that in our program, after we declare class

AnimalComparatorByHeight, we use the class in only one statement:

Comparator<Animal> comp = new AnimalComparatorByHeight();

We can shorten our code even further by not giving the class a name in a separate class definition. Define the body of the class only when it’s used. Make it an anonymous class.

_

Page 5: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

5

Anonymous Classes

Comparator<Animal> comp = new Comparator<Animal>() { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } };

Collections.sort(aList, comp);

Interface that the class implements

Note the semicolon

Comparator<Animal> comp = new AnimalComparatorByHeight();Collections.sort(aList, comp);

Page 6: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

6

Anonymous Classes We can even get rid of variable comp:

Collections.sort(aList, new Comparator<Animal>() { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } });

Note the closing parenthesis

Collections.sort(aList, comp);

Page 7: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

7

Anonymous Classespublic class Animal{ public static Comparator<Animal> comparatorByWeight() { return new Comparator<Animal>() { public int compare(Animal animal1, Animal animal2) { if (animal1.weight < animal2.weight) return -1; if (animal1.weight > animal2.weight) return +1; return 0; } } }

public static Comparator<Animal> comparatorByHeight() { return new Comparator<Animal>() { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } } } ...}

Return ananonymous weightcomparatorclass.

Return ananonymous heightcomparatorclass.

Page 8: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

8

Anonymous Classes Now we can have more readable code:

Collections.sort(aList, Animal.comparatorByWeight());Collections.sort(aList, Animal.comparatorByHeight());

Page 9: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

9

The JFrame Window Frame Class An introduction to graphical user interface (GUI)

programming using the Java Foundation Classes (JFC) AKA “Swing”

Use the Swing JFrame class to create window objects. Borders Title bar Default title bar buttons (close, resize, etc.)

Basic code:

JFrame frame = new JFrame();frame.pack();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);

Included at noextra charge!

Page 10: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

10

The JFrame Window Frame Class Create two button components and

a text field component:

JButton helloButton = new JButton("Say Hello");JButton goodbyeButton = new JButton("Say Goodbye");

final int FIELD_WIDTH = 20;JTextField textField = new JTextField(FIELD_WIDTH);textField.setText("Click a button!");

Page 11: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

11

The JFrame Window Frame Class Before we add the buttons and the text field to the frame,

we must specify how to lay out the components. A window frame can lay out its components in different ways.

Therefore, what must we do to the layout algorithm? We must encapsulate the layout algorithm!

Delegate to a layout manager to lay out the components. The FlowLayout manager simply places the components

side by side in the order they’re added to the frame. We can change to another layout manager later.

frame.setLayout(new FlowLayout());

frame.add(helloButton);frame.add(goodbyeButton);frame.add(textField); JFrame demo

Page 12: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

12

Button Actions But these buttons don’t do anything

when you press them!

We need to write code that’s executed whenever the user presses a button. Attach the code to the button as an action listener object.

The action listener listens for certain actions, namely, a button press.

A button can have multiple action listener objects. When the button is pressed, each action listener executes.

_

Page 13: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

13

Button Actions How does the button code know how

to execute an action listener object? The action listener object must be instantiated from

a class that implements the ActionListener interface:

public interface ActionListener{ int actionPerformed(ActionEvent event);}

This interface is the contract between the button and the action listener. When the button is pressed, the button code knows that

it can call the actionPerformed() method of each action listener object that was attached to the button.

Page 14: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

14

Button Actions

This statement does not execute the action listener’s actionPerformed() method. It creates the action listener using an anonymous class

and attaches it to the hello button. The actionPerformed() method executes

only when the button is pressed.

helloButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText("Hello, World"); } });

Create an action listener and attach it to the hello button:

Button action demo #1

Page 15: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

15

Button Actions

The inner action listener class references local variable textfield from the enclosing scope. In order for this to work, variable textfield must be final.

public static void main(String[] args){ ... final JTextField textField = new JTextField(FIELD_WIDTH); ... helloButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText("Hello, World!"); } }); ...}

Page 16: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

16

Button Actions Since the action listeners of the two buttons differ

only in the messages that they display in the text field, we can create an action listener with a helper method. This should remind us of the Factory Method Design Pattern.

public static ActionListener createGreetingButtonListener( final String message){ return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } };}

Button action demo #2

Page 17: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

17

Inversion of Control (IoC) In a traditional command-line program,

the program is in control of the sequence of actions.

In a GUI program, the program is not in control of the sequence of actions. It specifies what actions to perform, but not when. The sequence is determined by the user interface,

for example, by the order the user presses buttons._

Page 18: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

18

Timer Actions A timer also has an action listener.

It listens for each “tick” of the timer.

Create and start a timer:

final int DELAY = 1000; // Milliseconds between timer ticks

Timer t = new Timer(DELAY, listener);t.start();

Page 19: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

19

Timer Actions

On each tick of the timer, display the current time:

ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { Date now = new Date(); textField.setText(now.toString()); } };

Timer demo

Page 20: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

20

Graphics Context

Graphics g is the graphics context . Treat the context as a black box.

It contains information needed by the graphics system. Java now has a more powerful Graphics2D class. Apply a cast to the Graphics object:

Recall that interface Icon has a paintIcon() method:public void paintIcon(Component c, Graphics g, int x, int y);

public void paintIcon(Component c, Graphics g, int x, int y){ Graphics2D g2 = (Graphics2D) g; . . .}

Page 21: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

21

The Shape Interface The draw() method of the graphics context

can draw an object of any class that implements the Shape interface:

Shape s = ...;g2.draw(s);

Class Rectangle2D.Double implements the Shape interface:g2.draw(new Rectangle2D.Double(x, y, width, height));

top left corner

Page 22: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

22

The Shape Interface

From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

Class Ellipse2D.Double also implements the Shape interface:g2.draw(new Ellipse2D.Double(x, y, width, height));

bounding box

Page 23: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

23

The Shape Interface

Class Line2D.Double draws a line segment:

Point2D.Double start = new Point2D.Double(x1, y1);Point2D.Double end = new Point2D.Double(x2, y2);Shape segment = new Line2D.Double(start, end);g2.draw(segment);

Page 24: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

24

The Shape Interface

Is there a “typo”in this diagram?

From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

Page 25: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

25

More Drawing Methods The graphics context’s fill() method fills in a shape

with the current color:

g2.fill(ellipse);

To set the current color:

g2.setColor(Color.RED);

Page 26: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

26

More Drawing Methods, cont’d To draw text:

g2.drawString(text, x, y);basepoint

Draw a car demo

From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

Page 27: CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: October 1

CS 151: Object-Oriented Design© R. Mak

27

The Java Interface as a Contract

public interface ActionListener{ void actionPerformed(ActionEvent event);}

public interface HouseholdPet{ void feed(Food f);}

public interface Icon{ int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y);}

Any class that implements aninterface is guaranteed to

implement each and every oneof the interface methods.