Inheritance & Interfaces. The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on Observer/Observable.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

 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.
CompSci 427jd.1 Javadoc. CompSci 427jd.2 Javadoc The Plan  What is Javadoc?  Writing Javadoc comments  Using the Javadoc tool  Practice.
– Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory.
GUI Programming in Java
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
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,
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
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.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
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.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Iteration 1 Looping Structures. Iteration 2 The Plan While not everyone understands: 1.Motivate loops 2.For loops 3.While loops 4.Do-while loops 5.Equivalence.
CompSci Introduction to Jam’s Video Game Package.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
CS Lecture 04 Mice Lynda Thomas
CompSci Video Game Package Design Design of the Video Game Package.
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.
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.
Video Game Package Intro 1 Last Edited 1/10/04CPS4: Java for Video Games Introduction.
CompSci 44.1 Game Package Introduction to Jam’s Video Game Package.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
CompSci Inheritance & Interfaces. CompSci Inheritance & Interfaces The Plan  Motivate Inheritance  Motivate Interfaces  Examples  Continue.
Events and Event Handling
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
GUIs for Video Games.
Interfaces.
JavaDoc CECS277 Mimi Opkins.
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Introduction to Computing Using Java
null, true, and false are also reserved.
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Presentation transcript:

Inheritance & Interfaces

The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on Observer/Observable

Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: ● Code re-use ● Grouping similar code ● Flexibility to customize

Interface Motivation ● Interfaces in Java are achieved by implementing interfaces Benefits of interfaces ● Can specify functionality without specifying implementation details ● Helps separate code for more local modifications ● Allows incremental development

Motivation The real motivation is that inheritance and interfaces were used extensively in developing the video gaming package. Without these two, the code would be much more difficult to modify, extend, and use.

Examples from the Video Game Package – Keyboard – Mouse – GameLoop – Tracker – AttractorTracker – Alarm – TimeSpriteKiller – BlurSprite

From the Java API Documentation KeyListener

Keyboard.java package tipgame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; /* Jam Jenkins */ public class Keyboard implements KeyListener { private char key; private boolean keyDown; /** Creates a new instance of Keyboard */ public Keyboard() { clear(); } public void clear() { keyDown=false; key=KeyEvent.CHAR_UNDEFINED; } public char consumeKey() { char temp=key; key=KeyEvent.CHAR_UNDEFINED; return temp; } public char getLastKey() { return key; } public void keyPressed(KeyEvent e) { key=e.getKeyChar(); keyDown=true; } public void keyReleased(KeyEvent e) { key=e.getKeyChar(); keyDown=false; } public void keyTyped(KeyEvent e) { key=e.getKeyChar(); } }

From the Java API Documentation MouseListener

From the Java API Documentation MouseMotionListener

Mouse.java package tipgame; import java.awt.Point; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; /**This class uses polling as opposed to event based methods for determining *mouse positions and actions. * Jam Jenkins */ public class Mouse implements MouseListener, MouseMotionListener { private Point mousePosition; private Point mouseClick; private boolean mouseDown; /** Creates a new instance of Mouse */ public Mouse() { mouseDown=false; } public void clear() { mousePosition=null; mouseClick=null; mouseDown=false; }

Mouse.java the last position of the mouse*/ public Point getMousePosition() { return mousePosition; } /**determines the last position of a click and once called, clears that the last clicked position, null if not clicked since last call*/ public Point getClickPosition() { Point toReturn=mouseClick; mouseClick=null; return toReturn; } true if the mouse is currently down, false otherwise*/ public boolean mousePressed() { return mouseDown; }

Mouse.java public void mouseClicked(java.awt.event.MouseEvent mouseEvent) { mouseClick=mouseEvent.getPoint(); } public void mouseDragged(java.awt.event.MouseEvent mouseEvent) { mousePosition=mouseEvent.getPoint(); } public void mouseEntered(java.awt.event.MouseEvent mouseEvent) { } public void mouseExited(java.awt.event.MouseEvent mouseEvent) { } public void mouseMoved(java.awt.event.MouseEvent mouseEvent) { mousePosition=mouseEvent.getPoint(); } public void mousePressed(java.awt.event.MouseEvent mouseEvent) { mouseDown=true; } public void mouseReleased(java.awt.event.MouseEvent mouseEvent) { mouseDown=false; } }

GameLoop ● FrameAdvancer animates the AnimationCanvas. ● GameLoop extends FrameAdvancer by adding a Keyboard and a Mouse as Listeners to the AnimationCanvas. ● GameLoop can respond to user interaction while Frame Advancer does not.

GameLoop.java public class GameLoop extends FrameAdvancer { private JPanel canvasPanel; protected Mouse mouse; protected Keyboard keyboard; public GameLoop() { super(); mouse=new Mouse(); keyboard=new Keyboard(); canvas.addMouseListener(mouse); canvas.addMouseMotionListener(mouse); canvas.addKeyListener(keyboard); canvasPanel=new JPanel(new BorderLayout()); canvasPanel.add(canvas); } /**used to put the canvas into a GUI*/ public AnimationCanvas getCanvas() { return canvas; } public void start() { keyboard.clear(); mouse.clear(); super.start(); }

Tracker ● Used with Sprites ● Describes general motion – location – size – orientation

Tracker.java package tipgame; import java.awt.geom.Point2D; /** * Jam */ public interface Tracker { Point2D.Double getLocation(); double getScaleFactor(); double getRotationAddition(); void advanceTime(); }

TrackerAdapter ● You don't want to write all the methods needed to implement an interface. ● The default implementations of the methods in interfaces exist. The Solution: ● Extend an Adapter

TrackerAdapter.java public abstract class TrackerAdapter implements Tracker { public Point2D.Double getLocation() { return new Point2D.Double(); } public double getScaleFactor() { return 1; } public double getRotationAddition() { return 0; } public void advanceTime() { advanceTime(GameLoop.timeInterval); } public abstract void advanceTime(double time); }

AttractorTracker.java public class AttractorTracker extends TrackerAdapter { double speed; Sprite moving; Sprite toward; Point2D.Double nextLocation; public AttractorTracker(Sprite from, Sprite to, double rate) { moving=from; toward=to; speed=rate; nextLocation=new Point2D.Double(); } public Sprite getTarget() { return toward; }

AttractorTracker.java public Point2D.Double getLocation() { return nextLocation; } public void advanceTime(double time) { Point2D.Double from=moving.getLocation(); Point2D.Double to=toward.getLocation(); nextLocation.x=to.x-from.x; nextLocation.y=to.y-from.y; double factor=speed*time/from.distance(to); nextLocation.x*=factor; nextLocation.y*=factor; nextLocation.x+=from.x; nextLocation.y+=from.y; }

Alarm Used to signal timing events Used with FrameAdvancer

Alarm.java public interface Alarm { public void alarm(); }

TimedKillSprite ● Used to get rid of a Sprite after a given period of time. ● Can be used for splash screens ● Has other uses as well

TimedSpriteKiller.java public class TimedSpriteKiller implements Alarm { Sprite sprite; public TimedSpriteKiller(Sprite s) { sprite=s; } public void setKillTime(double delay) { GameLoop.scheduleRelative(this, delay); } public void alarm() { sprite.kill(); }

BlurSprite

● Extends Sprite ● Draws history of Sprite motion ● Can augment any Sprite using a Shape (i.e. any Sprite except ImageSprite)

BlurSprite.java public class BlurSprite extends Sprite { private LinkedList previous=new LinkedList(); private static int DEFAULT_PER_FRAME=3; private static int DEFAULT_FRAMES=2; private int numFrames=DEFAULT_FRAMES; private int numPerFrame=DEFAULT_PER_FRAME; private int drawNumber=0; public void setEnabled(boolean enabled) { super.setEnabled(enabled); if(enabled==true) { previous.clear(); } public void setHistory(int frames, int perFrame) { numFrames=frames; numPerFrame=perFrame; }

BlurSprite.java public void update() { if(!isEnabled()) return; super.update(); drawNumber++; int frequency=Math.max(1, FrameAdvancer.getUpdatesPerFrame()/numPerFrame); if(drawNumber%frequency!=0) { return; } boolean bounding=getUseBoundingBox(); setUseBoundingBox(false); previous.addLast(getShape()); setUseBoundingBox(bounding); if(previous.size()>numFrames*numPerFrame) { previous.removeFirst(); }

BlurSprite.java public void paint(Graphics2D brush) { if(!isEnabled()) return; brush.setColor(getColor()); PathBlur.paintExponentialBlur(brush, (Shape[])previous.toArray(new Shape[0]), 2); }

Practice (from GUIs lecture) Write a program to count the number of clicks on a yes, no and maybe button. To do this, write three classes: ● ClickCount – keeps three integer instance variables to count the number of clicks (hint: extend Observable) ● ClickCountPanel – observes ClickCount for changes and updates its components when an update occurs (hint: implement Observer) ● ClickGUI – contains three buttons and the count panel. Clicking on the buttons registers the click via ClickCount. Look at sample programs from previous lecture (the GUIs lecture and GUIs for Video Games) to get you started.