Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
CS18000: Problem Solving and Object-Oriented Programming.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Chapter 121 Window Interfaces Using Swing Chapter 12.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
UID – Event Handling and Listeners Boriana Koleva
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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,
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Quick topix 11/7/13 CSE 1102 Fall previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Interfaces.
CSE 331 Software Design and Implementation
Ellen Walker Hiram College
CSE 331 Software Design and Implementation
Events, Event Handlers, and Threads
Border Layout, using Panels, Introduction to PinBallGame
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface, revisited Event adapters Inner classes

Abstract classes Abstract classes are ones for which the implementation details must be filled in later, in subclasses

Interfaces Unlike C++, Java does not allow “multiple inheritance”. A class can only have one superclass. Interfaces are a way to provide a kind of multiple inheritance without the complexity of C++. When a class implements Comparable, we know we can use the compareTo method A class can implement several interfaces.

The Cloneable interface This interface is empty! It does not specify any methods or constants. It is called a tag interface or marker interface. A class must implement the Cloneable interface if it overrides the clone() method in the Object class. When clone() is called at run time, an exception occurs if the implicit parameter object is of a class that does not implement the Cloneable interface.

Overriding clone() in the Date class First, Date must specify that it implements the Cloneable interface Then, Date must provide the clone() method. Simplest way is to construct a new Date and copy the contents of the fields explicitly. Alternatively, can invoke super.clone() but then need to catch the CloneNotSupported exception – skip for now Anyway, how does the super method in the Object class know about the fields in Date()? How can it make a cloned copy? Answer: the Object clone method is a native method. It is not written in Java, but is implemented in the JVM for the native platform (machine). It works by simply copying chunks of memory.

Shallow and deep copies Suppose Date has another field which is actually an object reference If Date’s implementation of clone() just copies the object reference, the cloned copy will reference the same object, not a copy This is called a shallow copy If this object is immutable, such as a String, this is fine But if it’s mutable, it means that if the object is later changed in the cloned copy, it will also change in the original copy. In this situation, it’s better to make a deep copy by also cloning the object field. Since the Object clone() does not do this, this must be done in the overriding method

ActionListener interface extends EventListener interface, which is a tag interface specifies only one method: –public void actionPerformed(ActionEvent e)

Event handling – 3 ingredients The event class, such as ActionEvent, provided by Java library The listener class, such as our class YesListener that implements the ActionListener interface in the Java library The event source, such as a JButton object where the user clicks on the mouse: YesListener must be added to (registered with) the button by calling the method addActionListener

Inner classes If the listener wants to do anything interesting, it needs to access the instance variables of the class that calls it - but these are typically private (or local variables, as in our FrameDemo example from last time) Therefore, we put the listener in an inner class. Anonymous inner classes: we don’t need to give the name YesListener to the inner class, but add it directly to the button anonymously (see p.471) –advantage: shorter to write –disadvantage: potentially confusing

Returning to FrameDemo Let’s change it so that the listener writes in the JTextField associated with the button that was clicked, instead of calling System.out.println Should be possible since each yes button has its own listener But how, since the body of actionPerformed cannot distinguish which button invoked it? What can actionPerformed access? It cannot access local variables It can access data fields of the class that it is inside Thus, let’s introduce a class YesNoSet, with data fields yesButton, noButton and text, that contains YesListener as an inner class Then we can construct YesNoSet objects inside the double loop and, for each one, add its buttons and text field object to the frame window

The MouseListener interface This specifies five abstract methods –mouseClicked –mouseEntered –mouseExited –mousePressed –mouseReleased Each returns void and has an explicit parameter of type MouseEvent MouseEvent and ActionEvent are both descended from AWTEvent, but MouseEvent is much further down the inheritance tree

Event adapters Suppose we want to write a listener that implements mouseEntered but none of the other methods Any class that implements the MouseListener interface must implement all the methods However, there is a MouseAdapter class in the library that already implements all the methods as do- nothing methods We can then extend this class by overriding only the methods that we want to do something For example, we can write a class MouseEnteredListener that extends MouseAdapter and overrides only mouseEntered

Panels JPanel panel = new JPanel() Good for organizing: can add buttons, etc., to a panel and panels to a frame window Also good for graphics…

Graphics Chapter 13 Easiest way is to draw on panels class drawPanel extends JPanel{ protected void paintComponent(Graphics g){ super.paintComponent(g); g.drawLine(0,0,50,50); count++; System.out.println("drawing, count is "+ count); } } We don’t call paintComponent: the system does when it needs to redraw the graphics window

HW 7 Please write something that challenges you but is also fun Details completely up to you Don’t forget about making sure user interface is clear, and there are plenty of comments