Chapter 4 Interface Types and Polymorphism Part 2

Slides:



Advertisements
Similar presentations
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Advertisements

CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
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)
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
Design Patterns and Graphical User Interfaces Horstmann ,
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,
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
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)
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Basics of GUI Programming Chapter 11 and Chapter 22.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
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)
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 4 Interface Types and Polymorphism Part 2.
Chapter 4 Interfaces and Polymorphism Object-Oriented Design & Patterns Cay S. Horstmann.
Multiple buttons and action calls
Modular Event Handling
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Interface types and Polymorphism
ITEC324 Principle of CS III
A First Look at GUI Applications
Processing Timer Events
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
ITEC324 Principle of CS III
Presentation transcript:

Chapter 4 Interface Types and Polymorphism Part 2

Graphic User Interface (Basic) How to display a window and how to add user interface components to it.

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

Frame setDefaultCloseOperation EXIT_ON_CLOSE DISPOSE_ON_CLOSE DO_NOTHING_ON_CLOSE HIDE_ON_CLOSE http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html Examples ActionTest.java FramDemo2.java (http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/)

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

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

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

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

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

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

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 }

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

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

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.) 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/AnimationTest.java Ch4/animation/CarShape.java

Implementing the Animation