Inheritance Interfaces Comp Sci 4 Inheritance Interfaces 20

  • Slides: 34
Download presentation
Inheritance & Interfaces Comp. Sci 4 Inheritance & Interfaces 20. 1

Inheritance & Interfaces Comp. Sci 4 Inheritance & Interfaces 20. 1

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

The Plan v Motivate Inheritance v Motivate Interfaces v Examples v Continue Practice on Observer/Observable Comp. Sci 4 Inheritance & Interfaces 20. 2

Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: v Code

Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: v Code re-use v Grouping similar code v Flexibility to customize Comp. Sci 4 Inheritance & Interfaces 20. 3

Interface Motivation v Interfaces in Java are achieved by implementing interfaces Benefits of interfaces

Interface Motivation v Interfaces in Java are achieved by implementing interfaces Benefits of interfaces v Can specify functionality without specifying implementation details v Helps separate code for more local modifications v Allows incremental development Comp. Sci 4 Inheritance & Interfaces 20. 4

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. Comp. Sci 4 Inheritance & Interfaces 20. 5

Examples from the Video Game Package v v v v Keyboard Mouse Game. Loop

Examples from the Video Game Package v v v v Keyboard Mouse Game. Loop Tracker Attractor. Tracker Alarm Time. Sprite. Killer Blur. Sprite Comp. Sci 4 Inheritance & Interfaces 20. 6

From the Java API Documentation Key. Listener Comp. Sci 4 Inheritance & Interfaces 20.

From the Java API Documentation Key. Listener Comp. Sci 4 Inheritance & Interfaces 20. 7

Comp. Sci 4 Inheritance & Interfaces 20. 8

Comp. Sci 4 Inheritance & Interfaces 20. 8

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

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; Keyboard. java 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 Keyboard() { clear(); } public void key. Released(Key. Event e) { key=e. get. Key. Char(); key. Down=false; } 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; } Comp. Sci 4 public void key. Typed(Key. Event e) { key=e. get. Key. Char(); } } Inheritance & Interfaces 20. 9

From the Java API Documentation Mouse. Listener Comp. Sci 4 Inheritance & Interfaces 20.

From the Java API Documentation Mouse. Listener Comp. Sci 4 Inheritance & Interfaces 20. 10

Comp. Sci 4 Inheritance & Interfaces 20. 11

Comp. Sci 4 Inheritance & Interfaces 20. 11

From the Java API Documentation Mouse. Motion. Listener Comp. Sci 4 Inheritance & Interfaces

From the Java API Documentation Mouse. Motion. Listener Comp. Sci 4 Inheritance & Interfaces 20. 12

Comp. Sci 4 Inheritance & Interfaces 20. 13

Comp. Sci 4 Inheritance & Interfaces 20. 13

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; } Comp. Sci 4 Inheritance & Interfaces 20. 14

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; } Comp. Sci 4 Inheritance & Interfaces 20. 15

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; } } Comp. Sci 4 Inheritance & Interfaces 20. 16

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

Game. Loop v v v 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. Comp. Sci 4 Inheritance & Interfaces 20. 17

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; } } Comp. Sci 4 public void start() { keyboard. clear(); mouse. clear(); super. start(); } Inheritance & Interfaces 20. 18

Tracker v v Used with Sprites Describes general motion q q q location size

Tracker v v Used with Sprites Describes general motion q q q location size orientation Comp. Sci 4 Inheritance & Interfaces 20. 19

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(); } Comp. Sci 4 void advance. Time(); Inheritance & Interfaces 20. 20

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

Tracker. Adapter v v v 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 Comp. Sci 4 Inheritance & Interfaces 20. 21

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); } Comp. Sci 4 Inheritance & Interfaces 20. 22

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; } Comp. Sci 4 Inheritance & Interfaces 20. 23

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; } } Comp. Sci 4 Inheritance & Interfaces 20. 24

Alarm Used to signal timing events Used with Frame. Advancer Comp. Sci 4 Inheritance

Alarm Used to signal timing events Used with Frame. Advancer Comp. Sci 4 Inheritance & Interfaces 20. 25

Alarm. java public interface Alarm { public void alarm(); } Comp. Sci 4 Inheritance

Alarm. java public interface Alarm { public void alarm(); } Comp. Sci 4 Inheritance & Interfaces 20. 26

Timed. Kill. Sprite v v v Used to get rid of a Sprite after

Timed. Kill. Sprite v v v Used to get rid of a Sprite after a given period of time. Can be used for splash screens Has other uses as well Comp. Sci 4 Inheritance & Interfaces 20. 27

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(); } } Comp. Sci 4 Inheritance & Interfaces 20. 28

Blur. Sprite Comp. Sci 4 Inheritance & Interfaces 20. 29

Blur. Sprite Comp. Sci 4 Inheritance & Interfaces 20. 29

Blur. Sprite v v v Extends Sprite Draws history of Sprite motion Can augment

Blur. Sprite v v v Extends Sprite Draws history of Sprite motion Can augment any Sprite using a Shape (i. e. any Sprite except Image. Sprite) Comp. Sci 4 Inheritance & Interfaces 20. 30

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; } Comp. Sci 4 Inheritance & Interfaces 20. 31

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(); } } Comp. Sci 4 Inheritance & Interfaces 20. 32

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); } } Comp. Sci 4 Inheritance & Interfaces 20. 33

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. Comp. Sci 4 Inheritance & Interfaces 20. 34