Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Interface Types and Polymorphism Part 2.

Similar presentations


Presentation on theme: "Chapter 4 Interface Types and Polymorphism Part 2."— Presentation transcript:

1 Chapter 4 Interface Types and Polymorphism Part 2

2 Graphic User Interface Let’s start with the basics  How to display a window and how to add user interface components to it.

3 Frames (1) A frame window is a top-level window, usually decorated with borders and a title bar. JFrame frame = new JFrame(); frame.pack(); frame.setVisible(true); frame.setsize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

4 Frame (2) setDefaultCloseOperation setDefaultCloseOperation  EXIT_ON_CLOSE  DISPOSE_ON_CLOSE  DO_NOTHING_ON_CLOSE  HIDE_ON_CLOSE  http://java.sun.com/docs/books/tutorial/uiswing/compo nents/frame.html http://java.sun.com/docs/books/tutorial/uiswing/compo nents/frame.html  http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Wi ndowConstants.html http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Wi ndowConstants.html

5 Adding Components (1) 1.Construct components: (ex) a button JButton helloButton = new JButton("Say Hello"); JButton goodbyeButton = new JButton("Say Goodbye"); 2.Set frame layout container.setLayout(new FlowLayout());  FlowLayout lines up components side by side 3.Add the components to the frame. frame.add(helloButton); frame.add(goodbyeButton);

6 Adding Components (2) Source codes: Ch4/frame/FrameTester.javaCh4/frame/FrameTester.java

7 User Interface Actions (1) Previous program's buttons don't have any effect Add listener object(s) to button belong to class implementing ActionListener interface public interface ActionListener { int actionPerformed(ActionEvent event); } Listeners are notified when button is clicked

8 User Interface Actions (2) Add action code into actionPerformed method helloButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText("Hello, World"); } }); When button is clicked, text field is set

9 User Interface Actions (3) Review how buttons do their jobs 1.Construct a listener object and add it to the botton: ActionListener listener = …; helloButton.addActionListener(listener);  The button simply stores the listener object. Note that the actionPerformed method is not yet called. 2.When button clicked, button notifies listeners to call the actionPerformed method of the listener: ActionEvent event = …; listener.actionPerformed(event); 3.The actionPerformed method executes.

10 User Interface Action (4) Ch4/action1/ActionTest.java

11 User Interface Action (5) Accessing Variables from Enclosing Scope Method of Inner classes can access variables that are visible in the enclosing scope  e.g. actionPerformed method accesses the textField variable of the enclosing main method If an inner class accesses a local variable from an enclosing scope, the variable must be declared as final. final JTextField textField = new TextField(Field_WIDTH);

12 Constructing Related Actions (1) To construct multiple objects of the same anonymous class, you must instantiate the anonymous class in a helper method and then call the helper method multiple times. Note: you should declare parameters final public static ActionListener createGreetingButtonListener(final String message) { return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } }; }

13 Constructing Related Actions (2) Public class ActionTest { Public static void main(String[] args) { … textField = new JTextField(FIELD_SIZE); helloButton.addActionListener( creatGreetingButtonListener(“Hello, World !”)); goodbyeButton. addActionListener( creatGreetingButtonListener(“Goodbye, World !”)); … } // the helper method in the previous slide places here }

14 Timers Javax.swing package Timer generates a sequence of action events spaced apart at equal time interval, and notifies a designated action listener. ActionListener listener =...; final int DELAY = 1000; // 1000 millisec = 1 sec Timer t = new Timer(DELAY, listener); t.start();  The start method returns immediately. A new thread of execution is started that issues action events in the specified frequency.

15 Ch4/timer/TimerTester.java Ch4/timer/TimerTester.java

16 Designing an Interface Type (cont.) Public class ShapeIcon implements Icon { Public void paintIcon(Component c, Graphics g, int x, int y) { Paint the shape } … }

17 Designing an Interface Type (cont.) ShapeIcon icon = new ShapeIcon(…); JLabel label = new JLabel(icon); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { move the shape label.repaint(); } };

18 Designing an Interface Type (cont.) Use timer to move car shapes Draw car with CarShape Two responsibilities:  Draw shape  Move shape Define new interface type MoveableShape : we can decoupled the animation from the car shape.

19 CRC Card for the MoveableShape Interface Type

20 Designing an Interface Type Name the methods to conform to standard library public interface MoveableShape { void draw(Graphics2D g2); void translate(int dx, int dy); } CarShape class implements MoveableShape public class CarShape implements MoveableShape { public void translate(int dx, int dy) { x += dx; y += dy; }... }

21 Implementing the Animation Label contains icon that draws shape Timer action moves shape, calls repaint on label Label needs Icon, we have MoveableShape Supply ShapeIcon adapter class ShapeIcon.paintIcon calls MoveableShape.draw

22 Implementing the Animation

23 Ch4/animation/MoveableShape.java Ch4/animation/ShapeIcon.java Ch4/animation/AnimationTester.java Ch4/animation/AnimationTester.java Ch4/animation/CarShape.java


Download ppt "Chapter 4 Interface Types and Polymorphism Part 2."

Similar presentations


Ads by Google