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
(Basic) How to display a window and how to add user interface components to it.

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

4 Frame setDefaultCloseOperation
EXIT_ON_CLOSE DISPOSE_ON_CLOSE DO_NOTHING_ON_CLOSE HIDE_ON_CLOSE Examples ActionTest.java FramDemo2.java (

5 Adding Components Construct components JButton helloButton = new JButton("Say Hello"); Set content pane layout Container contentPane = frame.getContentPane(); container.setLayout(new FlowLayout()); Add components to content pane contentPane.add(helloButton); Ch4/frame/FrameTest.java

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

7 User Interface Actions
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

8 User Interface Actions (cont.)
Constructor attaches listener: ActionListener listener = …; helloButton.addActionListener(listener); When button clicked, button notifies listeners to call the actionPerformed method of the listener: listener.actionPerformed(event);

9 User Interface Action (cont.)
Ch4/action1/ActionTest.java

10 User Interface Action Accessing Variables from Enclosing Scope
Inner class can access variables from enclosing scope e.g. textField Can access enclosing instance fields, local variables Local variables must be marked final final JTextField textField = ...;

11 Constructing Related Actions (cont.)
Write helper method that constructs objects Pass variable information as parameters Declare parameters final public static ActionListener createGreetingButtonListener(final String message) { return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } }; //return }

12 Constructing Related Actions (cont.)
Public class ActionTest { Public static void main(String[] args) textField = new JTextField(FIELD_SIZE); helloButton.addActionListener( creatGreetingButtonListener(“Hello, World !”)); goodbyeButton. addActionListener( creatGreetingButtonListener(“Goodbye, World !”)); }

13 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(); Action listener called when delay elapsed Ch4/timer/TimerTest.java

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

15 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.

16 CRC Card for the MoveableShape Interface Type

17 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; }    }

18 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

19 Implementing the Animation
Ch4/animation/MoveableShape.java Ch4/animation/ShapeIcon.java Ch4/animation/AnimationTest.java Ch4/animation/CarShape.java

20 Implementing the Animation


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

Similar presentations


Ads by Google