Chapter 4 Interface Types and Polymorphism Part 2.

Slides:



Advertisements
Similar presentations
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Advertisements

Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Graphical User Interfaces
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Interfaces & Polymorphism part 2:
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
Basics of GUI Programming Chapter 11 and Chapter 22.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 4 Interfaces and Polymorphism Object-Oriented Design & Patterns Cay S. Horstmann.
Multiple buttons and action calls
GUIs and Events Rick Mercer.
Chapter 8 Event Handling.
Interface types and Polymorphism
CompSci 230 S Programming Techniques
ITEC324 Principle of CS III
A First Look at GUI Applications
Processing Timer Events
Graphical User Interface (pronounced "gooey")
Chapter 4 Interface Types and Polymorphism Part 2
Java Programming: From Problem Analysis to Program Design,
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Timer class and inner classes
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
ITEC324 Principle of CS III
Presentation transcript:

Chapter 4 Interface Types and Polymorphism Part 2

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

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

Frame (2) setDefaultCloseOperation setDefaultCloseOperation  EXIT_ON_CLOSE  DISPOSE_ON_CLOSE  DO_NOTHING_ON_CLOSE  HIDE_ON_CLOSE  nents/frame.html nents/frame.html  ndowConstants.html ndowConstants.html

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

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

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

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

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.

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

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

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

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 }

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.

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

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

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(); } };

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.

CRC Card for the MoveableShape Interface Type

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

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

Implementing the Animation

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