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.
Graphical User Interfaces
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Design Patterns and Graphical User Interfaces Horstmann ,
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,
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
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.
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.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Raw GUIs, ArrayLists Inheritance.
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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 Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
Graphical User Interfaces
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
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 UIButtonListener revisited Optional: GUI without ecs100 library Assignment 9 The model solutions for exercise questions are handed out

©Xiaoying Gao, Peter Andreae COMP :3 DiskGame class structure RoundDisk damage explode … Disk FatDisk damage explode … BombDisk damage explode … BouncyDisk damage explode … DiskGame User Interface Array of Disk

© Peter Andreae COMP :4 A Disk Interface class. public interface Disk { public void damage(); public boolean on(int x, int y); public void explode(); public double score(); public boolean withinRange(Disk other); } Declares a type that you can use for fields,variables, arrays: public class DiskGame { private int maxDisks = 20; private Disk [ ] disks = new Disk [maxDisks]; public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ Disk disk = this.disks[i]; if (disk != null && disk.on(x, y) ) { disk.damage(); Java will NOT complain! Disks DO have these methods Note the ; instead of { …. }

© Peter Andreae COMP :5 More classes implementing Disk public class FatDisk implements Disk { private int x, y; private int layers = 5; public void damage( ){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } } public class BombDisk implements Disk { private int x, y, maxRad; private int radius = 10; public void damage(){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } }

© Xiaoying Gao, Peter Andreae COMP :6 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 Disk(…) // 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 :7 Interface example public interface Tutor { public void printPayment(); } public class TutorAdmin{ private ArrayList tutors = new ArrayList (); public TutorAdmin(){ this.tutors.add(new SeniorTutor("Alan")); this.tutors.add(new StudentTutor("Sam", 5)); this.tutors.add(new StudentTutor("Tom", 7)); this.tutors.add(new SeniorTutor("Bob")); for(Tutor t: tutors) t.printPayment(); }

© Xiaoying Gao, Peter Andreae COMP :8 The SeniorTutor and StudentTutor class public class SeniorTutor implements Tutor{ private String name; private double salary = 2000; public SeniorTutor(String c){ this.name = c; } public void printPayment(){ UI.println("pay fortnightly: $" +salary); } public class StudentTutor implementsTutor { private String name; private double hours; public StudentTutor(String s, double h){ this.name = s; this.hours = h; } public void printPayment(){ UI.println("pay for "+ this.hours+ ": $"+ this.hours*20); } public void addHours(double n){ this.hours = this.hours + n; }

© Xiaoying Gao, Peter Andreae COMP :9 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 Disk, Tutor, UIButtonListener, UIMouseListener, ButtonListener, … interface type may encompass several different object types

© Xiaoying Gao, Peter Andreae COMP :10 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 buttonPerformed( …

© Xiaoying Gao, Peter Andreae COMP :11 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 :12 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 bottonPerformed( … Quit Button-5 name: aListener: : “ Quit ”

©Xiaoying Gao, Peter Andreae COMP :13 Standard library (without ecs100 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 :14 Doing without UI & ecs100 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 actionPerformed method Writing to the text area is different.

©Xiaoying Gao, Peter Andreae COMP :15 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 :16 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 :17 Doing without UI & ecs100 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 :18 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 :19 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 :20 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 :21 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 :22 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 :23 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