ECE 462 Object-Oriented Programming using C++ and Java. Lecture 17 Yung-Hsiang Lu

Similar documents
For programs go to You wil need apache ant to build some of these -- download it at

ECE 462 Object-Oriented Programming using C++ and Java. Flickering and Double Buffering

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

PESIT Bangalore South Campus

Graphical User Interfaces 2

Computer Games Development Spring Practical 3 GameCore, Transforms & Collision Detection

AP Computer Science Unit 13. Still More Graphics and Animation.

Graphical User Interfaces 2

Avoid Common Pitfalls when Programming 2D Graphics in Java: Lessons Learnt from Implementing the Minueto Toolkit

G51PRG: Introduction to Programming Second semester Applets and graphics

Programmierpraktikum

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

Programming Languages and Techniques (CIS120)

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Science and Engineering

GUI in Java TalentHome Solutions

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

IT101. Graphical User Interface

Letterkenny Institute of Technology

Chapter 1 GUI Applications

Lecture 3: Java Graphics & Events

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

EE219 Object Oriented Programming I (2005/2006) SEMESTER 1 SOLUTIONS

User interfaces and Swing

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

ISO-2022-JP (JIS ) 1 2. (Windows95/98 MacOS ) Java UNICODE UNICODE. Java. .java.java.txt native2ascii. .java

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

H212 Introduction to Software Systems Honors

AP CS Unit 12: Drawing and Mouse Events

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

Building a Java First-Person Shooter

Swing Rocks - A Tribute to Filthy Rich Clients

Smoother Graphics Taking Control of Painting the Screen

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Building a Java First-Person Shooter

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

News! Feedback after Lecture 4! About Java and OOP!

CS 134 Test Program #2

Programming Languages and Techniques (CIS120)

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Computer Science 210: Data Structures. Intro to Java Graphics

Lecture 5: Java Graphics

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

ECE 462 Object-Oriented Programming using C++ and Java. Testing

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Platform Games Drawing Sprites & Detecting Collisions

2. (10 pts) Which of the following classes implements the Shape interface? Circle all that apply. a) Ellipse2D b) Area c) Stroke d) AffineTransform e)

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Windows and Events. created originally by Brian Bailey

GUI Event Handlers (Part II)

Programming Languages and Techniques (CIS120)

CS 251 Intermediate Programming GUIs: Event Listeners

UCLA PIC 20A Java Programming

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

The AWT Event Model 9

CT 229 Arrays in Java

Creating Breakout - Part 2

CSIS 10A Assignment 14 SOLUTIONS

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

ECE 462 Object-Oriented Programming using C++ and Java. Graphical User Interface

Introduction to Unreal Engine Blueprints for Beginners. By Chaven R Yenketswamy

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Programming Languages and Techniques (CIS120)

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Handling Mouse and Keyboard Events

Programming Languages and Techniques (CIS120)

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

The init() Method. Browser Calling Applet Methods

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

2IS45 Programming

NITI NITI I PRIORITET

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Side Trip into Java: Enumeration Types. CS61B Lecture #36. Making Enumerals Available Elsewhere. Enum Types in Java. import static checkers.piece.

CS61B Lecture #36. DSIJ, Chapter 10, HFJ, pp Today: A Brief Side Trip: Enumeration types.

L4,5: Java Overview II

Java for Interfaces and Networks (DT3010, HT10)

Graphical User Interface (GUI)

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Introduction to the JAVA UI classes Advanced HCI IAT351

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

CS 180 Problem Solving and Object Oriented Programming Fall 2011

CS108, Stanford Handout #22. Thread 3 GUI

Advanced Java Programming (17625) Event Handling. 20 Marks

1 Getting started with Processing

Advanced Internet Programming CSY3020

Fruit Snake SECTION 1

The Nervous Shapes Example

Profiling & Optimization

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Applet which displays a simulated trackball in the upper half of its window.

The AWT Package, Graphics- Introduction to Images

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Index. Animation. factors, 186. Abstract Window Toolkit (AWT), 111. frame rate, 185. Access modifiers. multiple objects. package-private, 27

Transcription:

ECE 462 Object-Oriented Programming using C++ and Java Lecture 17 Yung-Hsiang Lu yunglu@purdue.edu

Program Performance Why is performance important? Users expect more and better features programs are more and more complex Response time can determine market success if your program is too slow, you can lose customers. Programs are ported to resource-limited systems, such as cellular phones. Processor speed stops improving, in terms of the clock speed. New processors have more cores. What should we do? Algorithmic improvement ECE368 Programming techniques understand different choices week 10 2

How to Improve Performance use compiler optimization, g++ -O and javac O avoid IO: print to screen, save to disk, send to / receive from network avoid creating / destroying objects, pass by reference reduce the overhead in checking error conditions if (common case) { handle the case else { if (error1) { handle error case 1 if (error2) { handle error case 2 if (error3)... week 10 3

Virtual Function Does a virtual function take longer than a non-virtual function? Yes Does it take longer for a virtual function deeper in class hierarchy? class C1 { virtual void func() {... class C2: public C1 { virtual void func() {... class C3: public C2 { virtual void func() {... C1 * c1obj = new C1; c1obj -> func(); C1 * c13obj = new C3; c13obj -> func(); C3 * c3obj = new C3; c3obj -> func(); Do they take the same amount of time? Yes week 10 4

Performance Cost of Multiple Inheritance Does it take longer to call a virtual function in MultiDerived than in Derived1 or in Derived2? No Derived1 CommonBase Derived2 MultiDerived week 10 5

Avoid Temporary Objects Are these two code segments the same? All are string objects. s6 = s1 + s2 + s3 + s4 + s5; s7 = s1; s7 += s2; s7 += s3; s7 += s4; s7 += s5; Yes, in functionality (s6 == s7) No, in performance. Which one is faster? For s6 = s1 + s2 + s3 + s4 + s5, the compiler has to create a temporary object t1 to hold the result of s1 + s2, another temporary object t2 for t1 + s3, another object t3 for t2 + s4... It is faster to obtain s7. week 10 6

Allocate Memory First If an operation requires frequent memory allocation / release, try to calculate the needed amount and allocate it right away. snew = s1; snew += s2; snew += s3; snew += s4; snew += s5;... snew.resize(s1.length() + s2.length() +...); index = 0; snew.replace(index, s1.length(), s1); index += s1.length(); snew.replace(index, s2.length(), s2); index += s2.length();... week 10 7

Profile Program Performance C++: Use profile tools, such as gprof > g++ -O -p source.cpp -o executable > executable > gprof executable > profile percentage of time spent in each function > man gprof for the options > g++ -O -fprofile-arcs -ftest-coverage src.cpp -o dest > dest > gcov src.cpp > less src.cpp.gcov Java: > java -Xprof ajavaclass week 10 8

Developing Complex Programs (Using Games as Examples) Reference Books (purchase not necessary) Learn the languages in a bigger context: Games week 10 9

Java Games Applet games: running through web browser + no installation needed, easy upgrade (the web version is always the latest) security restrictions, cannot save game status Window games: + no restrictions like applets players may be distracted by other windows Full-screen games: + no other program can appear to distract players do not allow players to engage in other activities (such as instant messaging) week 10 10

Full Screen Game // Modified from Developing Games in Java by David Brackeen // http://www.brackeen.com/javagamebook/#download import java.awt.*; import javax.swing.jframe; public class FullScreenTest extends JFrame { private int xpos; private int ypos; private int xdir; private int ydir; private int width; private int height; public static void main(string[] args) { DisplayMode displaymode; week 10 11

if (args.length == 3) { displaymode = new DisplayMode( Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), DisplayMode.REFRESH_RATE_UNKNOWN); else { displaymode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); FullScreenTest test = new FullScreenTest(displayMode.getWidth(), displaymode.getheight()); test.test(displaymode); week 10 12

private static final long DEMO_TIME = 50000; public FullScreenTest(int w, int h) { xdir = 1; ydir = 1; width = w; height = h; xpos = width / 7; ypos = height / 11; public void test(displaymode displaymode) { setbackground(color.blue); setforeground(color.white); setfont(new Font("Dialog", 0, 24)); SimpleScreenManager screen = new SimpleScreenManager(); try { screen.setfullscreen(displaymode, this); week 10 13

try { Thread.sleep(DEMO_TIME); catch (InterruptedException ex) { finally { screen.restorescreen(); public void paint(graphics gfx) { gfx.setcolor(color.blue); gfx.drawstring("x= " + xpos + " y= " + ypos, 50, 50); gfx.filloval(xpos, ypos, 40, 40); xpos += (int) ( Math.random() * 3) * xdir; ypos += (int) ( Math.random() * 3) * ydir; if (xpos < 20) { xpos = 20; xdir = - xdir; if (xpos > (width - 40)) { xpos = width - 40; xdir = - xdir; if (ypos < 20) { ypos = 20; ydir = - ydir; if (ypos > (height - 40)) { ypos = height - 40; ydir = - ydir; week 10 14

gfx.setcolor(color.white); gfx.filloval(xpos, ypos, 40, 40); gfx.drawstring("x= " + xpos + " y= " + ypos, 50, 50); // add some delays long nexttime = System.currentTimeMillis() + 1; while (System.currentTimeMillis() < nexttime) { repaint(); week 10 15

import java.awt.*; import javax.swing.jframe; /** The SimpleScreenManager class manages initializing and displaying full screen graphics modes. */ public class SimpleScreenManager { private GraphicsDevice device; /** Creates a new SimpleScreenManager object. */ public SimpleScreenManager() { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = environment.getdefaultscreendevice(); /** Enters full screen mode and changes the display mode. */ week 10 16

public void setfullscreen(displaymode displaymode, JFrame window) { window.setundecorated(true); window.setresizable(false); device.setfullscreenwindow(window); if (displaymode!= null && device.isdisplaychangesupported()) { try { device.setdisplaymode(displaymode); catch (IllegalArgumentException ex) { // ignore - illegal mode for this device /** Returns the window currently used in full screen mode. */ week 10 17

public Window getfullscreenwindow() { return device.getfullscreenwindow(); /** Restores the screen's display mode. */ public void restorescreen() { Window window = device.getfullscreenwindow(); if (window!= null) { window.dispose(); device.setfullscreenwindow(null); week 10 18

Determine Parameters DisplayMode(width, height, bitdepth, refreshrate) typical resolutions: 800x600 and 1024x768. higher resolution = better image quality, if the system is sufficiently fast. bit depth = 16 or higher better color quality, 8 is too low. high quality color = 8 bits for R, G, and B (24 bits total) REFRESH_RATE_UNKNOWN usually works well. Human eyes cannot detect refresh rate higher than 75Hz week 10 19

Create Motion (Animation) Draw an object Erase the object in the original location Draw it in the new location Repeat week 10 20

Flickering and Double Buffering If erasing occurs in the same image that is being shown, users can see the object disappearing for a short period of time flickering Solution: double buffering (images) show a completed image with the object already drawn have a second image on which the object is erased, drawn at the new location show the second image to user repeat between the two images week 10 21

flickering buffer 1 buffer 2 week 10 22

Animation with Double Buffering import java.awt.image; import java.util.arraylist; /** The Animation class manages a series of images (frames) and the amount of time to display each frame. */ public class Animation { private ArrayList frames; private int currframeindex; private long animtime; private long totalduration; /** Creates a new, empty Animation. */ public Animation() { frames = new ArrayList(); totalduration = 0; restart(); week 10 23

/** Adds an image to the animation with the specified duration (time to display the image). */ public synchronized void addframe(image image, long duration) { totalduration += duration; frames.add(new AnimFrame(image, totalduration)); /** Starts this animation over from the beginning. */ public synchronized void restart() { animtime = 0; currframeindex = 0; /** Updates this animation's current image (frame), if neccesary. */ public synchronized void update(long elapsedtime) { if (frames.size() > 1) { animtime += elapsedtime; if (animtime >= totalduration) { week 10 24

animtime = animtime % totalduration; currframeindex = 0; while (animtime > getframe(currframeindex).endtime) { currframeindex++; /** Gets this Animation's current image. Returns null if this animation has no images. */ public synchronized Image getimage() { if (frames.size() == 0) { return null; else { return getframe(currframeindex).image; week 10 25

private AnimFrame getframe(int i) { return (AnimFrame)frames.get(i); private class AnimFrame { Image image; long endtime; public AnimFrame(Image image, long endtime) { this.image = image; this.endtime = endtime; week 10 26

import java.awt.*; import javax.swing.imageicon; public class AnimationTest2 { public static void main(string args[]) { AnimationTest2 test = new AnimationTest2(); test.animate(); private static final DisplayMode POSSIBLE_MODES[] = { new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0), new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0), new DisplayMode(640, 480, 16, 0) ; private static final long DEMO_TIME = 40000; private ScreenManager screen; week 10 27

private Image bgimage; private Animation anim; public void loadimages() { // load images bgimage = loadimage("images/background.jpg"); Image player1 = loadimage("images/player1.png"); Image player2 = loadimage("images/player2.png"); Image player3 = loadimage("images/player3.png"); // create animation anim = new Animation(); anim.addframe(player1, 250); anim.addframe(player2, 150); anim.addframe(player1, 150); anim.addframe(player2, 150); anim.addframe(player3, 200); anim.addframe(player2, 150); week 10 28

private Image loadimage(string filename) { return new ImageIcon(fileName).getImage(); public void animate() { screen = new ScreenManager(); try { DisplayMode displaymode = screen.findfirstcompatiblemode(possible_modes); screen.setfullscreen(displaymode); loadimages(); animationloop(); finally { screen.restorescreen(); week 10 29

public void animationloop() { long starttime = System.currentTimeMillis(); long currtime = starttime; while (currtime - starttime < DEMO_TIME) { long elapsedtime = System.currentTimeMillis() - currtime; currtime += elapsedtime; // update animation anim.update(elapsedtime); // draw and update screen Graphics2D g = screen.getgraphics(); draw(g); g.dispose(); screen.update(); // take a nap try { Thread.sleep(20); catch (InterruptedException ex) { week 10 30

public void draw(graphics g) { // draw background g.drawimage(bgimage, 0, 0, null); // draw image g.drawimage(anim.getimage(), 0, 0, null); week 10 31

Implementing Double Buffering import java.awt.*; import java.awt.image.bufferstrategy; import java.awt.image.bufferedimage; import java.lang.reflect.invocationtargetexception; import javax.swing.jframe; /** The ScreenManager class manages initializing and displaying full screen graphics modes. */ public class ScreenManager { private GraphicsDevice device; /** Creates a new ScreenManager object. */ public ScreenManager() { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); week 10 32

device = environment.getdefaultscreendevice(); /** Returns a list of compatible display modes for the default device on the system. */ public DisplayMode[] getcompatibledisplaymodes() { return device.getdisplaymodes(); /** Returns the first compatible mode in a list of modes. Returns null if no modes are compatible. */ public DisplayMode findfirstcompatiblemode( DisplayMode modes[]) { DisplayMode goodmodes[] = device.getdisplaymodes(); for (int i = 0; i < modes.length; i++) { for (int j = 0; j < goodmodes.length; j++) { if (displaymodesmatch(modes[i], goodmodes[j])) { return modes[i]; week 10 33

return null; /** Returns the current display mode. */ public DisplayMode getcurrentdisplaymode() { return device.getdisplaymode(); /** Determines if two display modes "match". Two display modes match if they have the same resolution, bit depth, and refresh rate. The bit depth is ignored if one of the modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI. Likewise, the refresh rate is ignored if one of the modes has a refresh rate of DisplayMode.REFRESH_RATE_UNKNOWN. */ public boolean displaymodesmatch(displaymode mode1, DisplayMode mode2) { week 10 34

if (mode1.getwidth()!= mode2.getwidth() mode1.getheight()!= mode2.getheight()) { return false; if (mode1.getbitdepth()!= DisplayMode.BIT_DEPTH_MULTI && mode2.getbitdepth()!= DisplayMode.BIT_DEPTH_MULTI && mode1.getbitdepth()!= mode2.getbitdepth()) { return false; if (mode1.getrefreshrate()!= DisplayMode.REFRESH_RATE_UNKNOWN && mode2.getrefreshrate()!= DisplayMode.REFRESH_RATE_UNKNOWN && mode1.getrefreshrate()!= mode2.getrefreshrate()) { return false; return true; week 10 35

/** Enters full screen mode and changes the display mode. If the specified display mode is null or not compatible with this device, or if the display mode cannot be changed on this system, the current display mode is used. <p> The display uses a BufferStrategy with 2 buffers. */ public void setfullscreen(displaymode displaymode) { final JFrame frame = new JFrame(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setundecorated(true); frame.setignorerepaint(true); frame.setresizable(false); device.setfullscreenwindow(frame); if (displaymode!= null && device.isdisplaychangesupported()) { try { device.setdisplaymode(displaymode); catch (IllegalArgumentException ex) { week 10 36

// fix for mac os x frame.setsize(displaymode.getwidth(), displaymode.getheight()); // avoid potential deadlock in 1.4.1_02 try { EventQueue.invokeAndWait(new Runnable() { public void run() { frame.createbufferstrategy(2); ); catch (InterruptedException ex) { // ignore week 10 37

catch (InvocationTargetException ex) { // ignore /** Gets the graphics context for the display. The ScreenManager uses double buffering, so applications must call update() to show any graphics drawn. <p> The application must dispose of the graphics object. */ public Graphics2D getgraphics() { Window window = device.getfullscreenwindow(); if (window!= null) { BufferStrategy strategy = window.getbufferstrategy(); return (Graphics2D)strategy.getDrawGraphics(); else { return null; /** Updates the display. */ week 10 38

public void update() { Window window = device.getfullscreenwindow(); if (window!= null) { BufferStrategy strategy = window.getbufferstrategy(); if (!strategy.contentslost()) { strategy.show(); // swap buffers // Sync the display on some systems. // (on Linux, this fixes event queue problems) Toolkit.getDefaultToolkit().sync(); /** Returns the window currently used in full screen mode. Returns null if the device is not in full screen mode. */ public JFrame getfullscreenwindow() { return (JFrame)device.getFullScreenWindow(); week 10 39

/** Returns the width of the window currently used in full screen mode. Returns 0 if the device is not in full screen mode. */ public int getwidth() { Window window = device.getfullscreenwindow(); if (window!= null) { return window.getwidth(); else { return 0; /** Returns the height of the window currently used in full screen mode. Returns 0 if the device is not in full screen mode. */ public int getheight() { Window window = device.getfullscreenwindow(); if (window!= null) { return window.getheight(); else { return 0; /** Restores the screen's display mode. */ public void restorescreen() { week 10 40

Window window = device.getfullscreenwindow(); if (window!= null) { window.dispose(); device.setfullscreenwindow(null); /** Creates an image compatible with the current display. */ public BufferedImage createcompatibleimage(int w, int h, int transparancy) { Window window = device.getfullscreenwindow(); if (window!= null) { GraphicsConfiguration gc = window.getgraphicsconfiguration(); return gc.createcompatibleimage(w, h, transparancy); return null; week 10 41

public void draw(graphics2d g) { // draw background g.drawimage(bgimage, 0, 0, null); AffineTransform transform = new AffineTransform(); for (int i = 0; i < NUM_SPRITES; i++) { Sprite sprite = sprites[i]; // translate the sprite transform.settotranslation(sprite.getx(), sprite.gety()); // if the sprite is moving left, flip the image if (sprite.getvelocityx() < 0) { transform.scale(-1, 1); transform.translate(-sprite.getwidth(), 0); // draw it g.drawimage(sprite.getimage(), transform, null); week 10 42

ECE 462 Object-Oriented Programming using C++ and Java Lecture 18 Yung-Hsiang Lu yunglu@purdue.edu

Lab 10 Creating Moving and Rotating Balls with Changing Background

Procedure Download source code from http://www.brackeen.com/javagamebook/#download Enter chapter 02 Modify SpriteTest2 Download the images from /home/shay/a/sfwtools/public/data different numbers of balls translate (i.e. move) rotate change background detect collision, between a ball and the boundary and between balls (not shown in the demonstration) week 10 45

Rotation in Java AffineTransform transform = new AffineTransform(); transform.rotate(theta); // most likely, rotate around the center of the image transform.rotate(theta, imagetoshow.getwidth(null) / 2, imagetoshow.getheight(null) / 2); g.drawimage(imagetoshow, transform, null); week 10 46

Collision Detection required in many games, such as player hits walls in a maze missile hits enemy spacecraft ball bounces at the boundary of the play field a Tetris piece hits a square and cannot rotate a fighter s sword hits another fighter a racing car hits a guardrail a baseball hitter misses the ball week 10 47

week 10 48

Collision Detection in Lab 10 ball bounces after hitting a wall (x1,y1) r1 If the ball s center is (x1, y1), the ball hits the bottom if (Ymax - y1) < r1 two balls bounces after hitting each other, if AB< r1+ r2 AB = (x x ) + (y y ) < r1+ r2 2 2 1 2 1 2 remember that both balls are moving r2 B A r1 week 10 49

Programming Hints add an attributes for the angle (theta) and the angular velocity (dt) in Sprite::update, add theta += dt * elapsedtime; public void draw(graphics2d g) { if (changebackground == false) { bkindex = 0; g.drawimage(bgimage[bkindex], 0, 0, null); AffineTransform transform = new AffineTransform(); for (int i = 0; i < NUM_BALLS; i++) { Sprite sprite = balls[i]; if (translate) { transform.settotranslation(sprite.getx(), sprite.gety()); if (rotate) { transform.rotate(sprite.gett(), soimage.getwidth(null) / 2, soimage.getheight(null) / 2); g.drawimage(soimage, transform, null); week 10 50

Abstract Class for Game Programs (Brackeen s Book Chapter 3) public abstract class GameCore {... public void gameloop() { long starttime = System.currentTimeMillis(); long currtime = starttime; while (isrunning) { long elapsedtime = System.currentTimeMillis() - currtime; currtime += elapsedtime;... public abstract void draw(graphics2d g); public void stop() { isrunning = false; week 10 51

Handle Keyboard Events In Netbeans, an event handler is created by the programming interface. When writing code, use Listener for events. Pressing a key triggers one event: keypressed Releasing a key triggers two events: keyreleased and keytyped. The former indicates that no key is being pressed. The latter indicates which key is pressed and released. To know which key is pressed: public void keypressed(keyevent e) { int keycode = e.getkeycode(); String txt = KeyEvent.getKeyText(keyCode); // more information available in Java s KeyEvent class and // http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html week 10 52

import java.awt.*; import java.awt.event.keylistener; import java.awt.event.keyevent; import java.util.linkedlist; import com.brackeen.javagamebook.graphics.*; import com.brackeen.javagamebook.test.gamecore; /** A simple keyboard test. Displays keys pressed and released to the screen. Useful for debugging key input, too. */ public class KeyTest extends GameCore implements KeyListener { public static void main(string[] args) { new KeyTest().run(); private LinkedList messages = new LinkedList(); public void init() { super.init(); Window window = screen.getfullscreenwindow(); week 10 53

// allow input of the TAB key and other keys normally used for focus // traversal, call getfocustraversalkeys to find which they are window.setfocustraversalkeysenabled(false); // register this object as a key listener for the window window.addkeylistener(this); addmessage("keyinputtest. Press Escape to exit"); // a method from the KeyListner interface public void keypressed(keyevent e) { int keycode = e.getkeycode(); // exit the program if (keycode == KeyEvent.VK_ESCAPE) { // each key has it own code stop(); // set isrunning to false else { addmessage("pressed: " + KeyEvent.getKeyText(keyCode)); week 10 54

// make sure the key isn't processed for anything else e.consume(); // a method from the KeyListner interface public void keyreleased(keyevent e) { int keycode = e.getkeycode(); addmessage("released: " + KeyEvent.getKeyText(keyCode)); // make sure the key isn't processed for anything else e.consume(); // a method from the KeyListener interface public void keytyped(keyevent e) { // this is called after the key is released - ignore it // make sure the key isn't processed for anything else e.consume(); week 10 55

/** Add a message to the list of messages. */ public synchronized void addmessage(string message) { messages.add(message); if (messages.size() >= screen.getheight() / FONT_SIZE) { messages.remove(0); /** Draw the list of messages */ public synchronized void draw(graphics2d g) { Window window = screen.getfullscreenwindow(); g.setrenderinghint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // draw background g.setcolor(window.getbackground()); g.fillrect(0, 0, screen.getwidth(), screen.getheight()); week 10 56

// draw messages g.setcolor(window.getforeground()); int y = FONT_SIZE; for (int i=0; i<messages.size(); i++) { g.drawstring((string)messages.get(i), 5, y); y+=font_size; week 10 57

Handle Mouse Events four types of mouse events: button click, motion, wheel scroll, enter and leave create a MouseListener, MouseMotionListener, MouseWheelListener import java.awt.*; import java.awt.event.*; import java.util.linkedlist; import com.brackeen.javagamebook.graphics.*; import com.brackeen.javagamebook.test.gamecore; /** A simple mouse test. Draws a "Hello World!" message at the location of the cursor. Click to change to "trail mode" to draw several messages. Use the mouse wheel (if available) to change colors. */ week 10 58

public class MouseTest extends GameCore implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener { public static void main(string[] args) { new MouseTest().run(); private static final int TRAIL_SIZE = 10; private static final Color[] COLORS = { Color.white, Color.black, Color.yellow, Color.magenta ; private LinkedList traillist; private boolean trailmode; private int colorindex; public void init() { super.init(); traillist = new LinkedList(); Window window = screen.getfullscreenwindow(); week 10 59

window.addmouselistener(this); window.addmousemotionlistener(this); window.addmousewheellistener(this); window.addkeylistener(this); public synchronized void draw(graphics2d g) { int count = traillist.size(); if (count > 1 &&!trailmode) { count = 1; Window window = screen.getfullscreenwindow(); // draw background g.setcolor(window.getbackground()); g.fillrect(0, 0, screen.getwidth(), screen.getheight()); // draw instructions g.setrenderinghint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); week 10 60

g.setcolor(window.getforeground()); g.drawstring("mousetest. Press Escape to exit.", 5, FONT_SIZE); // draw mouse trail for (int i=0; i<count; i++) { Point p = (Point)trailList.get(i); g.drawstring("hello World!", p.x, p.y); // from the MouseListener interface public void mousepressed(mouseevent e) { trailmode =!trailmode; // from the MouseListener interface public void mousereleased(mouseevent e) { // do nothing // from the MouseListener interface week 10 61

public void mouseclicked(mouseevent e) { // called after mouse is released - ignore it // from the MouseListener interface public void mouseentered(mouseevent e) { mousemoved(e); // from the MouseListener interface public void mouseexited(mouseevent e) { mousemoved(e); // from the MouseMotionListener interface public void mousedragged(mouseevent e) { mousemoved(e); week 10 62

// from the MouseMotionListener interface public synchronized void mousemoved(mouseevent e) { Point p = new Point(e.getX(), e.gety()); traillist.addfirst(p); while (traillist.size() > TRAIL_SIZE) { traillist.removelast(); // from the MouseWheelListener interface public void mousewheelmoved(mousewheelevent e) { colorindex = (colorindex + e.getwheelrotation()) % COLORS.length; if (colorindex < 0) { colorindex+=colors.length; Window window = screen.getfullscreenwindow(); window.setforeground(colors[colorindex]); week 10 63

// from the KeyListener interface public void keypressed(keyevent e) { if (e.getkeycode() == KeyEvent.VK_ESCAPE) { // exit the program stop(); // from the KeyListener interface public void keyreleased(keyevent e) { // do nothing // from the KeyListener interface public void keytyped(keyevent e) { // do nothing week 10 64

Creating 2D Java Game week 10 65

Creating 2D Java Game week 10 66

Changing Background (Cloud) week 10 67

Changing Background background map: To provide a changing background without a very large actual background sky tiles week 10 68

Tile Map (review) Java objects are not deep copied (unless cloned); instead, they refer to the same objects and use much less memory than deep copy. the titles do not need individual copies of the images collision detection based on the type of a tile complex map a special map editor simple map text editor sufficient Each object s location is relative to the map (not to the screen) so that a player can scroll left or right easily. week 10 69

Manage Visible Objects off screen xoffset on screen off screen An object is visible if the location is between xoffset and xoffset + xresolution In a game with few moving objects, it is acceptable to draw every object. If it is outside the visible region, it cannot be seen. If the background moves slowly, it gives the impression of distance. This allows a smaller image for the background and saves memory. week 10 70

Tile-Based Collision Detection If the sprite s next location is (x,y), checking whether collision occurs is easy: tilemap.get(x,y) if a tile exists, collision week 10 71

Analyze a Complex Program run doxygen, already installed in ECN computers http://www.stack.nl/~dimitri/doxygen/ doxygen -g generate configuration, called Doxyfile change the following lines in Doxyfile OUTPUT_DIRECTORY = docs EXTRACT_ALL = YES EXTRACT_PRIVATE = YES INPUT = src/ RECURSIVE = YES doxygen (again) to generate documentation week 10 72

Create and Execute the Program The source comes with build.xml. It is the Makefile for program ant (http://ant.apache.org/). In your ee462xxx account, type ant should create tilegame.jar. To execute the program, type java -jar tilegame.jar week 10 73

Execution Flow GameManager::main new GameManager().run() calls GameCore::run because GameCore is the base class. This run has nothing to do with threads. It is a bad choice of the function s name. init(); set up input manager set up resource manager load resources (tile map, images) load sound start music gameloop(); get elapsed time update draw show on screen week 10 74

Object-Oriented Programming create class hierarchies so that common functionalities are handled by the base classes. Example, Sprite - Creature - Fly update is overridden in derived class, Creature all of them have attributes x, y, dx, and dy collision detection takes an object as the base class A member function is the base class can call another member function. If the second member function is overridden, the derived class function is called. For example, GameCore::run call init(). week 10 75

Objects Know What to Do Calling Each Object s update : Six classes implement the update function. Pay special attention to Sprite::update and Creature::update. Each moving object keeps its location and velocity. The program, however, has some examples that are not object-oriented. Avoid getx and setx functions; they break encapsulation When collision occurs, the object should decide what to do. The velocity should not be set externally. Do not use the name of a Java function, such as run week 10 76

Improve Java Performance String is immutable. Changing a String object means (1) discarding the original String object garbage (2) creating a new String object memory allocation. If a String object is changed frequently, use StringBuffer. Be careful about creating intermediate objects. Java String cannot have a derived class. String st1 = "hello"; for (int iter = 0; iter < NUMBER_ITERATION; iter ++) { st1 += iter; StringBuffer st2 = new StringBuffer("hello"); for (int iter = 0; iter < NUMBER_ITERATION; iter ++) { st2.append(iter); week 10 77

Compile-Time vs. Run-Time String s3 = "Hello"; s3 = "Hello" + " World!"; String s4 = "Hello"; s4 = new String("Hello") + "World"; Are they the same? Yes, functionality No, performance Assigning to s3 is faster because the String object can be created at compile-time. week 10 78

Tools for Java Profile tools: java -Xprof xxx Profile in netbeans Test coverage: EMMA week 10 79