CS 151 ObjectOriented Design October 1 Class Meeting
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
Review Quiz #2 SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 2
Anonymous Classes public class Animal. Comparator. By. Height implements Comparator<Animal> { public int compare(Animal animal 1, Animal animal 2) { if (animal 1. height < animal 2. height) return -1; if (animal 1. height > animal 2. height) return +1; return 0; } } Comparator<Animal> comp = new Animal. Comparator. By. Height(); Collections. sort(a. List, comp); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 3
Anonymous Classes o Notice that in our program, after we declare class Animal. Comparator. By. Height, we use the class in only one statement: Comparator<Animal> comp = new Animal. Comparator. By. Height(); o We can shorten our code even further by not giving the class a name in a separate class definition. n n Define the body of the class only when it’s used. Make it an anonymous class. _ SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 4
Anonymous Classes Comparator<Animal> comp = new Animal. Comparator. By. Height(); Collections. sort(a. List, comp); Comparator<Animal> comp = new Comparator<Animal>() Interface that the class implements { public int compare(Animal animal 1, Animal animal 2) { if (animal 1. height < animal 2. height) return 1; if (animal 1. height > animal 2. height) return +1; return 0; Note the semicolon } }; Collections. sort(a. List, comp); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 5
Anonymous Classes o We can even get rid of variable comp: Collections. sort(a. List, comp); Collections. sort(a. List, new Comparator<Animal>() { public int compare(Animal animal 1, Animal animal 2) { if (animal 1. height < animal 2. height) return 1; if (animal 1. height > animal 2. height) return +1; return 0; parenthesis Note the closing } }); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 6
Anonymous Classes public class Animal { public static Comparator<Animal> comparator. By. Weight() { return new Comparator<Animal>() { public int compare(Animal animal 1, Animal animal 2) { if (animal 1. weight < animal 2. weight) return -1; if (animal 1. weight > animal 2. weight) return +1; return 0; } } } public static Comparator<Animal> comparator. By. Height() { return new Comparator<Animal>() { public int compare(Animal animal 1, Animal animal 2) { if (animal 1. height < animal 2. height) return -1; if (animal 1. height > animal 2. height) return +1; return 0; } } } CS 151: Object-Oriented Design. . . SJSU Dept. of Computer Science } Fall 2013: October 1 © R. Mak Return an anonymous weight comparator class. Return an anonymous height comparator class. 7
Anonymous Classes o Now we can have more readable code: Collections. sort(a. List, Animal. comparator. By. Weight()); Collections. sort(a. List, Animal. comparator. By. Height()); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 8
The JFrame Window Frame Class o An introduction to graphical user interface (GUI) programming using the Java Foundation Classes (JFC) n o Use the Swing JFrame class to create window objects. n n n o AKA “Swing” Borders Title bar Default title bar buttons (close, resize, etc. ) Included at no extra charge! Basic code: JFrame frame = new JFrame(); frame. pack(); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 9
The JFrame Window Frame Class o Create two button components and a text field component: JButton hello. Button = new JButton("Say Hello"); JButton goodbye. Button = new JButton("Say Goodbye"); final int FIELD_WIDTH = 20; JText. Field text. Field = new JText. Field(FIELD_WIDTH); text. Field. set. Text("Click a button!"); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 10
The JFrame Window Frame Class o Before we add the buttons and the text field to the frame, we must specify how to lay out the components. n A window frame can lay out its components in different ways. o o o 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. n n The Flow. Layout 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. set. Layout(new Flow. Layout()); frame. add(hello. Button); frame. add(goodbye. Button); frame. add(text. Field); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak JFrame demo 11
Button Actions o But these buttons don’t do anything when you press them! o We need to write code that’s executed whenever the user presses a button. n Attach the code to the button as an action listener object. o n The action listener listens for certain actions, namely, a button press. A button can have multiple action listener objects. o When the button is pressed, each action listener executes. _ SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 12
Button Actions o How does the button code know how to execute an action listener object? n The action listener object must be instantiated from a class that implements the Action. Listener interface: public interface Action. Listener { int action. Performed(Action. Event event); } o This interface is the contract between the button and the action listener. n When the button is pressed, the button code knows that it can call the action. Performed() method of each action listener object that was attached to the button. SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 13
Button Actions o Create an action listener and attach it to the hello button: hello. Button. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event event) { text. Field. set. Text("Hello, World"); } }); o This statement does not execute the action listener’s action. Performed() method. n n It creates the action listener using an anonymous class and attaches it to the hello button. The action. Performed() method executes only when the button is pressed. SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak Button action demo #1 14
Button Actions public static void main(String[] args) {. . . final JText. Field text. Field = new JText. Field(FIELD_WIDTH); . . . hello. Button. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event event) { text. Field. set. Text("Hello, World!"); } }); . . . } o The inner action listener class references local variable textfield from the enclosing scope. n In order for this to work, variable textfield must be final. SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 15
Button Actions o 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. n This should remind us of the Factory Method Design Pattern. public static Action. Listener create. Greeting. Button. Listener( final String message) { return new Action. Listener() { public void action. Performed(Action. Event event) { text. Field. set. Text(message); } }; } SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak Button action demo #2 16
Inversion of Control (Io. C) o In a traditional command-line program, the program is in control of the sequence of actions. o In a GUI program, the program is not in control of the sequence of actions. n n 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. _ SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 17
Timer Actions o A timer also has an action listener. n o 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(); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 18
Timer Actions o On each tick of the timer, display the current time: Action. Listener listener = new Action. Listener() { public void action. Performed(Action. Event event) { Date now = new Date(); text. Field. set. Text(now. to. String()); } }; Timer demo SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 19
Graphics Context o Recall that interface Icon has a paint. Icon() method: public void paint. Icon(Component c, Graphics g, int x, int y); o Graphics g is the graphics context. n Treat the context as a black box. o n n It contains information needed by the graphics system. Java now has a more powerful Graphics 2 D class. Apply a cast to the Graphics object: public void paint. Icon(Component c, Graphics g, int x, int y) { Graphics 2 D g 2 = (Graphics 2 D) g; . . . } SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 20
The Shape Interface o The draw() method of the graphics context can draw an object of any class that implements the Shape interface: Shape s =. . . ; g 2. draw(s); o Class Rectangle 2 D. Double implements the Shape interface: top left corner g 2. draw(new Rectangle 2 D. Double(x, y, width, height)); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 21
The Shape Interface o Class Ellipse 2 D. Double also implements the Shape interface: g 2. draw(new Ellipse 2 D. Double(x, y, width, height)); bounding box SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 22
The Shape Interface o Class Line 2 D. Double draws a line segment: Point 2 D. Double start = new Point 2 D. Double(x 1, y 1); Point 2 D. Double end = new Point 2 D. Double(x 2, y 2); Shape segment = new Line 2 D. Double(start, end); g 2. draw(segment); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 23
The Shape Interface Is there a “typo” in this diagram? SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 24
More Drawing Methods o The graphics context’s fill() method fills in a shape with the current color: g 2. fill(ellipse); o To set the current color: g 2. set. Color(Color. RED); SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 25
More Drawing Methods, cont’d o To draw text: basepoint g 2. draw. String(text, x, y); Draw a car demo SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 26
The Java Interface as a Contract public interface Household. Pet { void feed(Food f); } Any class that implements an interface is guaranteed to implement each and every one of the interface methods. public interface Action. Listener { void action. Performed(Action. Event event); } public interface Icon { int get. Icon. Width(); int get. Icon. Height(); void paint. Icon(Component c, Graphics g, int x, int y); } SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 27
- Slides: 27