Inheritance & InterfacesThe PlanInheritance MotivationInterface MotivationMotivationExamples from the Video Game PackageFrom the Java API DocumentationPowerPoint PresentationKeyboard.javaSlide 10Slide 11Slide 12Slide 13Mouse.javaSlide 15Slide 16GameLoopGameLoop.javaTrackerTracker.javaTrackerAdapterTrackerAdapter.javaAttractorTracker.javaSlide 24AlarmAlarm.javaTimedKillSpriteTimedSpriteKiller.javaBlurSpriteSlide 30BlurSprite.javaSlide 32Slide 33Practice (from GUIs lecture)CompSci 4 20.1Inheritance & InterfacesInheritance & InterfacesCompSci 4 20.2Inheritance & InterfacesThe PlanMotivate InheritanceMotivate InterfacesExamplesContinue Practice on Observer/ObservableCompSci 4 20.3Inheritance & InterfacesInheritance MotivationInheritance in Java is achieved through extending classesInheritance enables:Code re-useGrouping similar codeFlexibility to customizeCompSci 4 20.4Inheritance & InterfacesInterface MotivationInterfaces in Java are achieved by implementing interfacesBenefits of interfacesCan specify functionality without specifying implementation detailsHelps separate code for more local modificationsAllows incremental developmentCompSci 4 20.5Inheritance & InterfacesMotivationThe 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.CompSci 4 20.6Inheritance & InterfacesExamples from the Video Game PackageKeyboardMouseGameLoopTrackerAttractorTrackerAlarmTimeSpriteKillerBlurSpriteCompSci 4 20.7Inheritance & InterfacesFrom the Java API DocumentationKeyListenerCompSci 4 20.8Inheritance & InterfacesCompSci 4 20.9Inheritance & InterfacesKeyboard.javapackage tipgame;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;/* * @author 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(); } }CompSci 4 20.10Inheritance & InterfacesFrom the Java API DocumentationMouseListenerCompSci 4 20.11Inheritance & InterfacesCompSci 4 20.12Inheritance & InterfacesFrom the Java API DocumentationMouseMotionListenerCompSci 4 20.13Inheritance & InterfacesCompSci 4 20.14Inheritance & InterfacesMouse.javapackage 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. * * @author 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; }CompSci 4 20.15Inheritance & InterfacesMouse.java /**@return the last position of the mouse*/ public Point getMousePosition() { return mousePosition; } /**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 getClickPosition() { Point toReturn=mouseClick; mouseClick=null; return toReturn; } /**@return true if the mouse is currently down, false otherwise*/ public boolean mousePressed() { return mouseDown; }CompSci 4 20.16Inheritance & InterfacesMouse.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; } }CompSci 4 20.17Inheritance & InterfacesGameLoopFrameAdvancer 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.CompSci 4 20.18Inheritance & InterfacesGameLoop.javapublic 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(); }}CompSci 4 20.19Inheritance & InterfacesTrackerUsed with SpritesDescribes general motionlocationsizeorientationCompSci 4 20.20Inheritance & InterfacesTracker.javapackage tipgame;import java.awt.geom.Point2D;/** * * @author Jam */public interface Tracker { Point2D.Double getLocation(); double getScaleFactor(); double getRotationAddition(); void advanceTime(); }CompSci 4 20.21Inheritance & InterfacesTrackerAdapterYou 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 AdapterCompSci 4 20.22Inheritance & InterfacesTrackerAdapter.javapublic 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()
View Full Document