Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 2 Review Quiz #2

3 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);

4 SJSU Dept. of Computer Science Fall 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 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. _

5 SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 5 Anonymous Classes Comparator comp = new Comparator () { 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 comp = new AnimalComparatorByHeight(); Collections.sort(aList, comp);

6 SJSU Dept. of Computer Science Fall 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 () { 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);

7 SJSU Dept. of Computer Science Fall 2013: October 1 CS 151: Object-Oriented Design © R. Mak 7 Anonymous Classes public class Animal { public static Comparator comparatorByWeight() { return new Comparator () { 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 comparatorByHeight() { return new Comparator () { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; }... } Return an anonymous weight comparator class. Return an anonymous height comparator class.

8 SJSU Dept. of Computer Science Fall 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());

9 SJSU Dept. of Computer Science Fall 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 no extra charge!

10 SJSU Dept. of Computer Science Fall 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!");

11 SJSU Dept. of Computer Science Fall 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

12 SJSU Dept. of Computer Science Fall 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. _

13 SJSU Dept. of Computer Science Fall 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.

14 SJSU Dept. of Computer Science Fall 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

15 SJSU Dept. of Computer Science Fall 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!"); } });... }

16 SJSU Dept. of Computer Science Fall 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

17 SJSU Dept. of Computer Science Fall 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. _

18 SJSU Dept. of Computer Science Fall 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();

19 SJSU Dept. of Computer Science Fall 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

20 SJSU Dept. of Computer Science Fall 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;... }

21 SJSU Dept. of Computer Science Fall 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

22 SJSU Dept. of Computer Science Fall 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

23 SJSU Dept. of Computer Science Fall 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);

24 SJSU Dept. of Computer Science Fall 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.

25 SJSU Dept. of Computer Science Fall 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);

26 SJSU Dept. of Computer Science Fall 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.

27 SJSU Dept. of Computer Science Fall 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 an interface is guaranteed to implement each and every one of the interface methods.


Download ppt "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."

Similar presentations


Ads by Google