Inheritance Interfaces Inheritance Interfaces Inheritance Interfaces The Plan

  • Slides: 36
Download presentation
Inheritance & Interfaces

Inheritance & Interfaces

Inheritance & Interfaces

Inheritance & Interfaces

Inheritance & Interfaces

Inheritance & Interfaces

The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on

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

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

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

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 – Game. Loop – Tracker

Examples from the Video Game Package Keyboard – Mouse – Game. Loop – Tracker – Attractor. Tracker – Alarm – Time. Sprite. Killer – Blur. Sprite –

From the Java API Documentation Key. Listener

From the Java API Documentation Key. Listener

Keyboard. java package tipgame; import java. awt. event. Key. Event; import java. awt. event.

Keyboard. java package tipgame; import java. awt. event. Key. Event; import java. awt. event. Key. Listener; /* * @author Jam Jenkins */ public class Keyboard implements Key. Listener { private char key; private boolean key. Down; */ public char get. Last. Key() { return key; } public void key. Pressed(Key. Event e) { key=e. get. Key. Char(); key. Down=true; } /** Creates a new instance of Keyboard public void key. Released(Key. Event e) { key=e. get. Key. Char(); key. Down=false; } public Keyboard() { clear(); } public void clear() { key. Down=false; key=Key. Event. CHAR_UNDEFINED; } public char consume. Key() { char temp=key; key=Key. Event. CHAR_UNDEFINED; return temp; } public void key. Typed(Key. Event e) { key=e. get. Key. Char(); } }

From the Java API Documentation Mouse. Listener

From the Java API Documentation Mouse. Listener

From the Java API Documentation Mouse. Motion. Listener

From the Java API Documentation Mouse. Motion. Listener

Mouse. java package tipgame; import java. awt. Point; import java. awt. event. Mouse. Listener;

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

Mouse. java /**@return the last position of the mouse*/ public Point get. Mouse. Position()

Mouse. java /**@return the last position of the mouse*/ public Point get. Mouse. Position() { return mouse. Position; } /**determines the last position of a click and once called, clears that click @return the last clicked position, null if not clicked since last call*/ public Point get. Click. Position() { Point to. Return=mouse. Click; mouse. Click=null; return to. Return; } /**@return true if the mouse is currently down, false otherwise*/ public boolean mouse. Pressed() { return mouse. Down; }

Mouse. java public void mouse. Clicked(java. awt. event. Mouse. Event mouse. Event) { mouse.

Mouse. java public void mouse. Clicked(java. awt. event. Mouse. Event mouse. Event) { mouse. Click=mouse. Event. get. Point(); } public void mouse. Dragged(java. awt. event. Mouse. Event mouse. Event) { mouse. Position=mouse. Event. get. Point(); } public void mouse. Entered(java. awt. event. Mouse. Event mouse. Event) { } public void mouse. Exited(java. awt. event. Mouse. Event mouse. Event) { } public void mouse. Moved(java. awt. event. Mouse. Event mouse. Event) { mouse. Position=mouse. Event. get. Point(); } public void mouse. Pressed(java. awt. event. Mouse. Event mouse. Event) { mouse. Down=true; } public void mouse. Released(java. awt. event. Mouse. Event mouse. Event) { mouse. Down=false; } }

Game. Loop ● Frame. Advancer animates the Animation. Canvas. Game. Loop extends Frame. Advancer

Game. Loop ● Frame. Advancer animates the Animation. Canvas. Game. Loop extends Frame. Advancer by adding a Keyboard and a Mouse as Listeners to the Animation. Canvas. ● Game. Loop can respond to user interaction while Frame Advancer does not. ●

Game. Loop. java public class Game. Loop extends Frame. Advancer { private JPanel canvas.

Game. Loop. java public class Game. Loop extends Frame. Advancer { private JPanel canvas. Panel; protected Mouse mouse; protected Keyboard keyboard; public Game. Loop() { super(); mouse=new Mouse(); keyboard=new Keyboard(); canvas. add. Mouse. Listener(mouse); canvas. add. Mouse. Motion. Listener(mouse); canvas. add. Key. Listener(keyboard); canvas. Panel=new JPanel(new Border. Layout()); canvas. Panel. add(canvas); } /**used to put the canvas into a GUI*/ public Animation. Canvas get. Canvas() { return canvas; } } public void start() { keyboard. clear(); mouse. clear(); super. start(); }

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

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

Tracker. java package tipgame; import java. awt. geom. Point 2 D; /** * *

Tracker. java package tipgame; import java. awt. geom. Point 2 D; /** * * @author Jam */ public interface Tracker { Point 2 D. Double get. Location(); double get. Scale. Factor(); double get. Rotation. Addition(); } void advance. Time();

Tracker. Adapter You don't want to write all the methods needed to implement an

Tracker. Adapter 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

Tracker. Adapter. java public abstract class Tracker. Adapter implements Tracker { public Point 2

Tracker. Adapter. java public abstract class Tracker. Adapter implements Tracker { public Point 2 D. Double get. Location() { return new Point 2 D. Double(); } public double get. Scale. Factor() { return 1; } public double get. Rotation. Addition() { return 0; } public void advance. Time() { advance. Time(Game. Loop. time. Interval); } public abstract void advance. Time(double time); }

Attractor. Tracker. java public class Attractor. Tracker extends Tracker. Adapter { double speed; Sprite

Attractor. Tracker. java public class Attractor. Tracker extends Tracker. Adapter { double speed; Sprite moving; Sprite toward; Point 2 D. Double next. Location; public Attractor. Tracker(Sprite from, Sprite to, double rate) { moving=from; toward=to; speed=rate; next. Location=new Point 2 D. Double(); } public Sprite get. Target() { return toward; }

Attractor. Tracker. java public Point 2 D. Double get. Location() { return next. Location;

Attractor. Tracker. java public Point 2 D. Double get. Location() { return next. Location; } public void advance. Time(double time) { Point 2 D. Double from=moving. get. Location(); Point 2 D. Double to=toward. get. Location(); next. Location. x=to. x-from. x; next. Location. y=to. y-from. y; double factor=speed*time/from. distance(to); next. Location. x*=factor; next. Location. y*=factor; next. Location. x+=from. x; next. Location. y+=from. y; } }

Alarm Used to signal timing events Used with Frame. Advancer

Alarm Used to signal timing events Used with Frame. Advancer

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

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

Timed. Kill. Sprite Used to get rid of a Sprite after a given period

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

Timed. Sprite. Killer. java public class Timed. Sprite. Killer implements Alarm { Sprite sprite;

Timed. Sprite. Killer. java public class Timed. Sprite. Killer implements Alarm { Sprite sprite; public Timed. Sprite. Killer(Sprite s) { sprite=s; } public void set. Kill. Time(double delay) { Game. Loop. schedule. Relative(this, delay); } public void alarm() { sprite. kill(); } }

Blur. Sprite

Blur. Sprite

Blur. Sprite ● Extends Sprite ● Draws history of Sprite motion Can augment any

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

Blur. Sprite. java public class Blur. Sprite extends Sprite { private Linked. List previous=new

Blur. Sprite. java public class Blur. Sprite extends Sprite { private Linked. List previous=new Linked. List(); private static int DEFAULT_PER_FRAME=3; private static int DEFAULT_FRAMES=2; private int num. Frames=DEFAULT_FRAMES; private int num. Per. Frame=DEFAULT_PER_FRAME; private int draw. Number=0; public void set. Enabled(boolean enabled) { super. set. Enabled(enabled); if(enabled==true) { previous. clear(); } } public void set. History(int frames, int per. Frame) { num. Frames=frames; num. Per. Frame=per. Frame; }

Blur. Sprite. java public void update() { if(!is. Enabled()) return; super. update(); draw. Number++;

Blur. Sprite. java public void update() { if(!is. Enabled()) return; super. update(); draw. Number++; int frequency=Math. max(1, Frame. Advancer. get. Updates. Per. Frame()/num. Per. Frame); if(draw. Number%frequency!=0) { return; } boolean bounding=get. Use. Bounding. Box(); set. Use. Bounding. Box(false); previous. add. Last(get. Shape()); set. Use. Bounding. Box(bounding); if(previous. size()>num. Frames*num. Per. Frame) { previous. remove. First(); } }

Blur. Sprite. java public void paint(Graphics 2 D brush) { if(!is. Enabled()) return; brush.

Blur. Sprite. java public void paint(Graphics 2 D brush) { if(!is. Enabled()) return; brush. set. Color(get. Color()); Path. Blur. paint. Exponential. Blur(brush, (Shape[])previous. to. Array(new Shape[0]), 2); } }

Practice (from GUIs lecture) Write a program to count the number of clicks on

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: Click. Count – keeps three integer instance variables to count the number of clicks (hint: extend Observable) ●Click. Count. Panel – observes Click. Count for changes and updates its components when an update occurs (hint: implement Observer) ●Click. GUI – contains three buttons and the count panel. Clicking on the buttons registers the click via Click. Count. ● Look at sample programs from previous lecture (the GUIs lecture and GUIs for Video Games) to get you started.