Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Graphical User Interfaces
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Raw GUIs, ArrayLists Inheritance.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
For (int i = 1; i
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
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,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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:
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Events and Event Handling
Graphical User Interfaces
CompSci 230 S Programming Techniques
Object-Orientated Analysis, Design and Programming
A First Look at GUI Applications
Computer Science 209 Graphics and GUIs.
Ellen Walker Hiram College
GUI Programming III: Events
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Events, Event Handlers, and Threads
Presentation transcript:

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces COMP 102 # T2

© Xiaoying Gao, Peter Andreae COMP :2 Outline More examples of Interface classes Optional: GUI without comp102 library Previous exam questions on Interface Assignment 10 The model solution for exercise question is ed.

© Xiaoying Gao, Peter Andreae COMP :3 Interface class summary If you define an interface class: You have defined a type You can declare variables (or arrays) to have that type. You cannot make an object of the interface class new Balloon(…) // NOT ALLOWED (there is no constructor) Objects from classes implementing the interface are of that type. You can call any of the specified methods on values in those variables

© Xiaoying Gao, Peter Andreae COMP :4 Implementing an Interface If you define classes that implement the interface: public class RoundBalloon implements Balloon{ private int x; …… public class LongBalloon implements Balloon{ private String text; …… These classes must define all the specified methods: implements is a “promise” to be a value of the interface type. ie, to have all the methods. Failure to fullfill the promise leads to an exception " … is not abstract and does not override abstract method …." ⇒ the "broken promise" exception

© Xiaoying Gao, Peter Andreae COMP :5 Summary of Types in Java Predefined, primitive types (not objects) boolean, int, double, float, char,… Object types String, Scanner, PrintStream, Color, …. Flower, Rectangle, …. Each defined by a class. Arrays int[ ], double[ ], String[ ], Shape [ ] … Array types do not have a name They are object types (you only have a reference to them) Interface types Shape, UIButtonListener, UIMouseListener, ButtonListener, … interface type may encompass several different object types

© Xiaoying Gao, Peter Andreae COMP :6 Interfaces: More examples We have used interfaces before! public class Plotter implements UIButtonListener{ public Plotter(){ UI.addButton(“Read”, this); UI.addButton(“Plot”, this); UI.addButton(“Stats”, this); } Read Plot Stats Button-7 name: aListener: : “ Read ” Button-11 name: aListener: : “ Plot ” Button-8 name: aListener: : “ Stats ” Plotter-54 numbers: count: public void actionPerformed( …

© Xiaoying Gao, Peter Andreae COMP :7 ActionListener is an Interface UIButtonListener is an Interface Every button object contains a field of type UIButtonListener UIButtonListener specifies one method: public interface UIButtonListener{ public void buttonPerformed(String button); } When a button is pressed. the JVM looks for the object in the button’s buttonListener field It then calls the buttonPerformed method on the object passing the name of the button. It can only do this because the object is an UIButtonListener. (and this is the only thing it can do on the object)

© Xiaoying Gao, Peter Andreae COMP :8 Interfaces: More examples The listener can be a Plotter, or a Genealogy, or a … as long as it is an UIButtonListener DoB Parents Children Button-18 name: aListener: : “ DoB ” Button-21 name: aListener: : “ Parents ” Button-5 name: aListener: : “ Children ” Genealogy-2 persons: public void actionPerformed( … Quit Button-5 name: aListener: : “ Quit ”

©Xiaoying Gao, Peter Andreae COMP :9 Doing without UI & comp102 library #1 UI class lets you print and read from the text pane What do you do without the UI? Output: System.out is the "terminal window". You can print/println/printf to System.out, just like UI System.out.println("Here is a message for you"); Input: System.in is the keyboard via the "terminal window" You have to wrap it with a Scanner to read: Scanner userInput= new Scanner(System.in); : System.out.print("Enter age: "); int age = userInput.nextInt(); No "ask…()" methods

©Xiaoying Gao, Peter Andreae COMP :10 Standard library (without comp102 library) MouseListener is an interface public interface MouseListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); } ActionListener is an interface public interface ActionListener{ public void actionPerformed(ActionEvent e); }

©Xiaoying Gao, Peter Andreae COMP :11 Doing without UI & comp102 library How do you make a window with a text area, and buttons? make a JFrame object and set its size Make a JTextArea object and add to frame Make a JPanel and add to frame Make JButtons and add to the panel Make the frame visible Make a buttonPerformed method Writing to the text area is different.

©Xiaoying Gao, Peter Andreae COMP :12 Demo Interface import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; public class DemoInterface implements ActionListener{ private JFrame frame = new JFrame("Demo program"); private JTextArea textOut = new JTextArea(30, 30); public DemoInterface () { this.frame.setSize(600, 400); this.frame.add(this.textOut, BorderLayout.CENTER); JPanel panel = new JPanel(); this.frame.add(panel, BorderLayout.SOUTH); JButton button1 = new JButton("Doit"); button1.addListener(this); panel.add(button1); JButton button2 = new JButton("Clear"); button2.addListener(this); panel.add(button2); this.frame.setVisible(true); }

©Xiaoying Gao, Peter Andreae COMP :13 Demo Interface public void actionPerformed(ActionEvent e) { String button = e.getAction(); if (button.equals("Clear")){ this.textOut.setText(""); } else if (button.equals("Doit")){ for (int i = 1; i <=10; i++){ this.textOut.append("square of " + i+ " is " + (i*i)); }

© Xiaoying Gao, Peter Andreae COMP :14 Designing MiniDraw (Assig 10) A simple drawing editor Allows user to create drawings consisting of shapes: rectangles, ovals, lines, dots, trees, different colours. can add a new shape, delete a shape move a shape to a different position resize a shape save the current drawing to a file load a previous drawing from a file and edit it further.

© Xiaoying Gao, Peter Andreae COMP :15 Designing MiniDraw What Types of Data? Program must remember all the shapes so it can edit them ⇒ Rectangles, Lines, Ovals, Dots, Trees ⇒ List of the shapes (The user interface) (Contains an arrayof shapes) MiniDraw LineOvalDotTreeRectangle

© Xiaoying Gao, Peter Andreae COMP :16 Designing MiniDraw What Methods? MiniDraw Interaction fields Array of shapes Rectangle redraw moveBy : Same methods, but not the same effects. (eg, will draw themselves differently) Need different fields to store different information Line redraw moveBy : Oval redraw moveBy : Dot redraw moveBy : Tree redraw moveBy :

© Xiaoying Gao, Peter Andreae COMP :17 Array of shapes Can't be an array of Line Can't be an array of Object (well…, yucky if we do) Need an interface class for the Shape type public Interface Shape{ public void redraw(); public void moveBy(double dx, double dy); public boolean on(double x, double y); public String toString(); : } Can use Shape as a type. Lines, Rectangles, etc must be Shapes also

© Xiaoying Gao, Peter Andreae COMP :18 Implementing the Shape Interface If you define classes that implement the interface: public class Rectangle implements Shape{ private int x; …… public class TextField implements Shape{ private String text; …… These classes must define all the specified methods (including the right parameters)

© Xiaoying Gao, Peter Andreae COMP :19 MiniDraw class diagram With the Shape Interface: Rectangle render moveBy … Shape Line render moveBy … Oval render moveBy … Dot render moveBy … Tree render moveBy … MiniDraw User Interface Array of Shape