DOC PREVIEW
Duke CPS 004 - Inheritance & Interfaces

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 PlanMotivate InheritanceMotivate InterfacesExamplesContinue Practice on Observer/ObservableCompSci 4 20.3Inheritance & InterfacesInheritance MotivationInheritance in Java is achieved through extending classesInheritance enables:Code re-useGrouping similar codeFlexibility to customizeCompSci 4 20.4Inheritance & InterfacesInterface MotivationInterfaces in Java are achieved by implementing interfacesBenefits of interfacesCan specify functionality without specifying implementation detailsHelps separate code for more local modificationsAllows 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 PackageKeyboardMouseGameLoopTrackerAttractorTrackerAlarmTimeSpriteKillerBlurSpriteCompSci 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 & InterfacesGameLoop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.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 & InterfacesTrackerUsed with SpritesDescribes general motionlocationsizeorientationCompSci 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 & InterfacesTrackerAdapter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 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

Duke CPS 004 - Inheritance & Interfaces

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Inheritance & Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance & Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance & Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?