Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces."— Presentation transcript:

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

2 © Xiaoying Gao, Peter Andreae COMP 102 30: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 emailed.

3 © Xiaoying Gao, Peter Andreae COMP 102 30: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

4 © Xiaoying Gao, Peter Andreae COMP 102 30: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

5 © Xiaoying Gao, Peter Andreae COMP 102 30: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

6 © Xiaoying Gao, Peter Andreae COMP 102 30: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( …

7 © Xiaoying Gao, Peter Andreae COMP 102 30: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)

8 © Xiaoying Gao, Peter Andreae COMP 102 30: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 ”

9 ©Xiaoying Gao, Peter Andreae COMP 102 30: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

10 ©Xiaoying Gao, Peter Andreae COMP 102 30: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); }

11 ©Xiaoying Gao, Peter Andreae COMP 102 30: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.

12 ©Xiaoying Gao, Peter Andreae COMP 102 30: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); }

13 ©Xiaoying Gao, Peter Andreae COMP 102 30: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)); }

14 © Xiaoying Gao, Peter Andreae COMP 102 30: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.

15 © Xiaoying Gao, Peter Andreae COMP 102 30: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

16 © Xiaoying Gao, Peter Andreae COMP 102 30: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 :

17 © Xiaoying Gao, Peter Andreae COMP 102 30: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

18 © Xiaoying Gao, Peter Andreae COMP 102 30: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)

19 © Xiaoying Gao, Peter Andreae COMP 102 30: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


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

Similar presentations


Ads by Google