Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Event Handling.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
CS 61B Data Structures and Programming Methodology July David Sun.
CPSC 2100 University of Tennessee at Chattanooga – Spring 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 6: Inheritance and.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance Horstmann ch , 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
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.
CompSci 230 S Software Construction
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Chapter 5 Introduction to Defining Classes
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
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.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
CS Lecture 04 Mice Lynda Thomas
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.
CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Events and Event Handling
CompSci 230 S Programming Techniques
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
Computer Science 209 Graphics and GUIs.
One class is an extension of another.
Inheritance and Abstract Classes
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
One class is an extension of another.
ITEC324 Principle of CS III
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)

Inheritance Start with simple Employee class public class Employee { public Employee(String aName) { name = aName; } public void setSalary(double aSalary) { salary = aSalary; } public String getName() { return name; } public double getSalary() { return salary; } private String name; private double salary; } Manager is a subclass

Sublasses Manager class adds new method: setBonus Manager class overrides existing method: getSalary Adds bonus public class Manager extends Employee { public Manager(String aName) {... } public void setBonus(double aBonus) { bonus = aBonus; } // new method public double getSalary() {... } // overrides Employee method private double bonus; // new field }

Programming: Inheritance hierarchy –General superclass at root of tree –More specific subclasses are children The Substitution Princip –You can use a subclass object whenever a superclass object is expected –System.out.println("salary=" + e.getSalary()); –Can set e to Manager reference –Polymorphism: Correct getSalary method is invoked

Graphic Programming with Inheritance Chapter 4: Create drawings by implementing Icon interface type Now: Form subclass of JComponent public class MyComponent extends JComponent { public void paintComponent(Graphics g) { drawing instructions go here }... } Advantage: Inherit behavior from JComponent Example: Can attach mouse listener to JComponent

Mouse Listeners Attach mouse listener to component Can listen to mouse events (clicks) or mouse motion events public interface MouseListener { void mouseClicked(MouseEvent event); void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); } public interface MouseMotionListener { void mouseMoved(MouseEvent event); void mouseDragged(MouseEvent event); }

The Adapter pattern, ex CarMove Mouse Adapters public class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } Component constructor adds listener: addMouseListener (new MouseAdapter() { public void mousePressed(MouseEvent event) { mouse action goes here } });

Inheritance and Template pattern Scene Editor Draws various shapes User can add, delete, move shapes User selects shape with mouse Selected shape is highlighted (filled in)

The SceneShape Interface Type public interface SceneShape { void setSelected(boolean b); boolean isSelected(); void draw(Graphics2D g2); void drawSelection(Graphics2D g2); void translate(int dx, int dy); boolean contains(Point2D aPoint); }

CarShape and HouseShape Classes public class CarShape implements SceneShape {... public void setSelected(boolean b) { selected = b; } public boolean isSelected() { return selected; } private boolean selected; } public class HouseShape implements SceneShape {... public void setSelected(boolean b) { selected = b; } public boolean isSelected() { return selected; } private boolean selected; }

Abstract Classes Factor out common behavior (setSelected, isSelected) Subclasses inherit common behavior Some methods still undefined (draw, drawSelection, translate, contains)

Template Method drawSelection calls draw Must declare draw in SelectableShape No implementation at that level! Declare as abstract method public abstract void draw(Graphics2D g2) Defined in CarShape, HouseShape drawSelection method calls draw, translate drawSelection doesn't know which methods--polymorphism drawSelection is a template method

TEMPLATE METHOD Pattern Solution Define a superclass that has a method for the algorithm and abstract methods for the primitive operations. Implement the algorithm to call the primitive operations in the appropriate order. Do not define the primitive operations in the superclass, or define them to have appropriate default behavior. Each subclass defines the primitive operations but not the algorithm.

Scene Editor Mouse listener selects/unselects item Mouse motion listener drags item Remove button removes selected items