Vous êtes sur la page 1sur 359

BEGINNING

JAVA GAME
PROGRAMMING
WzECOND EDITION

1-~l

ARBOUR

2008 Thomson Course Teclmology. a division of Thomson Learni ng


Inc. All rights reserved. No pa rt of this book may be reproduced or
transmitted in any form or by any means, electronic or mechanical.
includi ng photocopying, record ing, or by any informa tion storage or
retrieval system without written permission from Thomson Course
Teclmology PTR, except for the inclusion of brief quotations in a review.

Publisher and General Manager)


Thomson Course Technology PTR:
Stacy L. Hiquet

The Thomson Course Technology PTR logo and related trade dress are
trademarks of Thomson Course Technology. a div ision of Tho mso n
Learning Inc., and may not be used without written permission.

Manager of Editorial Services:


Heather Talbot

Java is a trademark of Sun Mic rosystems, Inc. in the Un ited States and
olher countries. All other trademarks are the property of their
respective ow ners.
Jlllporfclllt: Thomson Course Techn ology PTR canno t provide sofhvare

support. Please co ntact the appropriate softwa re manufacturer's


techni cal support lin e or Web site for assistance.
Thomson Course Technology PTR and the author have attempted
throughout this book to distinguish proprietary trademarks from
descriptive terms by following the cap italization style used by the
manufacturer.
Information contained in this book has been obta ined by Thomson
Co urse Technolo!:,'Y PTR from so urces believed to be reliable. However,
because of the possibil ity of human or mecha nical error by our sources,
Thomson Course Technology PTR, or othe rs, the Publisher does not
guarantee the accuracy, adequacy, or completeness of any information
and is not responsible for any erro rs Of omissions or the results
obtained from use of such information. Readers should be particularly
awafe of the fact that the Internet is an ever-cha nging entity. Some facts
may have changed since this book went to press.
Educatio nal facilities, compa nies, and organizations interested in
multiple copies or licensing of this book should contact the Publisher
for quantity discount information. Tra inin g ma nuals, CD-ROMs, and
portions of this book are also available individua lly or can be tailored
for spec ific needs.
ISBN-l0: 1-59863-476-3
ISBN - 13: 978 - 1-59863-476-1
Library of Co ngress Catalog Card Number: 2007938236
Printed in the Un ited States of America
0809 10 II 12 TW 1098765432 1

Thomson Course Technology PTR,


THOMSON
a division of Thomson Le:uning Inc.

,~ 25 Thomson Place
COURSE TECHNOLOGY
Boston, MA 022\0

Professional_ Technical. Reference

http://www.courseptr.com

Associate Director of Marketing:


Sa rah O'Donnell

Marketing Manager:
Jordan Casey
Senior Acquisitions Editor:
Emi Sm ith
Project Editor/Copy Editor:
Cath leen D. Small
Technical Reviewer:
Dusti n Clingman
PTR Editorial Services Coordinator:
Erin Johnson
Interior Layout Tech:
rcc Macm illan Inc.
Cover Designer:
Mike Ta namachi
CD-ROM Producer:
Brando n Penticuff
Indexer:
Katherine Stimson
Proofreader:
Kate Shoup

For Kaitlyn Faye

ACKNOWLEDGMENTS

I thank God for the many opportunities that have come my way this year, such as
the chance to write this book, and for the apparent talent needed to make
something tangible of these opportunities. I am grateful to my family for their
ongoing encouragement: Jennifer, Jeremiah, Kayleigh, Kaitlyn, Kourtney, Mom
and Dad, Grandma Cremeen, Dave and Barbara, my extended family at Vision
Baptist Church, and Pastor Michael Perham and his family-Jennifer, Ashley,
Bryce, and Sage-who have been such a blessing this past year.
Thank you to the students, faculty, and staff at UAT for contributing to such a
wonderfully creative environment for learning. I would like to thank the Alpha
Squad team, who had some influence on this book (and even helped to solve a few
coding problems with Galactic War): Roy Evans, Stewart Johnston, Peter Pascoal,
Travis Eddlemon, Daniel Muller, Daniel Stirk, Patrick Cissarz, David Coddington,
Marc Kirschner, Jeffrey Woodard, Jonathan Allmen, Levi Bath, Douglas Cannon,
Joshua Gertz, Justin Hair, Adam Knight, Eric Lacerna, Daryl Lynch, and Kevin
McCusker; and the faculty sponsors: Rebecca Whitehead, Michael Eilers, and
Arnaud Ehgner. I also owe my thanks to students Mark Walker and Andrew
Hawken for introducing me to the angular velocity code used in Galactic War.
I am also very thankful for the artwork featured in this book, provided by Ari Feldman
(www.flyingyogi.com) and Reiner Prokein (www.reinerstiJeset.de). Without their
wonderful graphics, Galactic War would have featured programmer art (cringen.
I offer my sincere thanks to the editors at Course Technology PTR and the
freelance editors who put this book together: Emi Smith, Cathleen Small, Dustin
Clingman, and Kate Shoup.

ABOUT THE AUTHOR

Jonathan S. Harbour is an Associate Professor of Game Development at the


University of Advancing Technology in Tempe, Arizona. His current game
project, Starflight: The Lost Colony (www.starflightgame.com). will be released in
late 2007. He lives in Arizona with his wife, Jennifer, and four children: Jeremiah,
Kayleigh, Kaitlyn, and Kourtney. He can be reached at www.jharbouLcom.

CONTENTS

PART I
Chapter 1

Introduction ..

xiii

JAVA FOR BEGINNERS

Getting Started with Java

. . . . . . . . . . . . . . . .. 3

Java and the Web . .


Studying the Market . .
Design Rules. .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Casual Games Market.

3
4

5
6
No Manual Required . .
.. . .... . .. ... . . 7
Casual Garners. .
.. ... .. . ... . .. ... .. .. . ... .. .
8
Casual Games.
8
Installing and Configuring Java.
.. .....
9
Installing Java
. . . . . . . . . . .. 10
Configuring Java
11
Java Version Numbers
........ .
14
Your Fi rst Java Program . .
. . . . . . ..
1S
Java Application. . . . .
15
Java Applet . . . . . .
.. .. .. .
17
What You Have Learned.
........
20
Rev iew Questions .
21
On Your Own.
22
22
Exercise 1 . .
Exercise 2 .... . .. . ... . . .. . .... . . . . . .. . . . .. .. . . .
22

vi

Contents

Chapter 2

Java Programming Essentials . . . . . . . . . . . . . . . . . . . ..

23

Java Applets . . .

24

Web Server Technology Explained


Hosting Java Applets . .

24

. ... . ... .. . . .. ... . . .. . . .

The Java Language ..


Java Data Types. . . .

. . . .. . . . . .. . .. . ... . . . . . . .. ..

The Essence of Class.


The rnain Function . .

27

37

39

Object-Oriented Programming
What You Have Learned . . . .

.
. . ...... . .

Review Questions.

On Your Own.
Exercise 1.
Exercise 2. .

Chapter 3

24

25
25

Compiling Java Code . .

40
46
47
48
48
48

. 49

Creating Your First Java Game


About the Game Project.

49

Creating the Game . ..

52

Creating the Project ..

53

The BaseVectorshape Class.

53
55

The Ship Class


The Bullet Class

56

The Asteroid Class ................... .

57

The Main Source Code File


Applet initO Event.
. ..... . . .
Applet updateO Event . . .

. ..... . .

Drawing the Player's Ship . ... . . . . ... . .... .. . . . . . ..


Draw ing the Bullets.
Draw ing the Asteroids.
Screen Refresh. . . . . . .

. . . .. . . .... . . . ...

Thread Events and the Game Loop


Game Loop Update
... . . .. .

Updating the Ship. . . . . . . . . . . . . . . . . . . ..


Updating the Bullets . .

. ... . . . .

Updating the Asteroids


Testing for Collisions.
Keyboard Events
Calculating Realistic Motion .

. . . .. . ..

58
59
60
61
61
62
63
63
64
65
65
66
68
69
71

vii

viii

Contents

What You Have Learned.


Review Questions
On You r Own.

73
73
74
74
74

. . . .. . .. .. . . . .

Exercise 1

Exercise 2.

PART II

Java Game Programming .. . . . . . . . . . . . . . . . . . . . . .. 75

Chapter 4

Vector-Based Graphics . . . . . . . . . . . . . . . . . . . . . . . . ..
Programming Vector Graphics

Working w ith Shapes ..

80
83
86

What You Have Learned.


.
.
. . ..... . .... . ... .

87
87

Exercise 1. .
Exercise 2

Chapter 5

87

88

89

Programming Bitmapped Graphics ... . ..... . .... . .. . .. . .


Load ing and Drawing Images.
.
.

89
90
92
94
9S
97
99
104
104
104

Bitmap-Based Graphics

Applying Transforms to Images.


Transparency. . . . .

. .... . . .... .. . . .

Opaque Images
............ .. ..
Transparent Images ............... .
Working Some Masking Magic .... . .. . . ...
What You Have Learned.
Review Questions .
On Your Own.

Chapter 6

77
78

Working w ith Polygons


Rotating and Scaling Shapes
Review Questions.
On Your Own.

77

Exe rcise 1.

lOS

Exercise 2.

lOS

Simple Sprites

Programm ing Simple Sprites. . . . . . . . .


The Point2D Class.
Basic Game Entities
The ImageEntity Class
Creating a Reusable Sprite Class

107
107
109
110
111
114

Contents

Collision Testing. . . . . . . . . . . .

114

Sprite Class Source Code . . . . . . . . . . . . . . . . . . . . . . . . . .


Testing the Sprite Class.
What You Have Learned.

118
121

. . . . . . . . . . . . . . . . . . . . . . . ..

122

Review Questions. . .

121

On Your Own . .

Exercise 1. . . . . . . . . . .
Exercise 2.

Chapter 7

122
122

Sprite Animation

123

Sprite Animation ...

123
124

Animation Techniqu es
Drawing Individual Frames

124
126

Keeping Track of Animation Frames.


Testing Sprite Animation
Encapsulating Sprite An imation in a Class ..... .. .. . . . . ..
Testing the AnimatedSprite Class.
What You Have Learned . .
Review Questions. . .
On Your Own.

Exercise 1..

Exercise 2.. ..

Chapter 8

Keyboard and Mouse Input


Listening to the User

127
132
13S
137
138
138
138
139

141

141
142

Listening for Keyboard Events

142

Keyboard Input...
Testing Keyboard Input . . .
Mouse Input ...

143

Reading Mouse Motion

146
147

145

Detecting Mouse Buttons.


Testing Mouse Input
What You Have Learned . .. ... . . ... .. . . ... ... ... . . . . .

148

Review Questions . .

153
153

On Your Own . .
Exercise 1

153

154

Sound Effects and Music


Playing Digital Sample Files

152

Exercise 2...

Chapter 9

114

. . . . . . . . . . . . . . . . . ..

1SS
.

155

ix

Contents

Getting Started with Java Sound ... . . ..... . . .

157

Playing Sounds ..

164
166

Playing MIDI Sequence Files ..


Loading a MIDI File

166

Playing Music. ....... . .. ......... . ........ .

167

Reusable Classes.
The SoundClip Class.

169
170

The MidiSequence Class ...... . . .. ... . ... ........


What You Have Learned ...
Review Questions.
On Your Own.
Exercise 1

Exercise 2

Chapter 10

172
175
175
176
176
176

Timing and the Game loop. . . . . . . . . . . . . . . . . . . . .

177

The Potency of a Game Loop . .

178

A Simple Loop . . .

..........

178

Overriding Some Default Applet Behaviors. . . . . . . . . . . . ..

181

Feeling Loopy . . . . . . . . . . . . . . .
Recovering LongLost Applet Methods. . .

182
183

Stepping Up to Threads

184

Starting and Stopping the Thread. .


The ThreadedLoop Program. .

184

Examining Multithreading

189

What You Have Learned.

185

...........

... ..... .. .. ... .. .. . .. ..

Review Questions . . . .

189

........

190

........
On Your Own.
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
Exercise 2. . . . . . . . . . . . . . . . . .

PART III

THE GALAalC WAR PROJEa

Chapter 11

Galactic War: From Vectors to Bitmaps

...........

190
190

191
.

Improving the Game

Generalizing the Vector Classes

193
193
194

The Main Source Code File: GalacticWar.java

197

What You Have Learned .

204

Review Questions .......... .. . ... . ...


On Your Own. . .

190

204
204

Contents

Chapter 12

Galactic War: Sprites and Collision Boxes


Creating the Project. . . . . . . . . . . . . .

.
. ... .. . . .

207

The Galactic War Bitmaps.


The New and Improved Source Code

Chapter 13

208
210

What You Have Learned.

224

Review Questions .

225

On Your Own . . . .

225

Galactic War: Squashed by Space Rocks

227

What You Have Learned . ......... . . . . . . . . . . .. ... . ..

233

Review Questions.

233

On Your Own.

234

Galactic War: Entity Management

Adjusting to Event-Driven Programming

235
236

Exp loring the Class Library . .

236

Building the New Game Class .............. ...... . ..

237

Enhancing Galactic War.


.... .. ... ..
Exploring the New Galactic War Source Code ...

249

What You Have Learned

273

Review Questions. . . . . .

.
.

Galactic War: Finishing the Game


Let's Talk about Powerups .
Ship and Bonus-Point Powerups . .
Weapon Upgrades.
Enhancing Galactic War.

249

274
274

275

. . .. ... . ... .. . . . . . ... . .

On Your Own..

Chapter 15

227

. ........

Being Civilized about Collisions.

Chapter 14

207

. . . . ... .... .. . .

275
276
276

. . . . .. .. . . .. .. . . . .... .....

281

New Sprite Types ...

281

New Game States.

281

New Sprite Images. . . . . . . . . . . .


.
Health/Shield Meters, Score, Firepower, and

282

Game State Variables.

283

New Input Keys . . ..

283

Sound and Music Objects . . .. . . . . . . . . . .. .... .......


Loading Media Files. .
. ... ... . ... . . ..

284
284

Game State Issue-Resetting the Game.

287

Detecting the Game-Over State .... . . . . .. . . . .. .. . .

288

Screen Refresh Updates. .

289

. .... .. . . ..

xi

xii

Contents

Preparing to End ..
Updating New Sprites.
Grabbing Powerups. . .. . . . .. .... ... .. . . ... ......
New Input Keys.
Spawning Powerups . . .
Making the Shield Work.
.
Making Use of Weapon Upgrade Powerups ..

Ta llying the Score


What You Have Learned . . .. . .... . . . . .. ... . .. . . . . . ..
Review Questions
On Your Own

Chapter 16

.
.

Galactic War: Web Deployment

Packaging an Applet in a Java Archive (JAR)


Using the jar.exe Program
Packag ing Galactic War in a Java Archive . .
Creating an HTML Host File for Your Applet ... . . . . .
A Simple HTML File. .

. ... ..... . . ...

Testing the Deployed Applet Game


.
What You Have Learned.
. ........ ... . . . . . .. ...
Review Questions.
Epilogue

. ...... . .
.

Appendix A: Chapter Quiz Answers


Index

291
292
294
296
299
301
302
30S
306
306
307

309
309
310
312
315
315
317
319
3 19
320

321
337

INTRODUCTION

This book willicach you how to write JaVil games that will run as applcts in a wcb
bT'mt..a. l"hc goal is to &vriop gal"l'lCS ror the casual gal'DC market. Gal'DC pro
gramming is a challmging subject that is not jusl difficult 10 IfU.SItt-it is
difficuh just to get started. This book takes aW3Y somc or thc m)')tc-ry or gamc
prog11lmming by expbining each step along the w~y, rrom one chapler to the
next I assume Ihat you havea litlle la\"a programming experience, but c\"en ir you
hal'C I"IeI'tt usal )aVil herorc. you should Ix- ablc 10 k<"q) up.

This book rnds IiU a hobby book. with no pressu~ and limittd pis, btcausc tht
pri=ry purpose or this book is to hdp you have: fun learning how 10 progrnm wrobased games. Typing in long source code listings out of a book is not fun, so I don't

ask you 10 do Ihat in every single chapter in Ihis book-Inslead, you willieam to wrile
shor1 programs to demonstratc tht major topics, and Ol'n" tinx' you will get the- hang
of il ~ is no mrtl'lOr'Uation ~uirtd bm-. btcause I'm a firm bd~ that
n:pdition-praclia---is the best W3y to learn, 001 throry and rnanorlzation.
Note

lIva

programming language ilwented by Sun Microsystt'lllS. The primal)' goal of Java's

1Jf'' '

~ wotS tu
tu mlI'lpilc a prllgrMn DIU and ' - it run on nwny ~ ~
syslemS. The JaQ ~ CINIeS a "l:Jorteoode- file tonIaIning W1ual m.xIlirw instJuctIarts that
tie lIl U- ~ ~ l tan cxecw (WI . " ~ S}W'II llpOll whidl it is

..

You will learn how 10 wr ile a simple la\"3 program in the first chapter. From
there, rou will Icam thc dClails or how 10 writc games that will run in a web

xiv

Introduction

browsc-r. WI" W\'l"r sourCl" code at a ~e that will nolll"3vl" you brilind. Aha- rou
havl" kamed enough, you will write your first ..d>-based g;lme. and you willihen
learn nl"W subjl"<:ts in each chapter to iru::rease your game programming skills. By
the end of this book, you will have learned to creale ~ complete web-based game
called Galactic War, and you will be able to deploy it to your website in a Ian
Archive (JAR) fill". And I'm not talking about some half-baked simulation posing
as a gamco I'm talking about a rmJ g;lmI'" that ill rdai] qlA.lity. suit:;lble for publishing in thl" castU1 gaml'" nurkl"t. Then att toous;rnds of casu<l1 gall'll"TS who att
paying to down]Nd g;lmd of this typl" from the many casual game sites on the
..,ro today-such as Rl"al Areadl" (...-.reala reade.com ). By learning how to
creale a casual game, you may even be preparing for a caret'r in the game
industry. developing gallll'S for Microsoft Xbox Live Arcade and other commercialendeavoT$.
gomtS _ widm ganws !IYI _ inWllfd on a
nI Ml in i Wfb ~ so
Ihil end usm do not need 10 ~tiI the gIflIe. Some giIlIe'i _
10 _
~(IIt lim; ;nf
pliyer dolt3 01\ th! Wfb - . n.. Im$t pcIIlUI.1r tn'I" III Wfb 9"""" is i casuil game:

While we're on the subjl"<:t of casual garnes, you can eVell program your own
Xbox 360 games, distributed on Xbox Livl" Arcade, using Microsoft's free: XNA
Game Studio bpre:ss softwue. Although this subject is beyond thl" scOpl" of this
book, I bring it up bromIC Microsoft's Cllanguagt' is unabuh~y similar 10
Java. Thomson published a book on this subject by Josq>h Hill. tilled XNA Gam..
51l1flio Express: Dn~lopiug Games for lViudo>o" (IUd the Xb<u 360 (Thomson
Course Technology PTR, 2007).
It all begins here! Are you serious aboul this subject and willing to learn? As a
senior instructor of game developmcnt, [ am scrutiniud daily by studcnts who
\":It, drink. and br\":ltM vidl game. I cannot CTI":Ite something that stinks. or I'll
neva- hear the end of it! So I am is motivated to teach you culling-edgt' game
de\'e1opment techniques here as I am in i real dassroom setting, by students who
are paying a lot more than the retail cost of this book to learn these concepts. I
have used this book in severnl Java courses already, so you arc guaranleed highquality material in lhese pages that will not be a waste of your time.

What Will You Learn in This Book?


This book will leach you thl" difference between Java applkations ind applets
(which run in a web browsc-r). You will thcnleam about Java's graphics classes

Introduction
and ~n writing graphia: code. You will lam how to get input from the user
and how to play sound efT~ts and music-all within the conteJ(t of an online
game. From there, the sky's the limit! Figure 1.1 shows the 8'lme you will !eMil to
create in this book. Starting with the basics (and I'm talking about eXlremebasics
here!), you will write a simple 2D game using Vlor graphics (using lines and
filled poIygolU).
You ..ill then learn new t~hniques ill each new chapter, such as how to load a
bitmap file and render it in the 20 applN window using the Graphics and
Graphies20 classes. You will eventually putlhc handful of game-related classes
together in an e'o'ent-dril'en game library. As you can see from Figure 1.1, the final

..

F"~1.1

YoIt willNm hgw 10 0Nte fils 'Ji"ll! torn wMdlI

xv

Kvi

Introduction
lPme uses some allradive arlwork and is ehlXk-full of small details! You will
learn aboul simple bitnl.;lpsand then sprites hefoR' getting into animation. Along
the w:ly, you will learn how to use Java's advall(ed 20 library to rotate and scale:
sprites. and I'll show you some interesting code tlu.t ITIOVd bulkts, powerups,
asteroids, and other game objects on 1M screen $IIlOOthly and rnIisticl1ly. The
end result is a profts.\ional sprile-based game mgine that pacb a serious punc:h!
By learning how to creale this rC'lail-qualily ra51111/ game, you will h.a\'e learned
enough to create your own games, suilable for sale in the casual game market.
No te
~ is a IWnfted type 01
reslricticJrrs, an appIel is

An

"""'d1

program
not"
III iless the
is installed like any other appIO:aliOll
~a

run in a -t> ~ Oue 10 secunl\'


lie system on a user's PC Iibo a J.wa applic.allOn,

~ 10

",ftw~.

Becaust this book is dedicale<lto teaching the basics of game programming, il


will cover many subjects very quickly, so you 'll need 10 read the chapters of this
book in order for best results. 1use a casual writing style to make the subjeds easy
to understand, and I use repetition rather tha n memoriza tion to nail the points
home. You wiJllearn by pradice, and you wiJJ not need to st ruggle with anyone
subject. because you will use thmt Sl'\'eral times throughout the book. Ea<:h
chapter builds on the one before: it. The Galactic War game developed in Parllli
back to previous chapleT$, so I recommend reading one chapler al a lime,
in order, 10 fuDy un<kntand everything that is going on. I lend 10 just use cock
after explaining how il works Ihe fim time, and often I do nol explain something
over and over again because the book mQ\'CS along at a brisk pace. We have a 101
10 co\'er in a limiled amount of space, so I recommend reading me book from
starl to finish.

men

What about the Programming language?


This book is about g;.mc programming, and il assumcs thai you alre~dy know ~t
least somc J~va. r rc<:ommend thm you acquire a Java primer to read ~forc
delving ill10 this book, or to keep handy for those parls that may confuse you. For
starters, you can pick up John Flynt's Java Progmmmiltgfor tlleAbsolutc &gilmer.
5otld &ii/jOlt (Thomson Cou rse Technology PTR, 2006). If I lose you halfway
through Ihis book, I apologiuo in advance, because we jusl don'l have enough
pages to ltach Ja~d and build a complete game. I do not utiliu any advanced
features of lava SE 6, ('Ven though this is the btest version of Java.

Introduction
Tip

n.. _
dII .. Ih5 boat _ COI'I'Ipifd n:l1MIN using 1wiI Sf: 6, buI: Illy Iat -.mm 01
Jav.lI Sf: wi! be ibIe 10 ooqJile n:I roolhe dIIlM 10 JrA's -*nt SlIpIlOI1Ior old dII (iIso
~ b.odwMds ~ , vitaIy irnportInt ...... in sottw..... dtwIopo.... n!hat Mi(ros,oh
_
to have abandoned with its products and languages-but that is' topic for another day),
While cowring some of Ihe basics over Ihe next th ree chapters, you'll create a
complete casual game in lava that runs in a web browser. which will be a
milestooe as well as a measure of your own skill kvd at thai poinL This book is
not a primer on the Java bnguage; ratber. it make$ llS(' oflhis verycapable. high
level bnguage to create games. You will lind the code in this book much easier to
understand if you have at least read a primer on th.. subject. We discuss gam..
programming, not basic Java progrornrning.
If this is your first experience with the lava language, then you may struggle with
the sourcecooe in this book. If you fed Ihat you are up to the chalJenge, th..n you
might be able 10 read th.. Jan cod.. and make sorn.. sense 01,11 of il. But I don't
$ptnd very much lime trying to leach anything about th.. Ian language beyond
the first thr<"(' chapt,"" b.allS(' game programming is a difficult sub;!, and ~
have a 101 of ground 10 cover.
All of th.. projn:ts in this book will com pile with the Jan Sf 6 development kil.
Although bter versions (such as lava Sf 7) will compile the code, older versions
of lava may complain about classes or methods thai are nol re<:ognize:d. I
recommend usi ng the v..rsion included on the book's CD ROM. (Look in th..
\jan fold..r.) You may also broWS<" 10 http://java..sun.comtosecwhelh..ran......
v,""ion of laV1ll is aV1llilabk. (As of tile writing of this book, Java SE 7 isdue out in
2008.) You will be able to rornpik the programs in this book using the javac.ex<"
program, and you will run the programs using appletviewcT.exe.

What IDE Should You Use7


The first v..rsion of this book focused on a professional IDE (Integrated {)e.c/.
orman Environment) to help organize: JaV1ll game projects.. However, much has
changed in Ihe IWO years silK<" the first edition came OUi. Borland JBuilder
Foundation was a free trial version of lBuilder. and we were able to supPOr1the
200S and 2006 versions al the time. BUI Borland is no longer making ~
versions available, and the only option now is tQ llS(' the 3O-day trial ofJBuilder
2007 Enterpris.- Edition, which is not ideal and is quite large.

xvii

)(\Iiii Introduction

I RaV'l: decided 10 Iry sonx1hing dilTt'renllhis lilTl(' around. If a revision is mnnt


updJ.tt' a book. Ihen it may seem odd 10 S1ql away from using a professional
IDE. But thai is exactly what many professional Java programmers art' doingutili7.ing a simple text edilor and the Java SE 6 Development Kit directly. There
are In,my reasons why this is preferable, and the best explanation may Ix: a desire
to al'(lid Ihe adoption of any single IDE, since there are no Slandard project files
in the industry. and each one is dr.uNtically dilTerent.

10

Tip
If yau ... famiiIr WIth . . ~1l<iDan 01_ book. yau....,. t.. ~ 10 INm N _.~ no
Ioogef using 80lland JBuiklH.1ns1Nd, _.~ WOIting WIlh lhr JOt( and a text editor lor ma>cimlKn
cr~l.patfOfTl1 wpport and tOE ~, Yoo will <Kwally giNn produttiYity as a fesUIt and
you will have mort o:onuol OV~ how Y"'I" Java applel$ behave.

Avoiding any single comp.mys IDE also aHo..., us 10 focus more on the Ja~"1l
codt'. and this helps with cross-platform dt'vdopmmt. Tht' code in this book ..iU
compilt' and run on Ihe foUo..ing syslems:
Windows XP (x86, x64)
Windows Server 2003 (x86, x64)
Windows 2000 (xS6)
Windows Vista (x86, x(4)
Linux (x86. x(4)

Solaris (Sparc 32, 64)


Solaris (x86, x(4)

n-r

Cttm-pUrbm ............" is . . abiiIy WI ~ and IIrI lhr SMIlf a:dt on


cifIemtI
~ ~ ~ oxIt M>d fllIel1f.lIllIt 8es f t ~ 00 ...., plalbms. You do lOX need
WI IfCOOlIile .,.:u J.wa oxIt for - , sy;Ifm. Ilt<a2lf lhr _
.dais fit .wi 0J'1 .... III 0I1twnI

Due to this extensive list of supportc:d operating systems, it is obvious why we


would not want to limit oUfS('lves to a singlt' IDE, but would prefer 10 support aU
of tht'st' syslems. M a conSt'q~t', 00n(' of tilt' source code on tilt' CD-RO:\1
includd any projn;t fila; il merdy includa SOUIU codt' files and galTl(' ~s
( image and sound files).

Introduction
Tip
oo will ~ using Java SE 6 On other wonh, Java Standard Edition 6). which is the IighlWei41t
version of Java best suited for ereat ng web-based games. Our text edilor of dloke is TextPad 5.0.
~ Lln (Offij'lile yoor Java SQu'te code fil~ with a maml key.

My favorite text editor. which I've been using for m3ny yeMs, is called Textl'ad
and is available on the CD-ROM in the lsoftware diredory, as well as at
www.lextpad.com. This very small and easy-to-uS<' source code editor recQgll;u$
the Java Development Kit and is abk to compile your JJVa code with a simpk
macro (Ctrl+I). By using Tcxtl'ad JS your IDE, you'll be working directly with
the file system on your hard drive rather thJn on a virtual projl.'Ct mmmger (su(h
:t.I the one in lBuilder). In TextPad, you'll see the 'tetual files on your drive, and
there is no concept of "adding" files to a "project." because you arc working with
your source files directly.

Conventions Used in This Book


The following styles Jre used in this book to highlight portions of text that are
important. You will find these highlighted boxes here and there throughout the

book.
Note
This is what a note looks like.

Not~

provide you with additional Information related 1<.l the lex!.

Tip
~

is what a tip looks like, Tips give you po<nlers about the mnen! tutorial being covered.

Caution
This Is what a ,"ulion looks li ke. Cautions provide you with guidance and whal1<.l do (or oot 1<.l
do) In a given situation.
Note
This is what a defin~ion looks like, Definitions el<,Olain the meaning behind a tedmKaI cOI'Kellt 01

"""

xix

PART

JAVA FOR BEGINNERS

The first part of the book will get you started progr..mming in Jav;!. You wiHleam
how to install and configure the Java Development Kit from Java SE 6 and test
your Java installati(JlI on your PC by writing your first gallle-all Apple! version
of ASfrroids that runs in a web browser.
Chapter I: Getting Started with Java
Chapter 2: Java Programming Essentials
Chapter 3: C reating You r Firsl Jav;! Game

CHAPTER

GETTING STARTED W ITH .JAVA

la\'3 C1n 1)<' a complex progrnmming language and a challenge to learn in its
entirety, bllt it iseasy to get up and running with lava very quickly using freelyavaitable dc\'dopment tools and basic code, I~va is one of th e most rewarding progmnming languages I have uSll'd, ~nd I'm sure you will ~gr as rou gain
apcrimtt with the language that it's worth your iml$tment oftirne. This chapter
will help you to get Sl~rted with J~n ~nd will be espn;iaIly helpful if you ha\'t: had
00 prior apcricna with this language. It e:xplains wlu.t you nd, w~ to get it,
and how to configure your S)'Slem to pl"t:J'"lre it for building Ja....based g;iElK'S.
H~ are .some of the topics that will be COVft'ed in this chapter.
Understanding Java ~nd the Web
Undcrst~nding the casual gJrTlC$ market
Installing and configuring Java
Writing your first Jav3 program

Java and the Web


let's take a look at game design for a moment and see how Java fits in, because
this is the cort" subject of the book. What truly has changed in the world of
gaming since the "good old days"? By that lenn, I am referring to the infancy of
the game industry that entertains the world today, back in the 1980s when arcade

Chapter 1 Getting Started with Java

game machines were at the top of their game. Many readers were probably bam
in the 1980s and have no recollC<tion of the games of that era, except perhaps
those games that were ported to the second-generation consoles of the early
1990s (Nintendo SNES, Sega Genesis., Atari Jaguar), You have, however, probably
seen the various anthology collections from Namco, Atari, Midway, and Taito,
featuring classics such as JOIISt, Dig Dug, Pllr-Matl, SP.lCC hWllders, Defem/er, and
others (some of which date back to even the 1970s).
Note

Ninteodo has given some 01 il'i <lassic g,lIT11" an OYe<haul alld rel~ them on the extraordinary
NinteOOo OS halldheld system. Good movel Not only are "'me da~cs. such as the original Slipet
Mario Brof., outsellir.g most other handheld 9'!mes, but rereleases, sl.'Ch as 1he legend of Zekh:
A link It> /he Past (lor the Game Boy Advance), have outsold most console and PC 9'!rnes,

Studying the Market


The game industry is pushed forward by garners, not by marketing and business
executives, which makes this industry somewhat unique in the world of entertainment. Isn't it obvious that professional sp<!rts (NFL, NBA, NHL, alld MBA
here in the States) arc not advanced or directed by the fans? On the contrary, the
fans arc often derided and ignored by not only the team franchises, but by the
organiations themselves. This is an example of how centralized management
can lead to problems. Unfortunatelr for sports fans. they are more than willing to
put up with the derision givcnto them because they love the sport. This is a level
of loyalty that simply doesn't exist in any other industry. If you love sports, )'ou
ignore all the problems and just enjoy the game, but that doesn't change the fact
that it's a seller's market (although digital entertainment is drawing fans away
from professional sports in droves).
How is the game industry a buyer's market (meaning, garners have a lot of
influence over the types of games that arc created)? Most games arc created
specifically for a consumer segment, not for the general public. The decision
makers at game publishing companies choose projects that will reach as many
core constituents as possible, while also trying to reach casual gamers and
hardcore fans of other game genres. For instance, Blizzard Entenainment (a
subsidiary of Vivendi Universal Games, which also owns Sierra Entertainment)
targets two game genres exclusively:
Real-time strategy (RTS) games: lVarCraft series, StarCraft
Role-playing games (RPGs): Diablo series, World of WarCraft

Java and the Web


Can you think of a game that Blizzard has published that does not fit into theso::
two g....nres? Blizzard has consistently hit the mark dead cent....r with thdr gam . . s in
terms of target audience, quality, polish, and subsequ....nt mass appeaL World of
W<lrCmfr has sold millions of copi....s with millions of simultaneous players
.mpport.. . d on its multitude of servers. WarCmft III has sold more than five
million units (including th.... add-ons), while the entire Wl/rCmft series has sold
twelv.... million units since its debut in 1994, StarCrail has sold nine million copies
since 1998 (including add-ons).
Why do you suppose Blizzard has b..-en so successful? Certainly not through
aggressive advertising campaigns! Garners have traditi011ally been immune to
marketing, relying primarily on word-of-mouth recommendations from friends,
online review sites, and bloggers for their game purchase decisions. If any of
Blizzard's games had not been up to par with the gmners, they would not have
continued to play the game. But the sales figures shown here reveal products that
have had a very long shelf life due to continued sales.

Design Rules
[could go into other compal1ies with equally impressiv.... success stories, as well as
those that have been dismal failures. But my goal is to demonstrate to you that
the game industry is indeed a buyer's (gamer's) market. It's not dictated and
ruled by the board of directors of one company or another or by marketing
prople, who have been stymied by the reluctance of gamers to go along with
traditional promotional theories. In other words, gamers are a tough audience!
It's an empowering position to be in, knowing that your personal preferences and
tastes are shared by millions of others who demand excellence and innovative
gamepJay, and that these demands are met, more or less. Companies that produce excellent games are rewarded by garners, while those that faU short quickly
close up shop and move on.
Would you like another real-world example? A few years ago, a IKOW publisher
. . merged in the game industry by the name of Eidos. This company's bank
account was padded by millions of PlayStation owners who had all fallen in love
with Lara Croft. Eidos seems to have misinterpreted the market, believing that
gamers loved the image and motif of this Bond-esque heroine. Eidos <:reated a
new hotshot team in Texas made up of some industry veterans in an endeavor
calle-<! loti Storm. The belief was that marketing the "rock-n-roll" hype of these

Chapter 1 Getting Stane<! wit h Java


dl"\-elopers would ltad to miUionsofprtorder sales for thtir Vmes (coming from
the succcssn of the: two Tomb Rnjdersequds).
Eidos failed to rKOgniu that ganxT$ bought into lara Croft because the: games
weu fun. not baUSl' of the iImlge. Wh~ Ion Slormwas l3un<:hi, Eidos printed
two-page spreads in ImI;or game ImIgazincs showing the tam rather than the
upcoming gamC$ in de.;eJopment. The devclopt'TS of Daikatalltl were not able to
k'p up with the marketing explosion and were derided for producing an annge
game that .....ould have been well roxcived .....ere it nOI blacklisted by gamers after
)"e,m of 1,Y1'<'. The impression was very strong lhat it W;lS all ;,bout s"les, not a
pming experience, and garners rejected that notioll. Eidos has moved on frol11
th.. experience too, having published some fantastic gamC$ such as LEGO; Star
WI!r"S. Dws Ex, and AnuchrolllU:

vmes

I n my experience, the fun factor of vidto


has risen exponentially in the Lut
two dec:ldes. along with the compleJ:ity of modem garlle$. Le'l's face it; you nn
only play Pnc-Aftm for an hour or 50 umil it becomes tedious. 1lJe Slim applies
to ITlOSt oftM dassjc arcade gal11('$. At o~ time, you wuld 6tl"\'ery vidto game
in existence in a single room, and those quarter-fueled machines were housed in
stand-up cabinets. Sincl!' that tilm, thl!'rt" have been about a half miUion gamts
created, though we might narrow down that figure to a few thousand good
games, out of which we find a fcw hundred "Hall of Famc" grl!'3tS.

The Casual Games Market


In the last two yea rs a new genre has arisen in the game industry calltd msulll

games. This genre has been relegattd to secondhand status for many years. .....hil..
the numbers of ganxT$ has risen from the hardcor<' "gttk" fans to include more
and mort" prop"'. The averagl!' gamer pb)'"$ games for a few hours a wed;, while
1M hardwr<' gamn spends 20 or mort hour'S plajing pmcs I"\.tty "ttk (lik(' a
part-time job). Casual garners, on the other hand, will only spmd a few minutes
pbying a gamt r1(lW and thtn-ptmaps I"\.try day, but r1(lt al....';))'$. Tht casual
gamer doa not becolfl(' addicted to any gamt the way a hardoor<' gamer does.
with gaffi('S such as ''''orld of warCmft, StIlr Wnl'S Gnlnxies. Tire Matrix OtJlirrr,
E""rQ"est, and so on.
So, whal is a c"sual g;l1ne anyw;]y? A c;lsual game is any gaillc that can be playcd
in a shon timcframc and requires no instructions on how to play. In this context,
almost every classk arcad.. gamc ever made falls into this category. It is only
recently that publisher and game industry pundits have btgun to rtaliu that

The

C3~ ...al Game~

Market

garners renlly don't want the long, drawnoul experience of instnlling a gnmc,
downloading ~ patch, ~nd spending eight hours karnillg how to play it. Sometimes it is rdreshing to just fire up n game nnd play for 10 or 20 minutes without
having to ,rrcw with il all evening! This was a gripe of mine for a long time. [t is
why 1spend far morc time playing console games than PC gnmes, and I'm sure
many readers share that scnliment.

No Manual Required
Yl'S, there are some PC games that are so compelling ()f innovative that they arc
worth the effort to gel them illstall~'" and running. The best ex.1mple of late is
WorM of lVarCraft. 1 have spoken to many gamers who dnim that if Blizurd's
games were not so darned much fun, they would boycott llli7.:t.Jrd altogether!
(How's that for a contradiction?) The impression I gel is that these gmners have a
lore/hale relationship with Bliz7.Jrd and many other !}1me publishers as well.
(Lest you suspe<:t that 1suffer from memory lapse over Blizza rd, let me dear up
one important point-l love their games, bill dislike their terrihle installersl).

CaS<' in point, [ could not inst all World of W",Cr<ift on my dently ~'(juipped
laptop (which has 1.2 Gll of RAM and a GeForce 6400 256-MB vidro card). First,
the installer lochxl up on a file ealled texture.mpq, and a subsequent install
attempt reported an error on disc two. 1 got around these issues by copying all
four discs to the hard drive and installed it from there with no more problems.
Howcvcr, as soon as [ fired up the game and logged in to my account, it dropped
out to download a 26O-MB update to the g.1me. When that was done, thrce more
small updates were installed just to bring the game up to the latest version. Arc
these problems tolerable! Yes and no. On the one h~nd, this is the most advanced
and complex MMORPG (massively IIWllipl"ycr Pliline role-playing gil"'C) ever
crealed, and Blizzard has a full-time team conl inually creating new content and
improving the game, which 1 appl~ud. Bll! on the other hand, thaI sure was a lot
of work just to get the game installed, and il took three hours of my time (which
is why hardenre gamers tend tn have more than one PC).
Would a casual gamer be willing to devote thatllluch time just to install a game
that will elld up requiring hundreds of hours of gameplay in order to rise through
the ranks within the game world? Most ca~ual gamers do not have the time or
patience to jump through so many hoops for Blizzard. Such is thc targel audience
for casual games! Have you ever given serious thuughtto this issue? If you are an
IT (i"for"'lIlio" ledlIIology) profe&Sional or a hardeore gamer, you are used to

Chilpter 1 Getting St,rted with Jav,

dealing with computer problems .md coping with them without incidmt, But do
you t\'er wonder, if you-a smart, experienced, knowkdgeable computer
expert-;ln- having problems with a game, how on earth ...ill an .. \~ge user
figure out thl'Sl' problems! Wdl, the short answer is, they dOIl't, which accounts
for most game returns.

Casual Garners
Casual garners include professionals such as doctors and lawyers. business
eucutives, wftware developers. and, well, n>eryollr rls<-. Casual games aunCl
pcopk from all cultures, classes. genders. religions. ethnicities, and polilical
orienlatioll$. Given lhat lll(l5I pottlltiDl game playet'$ art' THlI willing or able to
cope ...ith the issues involved in PC games. is it any wonder that this burgtQning
markt'! has hem ilJe'\1table for Sl"\~1 years now? While casual games are cur
rently played mainly in a web browser using technology such as 1.1\';1 and Rash,
the console systems ate featuring online gameplay as well, and this trend will
continue to gain popularity.

Casual Games
The casual game market was limited a f~ years ago. Only recently has this
subject started 10 show up on the radar of publisher$, schools, and retail store;,
even though gamen ha,"<' hem playing casual games for two dtotdes. (I predicted
casual games would take off a f~ years ago, but my dog ate that article.) Casual
games are a win-"in situation because they are just as easy to create as they are to
play, so the developer iii able to create a casual game in a short timefrome, and the
gamer has an enjoyable experience with a 101 of choices. Casual games have a very
simple distribution model (most arc put up on a website for online pby), a
respectable compensation model (developers receive a percentage of net sales or
a single lump sum), an often meager development cycle measured in weeks or a
f~ months. and almost no testing required. As a casual gamedevelopcr, you can
come up with an innoY.ltive gamt idea, develop the gamt, and gt'! il onto store
shelves (that is, a website) all within the timeframe of just the concept-art stagt'of
a full-blown rebil gamt.
Jay Moore is an l"V3.ngelist for Garage GaIl1l'5 who promotes the Torque game
engine around the country at conferen,es and trade shows. He spoke at the 2005
Technology Forum at the University of Advancing Technology, where he
addressed the possibility of ea rning a living as a casll,l l g.1me developer. Garage
Games' Torque engine has been port ed to Xbox, and they have published two

Installing and Configuring Java


games on Xbox Live Arcade that you can download and play if you are a Live
user. Marblf' Blilst is one such game, and Garage has m,my more games planned
for release on Live and through relail channels. In fact, when you purchase the
entire Torque game engine for SIOO, you have the option of publishing through
Garage Games, which does the contractual work and provides you with a contract (subject to quality considerations, of course}.
Microsoft has really embraced casual games by making it possible for independent developers to publish games directly on Xbox Live Arcade without going
through retail channels. Xbox 360 is the first con:;ole video game system in
history to provide downloadable games right out of the box without first purchasing retail software. If you arc interested in casual games, you can enjoy
playing on Xbox Live Arcade without buying a retail gmne at all, because many
games arc available for free trial and purchase on Xbox Live Arcade (with a
membership account, that is).
I attended the Austin Game Conference in 2005, and the focus of the show was
casual games. Microsoft's booth w;lS titled "Microsoft C1sual Games," and they
were giving away USB nash drives with the MSN Messenger SDK and showcasing
some of their Xbox Live Arcade games, as just one example.
One of the most impressive games available on Live Arcade is RoboBlilz. This
S'ulle was built using the Unreal Engine 3 (which Epic GanK'S developed for
U"rrlll TOllrtJllmcut
Ro/JpRljtz also makes usc of the impressive Ageia PhysX
physics engine to produce realistic gameplay. Another innovative g.1me 011 Xbox
Live Arcade frOm the creators of Projur Gothllm Rilcillg is Geometry W,lrS. This
game is unique and compelling, with gameplay thar involves gravity and weapons
that resemble geometric shapes.

1m.

[fyou feel as if I've been leading you along a train of thought, you would be right
to trust your feelings. The focus of this book is about programming games llsil1g
Java, and we willklrt1 to do just that shonly. Sincc Java is the pioneer language of
casual gamc development, I will be emphasizing this aspcct of gaming while
creating web-based projects in the chaptcrs to cOme.

Installing and Configuring Java


As you might have gueSS!, Java applet games run in a web browser-Internet
Explorer and Mozitla ['irefox work equally well for running Java games. Java
programs can also TUn on a deskrop system locally without going to a website.

10

Chapter 1

Getting Stilrted with Java

These typ<.-saf [,ragrams areealled fill... <lpplimtio",and require the Java Runtime
Environment ORE) to be inst~lIed. The web browser runtime is nat the .s.,~llle as
the desktop application runtime. which must be inst alled.
When you install Java Standard Editioll 6 (Java SE 6), the runtime indudes an
update for web browsers automatically. So if you write a lava game using features
from Java SE 6. the runtime might n......d to be upd'lted on an end uscr's 1'<: before
it will run the game properly. In some cascs. lhe compiled Ja'"3 progr;lm (which
ends up being a file with an extension af .d;lSS or .j;u) will run on older versions
of "lV'l, because some updates to the lava language have no impact on the
resulting compiled files.

Inst alling Java


Java SE 6 is anibble an the CD-ROl\l accompanying this book in the \IavJ folder.
You may also ,isit the fa\"J home Pilge al jJ'":J.sun.oom todownJoad the latest ,'ersion
of the JOK {s<''C Figure 1.1}. When you install the JDK, the installer will not automatiolly moditY your ~)'Stem's environment variables, which is something we will

... __. __
--_
. """,._-,

"'........ 10 "'" ~,,'_<ion Wlzord lor

JaYa(TM)OC~
" I:U6~e2

...,.. ...

............

-"'II"-,,,-,_.,,~

Figure 1.1
Ir>stalliog /alia SE 6.

IrlStalling and Configuring Java

nd to do so that you can run

Jay.l CQIllpiIeT (javac.aL") using a CQIllmand

promp( 01'" sbdI window. The CUfmlt vasion at t!'lt time of this writing is Java SE 6

w install fik is calkdjdk-6u2-windows-i586-p.aL". [f you aR using a


othtr than Winc\olo'$ you will nM;I to visit w Java website to do-.nIood the
arrropriate ~ lOr )<IUr syst~.
l!pdat~ 2, and
~'$l~

Configuring Java
lava will be installed by default in the f()lder C:IProgram Filesl)ava (on Ih~
Windows plalform; obviously, Ihis will be differen t depending on your system).
In this folder arc two folders ~alled jdkI.6.0_02 and jreI.6.0_02, containing Ihe
~eropment kil and runtime environment, respectively. We need to focus our
attention on the lirst foldCT ~omaining th~ 10K, in panicular. This fold~r contains a foldCTcallrd bin, in which)'O\l will find all of the Java programs ncedrd to
rompilC' and run Java rode. Th~ most imponant programs in this folder are
javac.C'Xe (the Java ~ompiICT) aDd appletviewer.C'Xe (theapplt! viewn utility). W~
nmi 10 add this folder 10 the system's environmenl ,.. riables so Ihat we can
im'Oke lhe Java compiler from any localion on the hard dri,'C'.
I'm going to focus my attention on Windows XP since il is the most widely used
operating system. IXpending on the system you're using, th~ steps will be differelll bm the concept will be similar. You need to add the jdkl.6.0_02/bin folder
l(l YUllr system path - the list of folders searched when YOIl invoke a program ;It
the command line. In Windows, open the Control Pand and run the System
utilily. ~[ect the Advanced tab, as shown in Figure 1.2. Her~ you will find a
button labeled Environmrnt Variables. Click on il.

Scroll down the liS! of syst~m variables until you find the Path variable. Select it,
then didr. the Edit button. Add the following to the end of the path variable:
(induding t!'lt snnioolon):
;{;:\Program Filcs\java\jd k 1.6.0_02\bin
If you have installed a differen t version. you will need 10 substitute the version
shown here with Ihe actual folder name representing the version you installed on
rour system. Click the OK button three times to back out oflhc dia logs and s<1VC
your scttings. Now let's vcrify that Ihe palh has h..-cn configured properly by
tesling the Java installation.

11

12

Chapter 1 Getting Sta rted with lilvil

S_Fl... . .
G. ......I

__

u~

H.._

~N.....

You """ be lowed M '" .... - . . . . _ 10 moI<e JrIO$l 01 ""'.. ch/rIget


,~

v...ae/ltlCl. proce.... ~_utOlIO."""~c>IIi_

~~ )

S!l\W'9

S....'4I"""R_
S_ _.tyIleIn l...... ,""" <loIiuggnp riormob>n

OK

Figure 1.2
The

S~lem

Propertie-s dialog box.

II e-

ilC(e-ssi~e

from Windows Cootroll'anel.

To 0pl?n a command prompt in Windows, open th~ Start menu, choosc Program
Files (or All Programs), and you will find it in Accessories. Thl? command
prompt should appear as shown in Figure 1.3.
If you arc using a systl?m like Linux, just open a shell if yOll don't have olle open
already, and the commands and parameters should oc the same. Now that yOIl
have a command prompt, type the following command and press Ellter:
javac -versioll
This will illvoke the Java cOlllpiler and instruct it to merely print out its vl?rsion
numocr, as shown in figure 1.4. The \'ersion reported here is just what rOll would
expect: 1.6.0_02 rcprescnts Java SE 1.6 UlXlate 2.

Installing lind Configuring Java

figure 1.]
A (~ prtIfTIpl window has

been

~.

Figure 1.4
TIle Jan oompilef printtd out its wmon numllef.

If you wtrt ablt 10 see lilt vt't'$ion number show up on your Sysltm, thtn you will

know that JaV;l has bn illSlalltd COlTtClly, and you'rt ready to slart writi ng and
compiling COOt! On tht othn hand, if you rectivtd an trror such as "Bad
comm;and or fik namc.o,n thtu your tuvironlll('Ilt ""ru.bk has not bttn set corrKtly, as expLainal pr~usIy. 11ltre is an alltmativt 10 tdiling tht syslnn's

13

14

C~apter

1 Gettiog Started

wit~

Java

mvirooment variable-yOll can iust modify the path (rom the command
prompt. However, whm yOll do this, the change is oot permanent, and you will
have to set the path every time )ou open a command prompt. But just for
reference. here is how you would do it:
set path=%pmh%;C:II'rogram Filcs\Javaljdkl.6.0_02
At this point, you are ready to start writ iog and compiliogjava code. So let's do
just that, 10 further test your Java installation. [n a mommt, we'll write a java
application that prints something out to the console (or shell). See the S<.,<:tion
Ixlow tilled "Your First lava Program."

Java Version Numbers


lava's versioning can be confusing, hut it's something you might want to
understand. The officiallatcst version of lava at the time of this writing is lava
Standard Edition 6 Update 2, or java SE 6. However, the actual version numocr is
1.6.0. Th.. computer industry is an)1hing but consis tent, given the cxtraordinary
changes thm have ta ken place on the Internet and in softw'lre development in
general. liut one thing has oc'Cn agrCl-d upon in lhe compll1cr industry rega rd ing
I'ersioning. The first release of a software product is version 1.0. Often a revision
or build number will Ix appended. such as 1.0.8392, which helps technical
support or caU ccnter personnel identify the version or sub-revision of software
that a customer is using wheo a problem arises. The revisioll lJumber is, in every
case. not part of the retail packaging and prod uct name, but rather a tracking
tool. Table 1 1 lists the ja\'3 "ersion history.

Table 1.1 mva Version


Year
, ~

,~,

'"'

'00'
,~

,~
,~

H~tory

Version

,..
"
U

,."
""
"

Marketed Name
Iav~

1.0 I JOI: 1.0


Iav. 1.1 I J[)I( l.l

~"

lISE 1.3
!lSE ' .4
USE S.O
IavJ SE 6
Iav. SE 7

--..

Code Name

....
~

'"

M."",

"

Your First Java Program


~ inlCfl.'Sting thing aoout this table is that it is very '()Jlsistent in the S('(Qnd
column (VeTSion), but there are a lot of inmnsisteneies in the third column
(Marketed Name). which is not a gnat surp~ sina marketing campaigns
sddom makes sense. If you were to follow the progression from Olle' '"CTSion to
the nat and tally them, you might note that then have hem K'VCIl major versions
of Java (while the eighth veTSioJl is 1.1, and not yet rclcascd). Produa branding is
a very C'Xpm.sive and time-<onsuming process, which is why bwincsses defend
their brand nam<'3 carefully.

When Sun released Java 2 as a trademarked name, the name caught on. and the
version we arc using now, Java SE 6, is still known internally as version 1.6.0. Java
funs love these quirks, which are not flalV5 but endearing traits, and it's not a
problem once you understand how it has evolved over the past decade.

Your First Java Program


I walll to take you through the steps of creating a new Java program (0 test the
JOK and a Java appld project to ttst web-browser integration before moving on
to the next chapteT_ Let's start at the very beginning so thaI when you ha"e
wrinen a full-featured game down the road, you'll be able to look ba,k and see
where you started.

Java Application
~ following program, shown in Figure 1.5, is calkd DrinkJava. Type it in and
save the file as DrinkJava.java.

illllO rtJ UI. tO .;


cllss Or l nkJna I
p ~ b ltc static wo t d .. t nl Str i ng Irgs()J I
Sys te. out. prj nt1n( [)o you IlkI' to dr t nk Jua 1" ) ;

p~bllc

Now let's compile the program. You'll need to open a Command Prompt
window ag<Jin, or you can continue using the one discussed earl ier if you still
have it open. Use the CD command to change to the folder where you savro. the
.java file-for instance, CD \chaptetOJ\OrinkJava. If you arc using the files on
the CD-ROM, you will need to mpy all of those files to your hard dri,'C first
be..:aUSoe you can't mmpile code on the CD. since the Java compiler ",,11 store the
rc:suIting .elass files in the same location as your source code fila..

1S

16

Chapter 1 Getting Started with Java

1/ kq' .... 1lV J..,. . : - . ." " _...., 1Ieco....

fI "., Jo_t_ S. _ _
II

""."""l_ .<__

U,,'....

ue e l _ D<.dJ..,.
_He nat'" YO.d _ IS ......... 111 l
Sn..... on ... '.I"" "","IDO <0 "" ... J""?-I,

Figure 1.5
The Drinl<Java program can be

fd i~ IIId

sawd wng

Once in the oorr1 foldcr, you can Ihen


your program:

Nol~.

lI$C

thc javac.oe program to cornpilc

J'.'c Orlnl:JU'.Ju,

lbc Java oompiJcr (ja\'a(.oc) should spend a (cw momcnls compiling )"Our
progr.un, and then mum thc command prompt cunor to you. If no ll1C$$lgc'
is displaycd, thai mcalU ('\crything wcnt ouy. You should then find a IlC'W
fill' in Ihc folder called DrinkJa\...c1ass.. You can scc thc list of filcs by typing
dir al thc command prompt, or just open Windows Explorcr to broW$(' thc
foldcr graphically.
To run the newly compilcd Drin k/ava.c1ass fiJc, you usc: the java.exc program:
J,.,OrlnkJH.

Note that I did not include the .class extension, which would have generaled an
error. Let Java worry about the extension on its own. By running this pmgrum,
you should see a linc OUlJlUl onlo the command prompt like this;
00 yOY 1He to drink J ,?

Perhaps without realizing it, wha t you navc done IKoIl' iscr....te a lava appllm/ion.
This is one of thc two t)'J1'eS of la'.. programs you can create. The otlKor type is
caJlcd an ppplcf, which describes a program that runs in a .....eb browser and is

Your First Java Program


rcally what we want to do in order to create Java games. YOIl Qln certainly write a
la"a g.lme as an application and run it on a local syst"m, but the real point of this
book is to cr"ate lava applets that run in a web browser.

Java Applet
Now let's create a lava applet and note how its code is different from a lava
application. This applet that you're about to write will run in a web browser, or
you can run it with the appletviewer.exe utility (which comes with Java). Open
}'our favorite text editor (Notepad, TextPad, Emacs, or whichever it may be) and
create a new file called FirstApplet.java with the following source code. TextPad is
shown in Figure 1.6. Before you try to run the program, though, we'll have to
create an HTML container, which we'll create shortly.

-'--

"""" """,.

oM>

:11:12 11 ~;,!,~<%q:.~

. r";',-"''''-=',,--~~-~---cr,-,--~-----o; l
"

, . ,,,,,, ,
,__
,,

w<
_1.,

', ~ '''''''l<''

""bl>

..

..,_. ....1

, 4 .. ,. , ... . "._

,,,

......wi'" ..

..

'0. "

--'-'----------------------------_.~.
Too' _01.,,,,, ,..-0'.'"

"
Figure 1.6
The firstApplet soorce code if,

Te~tPad.

17

18

Ch~pter

1 Getting

St~rted

with

J~v~

Tip
The 11';"1 edilion 01
www.textpad.com.

Te~IPad

is provided on the CD-ROM, Yoo can optionally download il from

I~port java.allt. ",


Illport java.applet.',
public chss FI rsUppi et extends Applet I
public voId palnt(;raphlcs 9) l
9.draIlStr1ng( "Thls Is lIy first Java Appletj", 20, 30),

The applet code is a lill1c different from the application-based code in the
DrinkJava program. Note the extends Applet code in the class definition and the
lack of a static main method. An applel program extends;1 class called Applet,
which mcans the applet will receive all of the features of the ApplH class (which
has tI\(" ability to run in a w!.'b hrowser). Essentially, all ofth{ complexity of tying
in your program with the web browser has been done for you through c1'lss
inhcritance (you r program inherits the (eatures of th" Appl et class).
You C:ln compile the program with this (Onlnland,
javac FirstApplet.java
Now, if you happen to be using TextPad, as I am (refer to l'igurel.6), you can
compile the program from within TextPad without having to drop to the
command prompt to compile it manually, As Figllre 1.7 shows, Textl'ad has a
macro that will launch the lava compil!.'r for you and r"port th" results of the
compil!.'. If there arc no ",rrors in the Java code, it will report "Tool completed
successfully."
In order to run an applet, you must provide an IlTMl.container. This is a basic
HTt.11. web page containing the code to eml>l.'d an applet on th", page. We'l! be
creating one of these containers for every program in the book. But don't worry,
it's easy to create an I-lTML container, and the eml~'dded arrlet COOl." is shon and
sweet. Create a new file called !'irstApplel,htrnl and enter the following code into
this file:
<htN
<head)<t I t 1~>F I fS tApp 1~t<ft It Ie)< I h~ad)
<body)

Your First Java Progrllm

<.ppln
codebilSe - ". "
code
- "flrstApplu.clilSS "
"ffrstApplet "
wldtb
- "640"
~lght
- "480"

u.

hspace
vspace

- "0"
- "0"

align

- ".1ddle"

>
<I.ppl et>
</ bodJ>< / ht_l)

This contai~r -b pa~ cock indudl'$ an C'tllbnkkd <appht> tag with paraII'I<tm that spify lhe: Java applet class file and the width and height of lhe:
........ r( ......

'".IK".......... "~ .......~ ......,01v."..p,... v,, ... p.... j.>~1

'. bill

..

--- ... ..

.....) \ ' - - -

,"..,

._,,,.1'1
-_
.
.1'1 .... _
__ .....

..
..

. _-"",';:l"'_

Figure 1.7
Compiling Java <ode is dndl in TelllPld.

"

19

20

Chapter 1 Getling Stfrted with Jav<l

---

_...

--

Figure 1.8

The fintApplft

pRlOpm

is nftIing iMidt lh. Appet

appltt, among other propmics. You can


liUso:

DOW

uriIiIy.

opm this file with Applet V~

applrt,iewer FintApplet.html
The Apple! Viewer "indow appears with the FirslApplet program running, as
shown in Figul? 1.8.
You can also open the FirslApplrt.html file in your favorite web browser. Using
Windows Explorer, locate the folder containing your project and locate the
FiTStApplel.html file, then double-dick it to open it in the default web browser.
Figu re 1.9 shows the applet running in Internet Exp lorer.

What You Have learned


Well, this has been a pmty htavy chapter thai covered a lot of material, but my
was to get the Nsics covered so that we can jump right into jaVil game
programming in the nat chapter. You learned about:

goal

Casual gam($, what tl\(y arc, and their importance

Review Questions

_... ...'- -

fIgure 1.9
RrstApp"t is IUlming .s an <.'flIbed<lro

,,~

, ~.

appI~t

in

Int~mel

ExPorer,

The Java Development Kit (lDK) and lava versions


Editing and compiling Java code
Standalone Java applications and Java applels

Review Questions
The following questions will help you [0 determine how well you have learned
the subjects discussed in this chapter.
l. What does the acronym ")DK" stand (or?

2. What version of Java arc we focusing on in this book?

21

22

Chapler I Getting Started wilh Java

J. What is

th~ nam~

of lh~ compa.ny lhal cr~aled la,'a?

4. \\'her~ on th~ web ",ill )'OU lind Inc, lext nlilor c<lned TcxtPad~

5, In ",'hal '"Car "''as la"a first rckased?


6. Where on 1M ",-dl is lhe primary download

sit~

for lal...?

7. Whal Iypot' of Ina program do you run wilh lhe ja\";l.Cl'C tool~

8. Whal lypc of Java program runs in a wdJ

bro,,~r?

g. Whal is Ih~ name of the command-lin~ 1001 used 10 run a web-hJsW lal...
program?
10, What is lhe name Oflhc paramelCT IMssed 10 lhe paint!) el'elll mdhod in 3n

applel?

On Your Own
US('

Ih~

foUo..ing ~nrci~ 10 lesl mur grasp of Ihe mJ.l~ co,'cre<! in Ihis

chapl~r,

Exercise 1
Modify th~ FirstAppltt program 50 thaI il displays your own nwssa&~ on Ihc
lo(:rC('n below Inc, CUrffnl message.

Exercise 2
See whether rou can figurc OUI "here System,oul.prinlln oUlput goes whcn run
from an applel project, SIllCC lhe It"xl is nOl displayed ill tht" applt"1 window.

CHAPTER

.JAVA PROGRAMMING
ESSENTIALS

/3V:l is a matuu programming 13ngu3ge thai offers many good features 3nd Q.pabilitiC$ that make it p<lpular on many computer systelTlli. Java is quite popubr
among Linux server programmcrs ;md adminislrators, as well as Windows application programmers. The key aspect of Java that makes it so appealing is its bu ilt-in
library support. The C++ language cannot even begin todo whal Java can do oll/of
1M box, so to speak. That is, you can't do any graphics programming in C++
wilhout an at .... 8"'phics libnary (5Ul:h <tS OpmGL or Dirt3D). Java, on the: Other
hand, has ~ing built in (while still supponing add-on libraries).

If you are jll5l getting S1aned as a java programmer. then this chapter will bdp you
gain some familiarilywith the Java bnguage. You wiD Ic:un most of the basics in this

chapter that you will need to wrile an applet-Ixucd wm game. There is a Jot more to
the lava language than what you willicam about in this sole ch,'ptcr, obviouslyl11,my entire books have been wrillen aboul java. This chapler will help give you a
jurnpstarl if you arc new to lava. If you have experience with java, you may find the
infonrultion to be: vague, bUl il is more importanl for a beginner 10 understand
ooncepts ralhcl"Ihan sundards. In addition. I will not deh~ into any features that are
new in Java SE 6. Herr is what you willlc:un in this chapter:
How to wrile Java code using appku
How to usc the java dala types
The basics of object-oriented programming
How to write java classes
23

24

Chapter". Java Programming Eswntia!s

Java Applets
Th~

are two dilT..rent types of prograll1$ you can coOlpi~ with Ja....: applications or applets. A Java UPtHicufion is a program compiled to run on a computer
asa sLllndaloTl<' application (hma the' naJ1l<'). A Java appl~. on the' othC'r hand, is
compiled specifically to run in a web browser. Java applicatiom are lm.llllly
written 10 run as ~'er programs, whilC' applm run as client prograll1$ in a
Tl<'tworkC'd environment. For enmple. Java Web Server (JWS) is a Java application that hosu wC'b pag.. fila to a .....tl browser (such as InterM1 Explorer or
Morilla Firefox). and it is comparable to Microsoft 's Internet Information Server
(115) web server and the open-source Apache' web scrv<:r.

Web Server Technology Explained


The main d ilTerence is lhat Microsoft's web :server (115) and the Apache web
:server were wrilten in C++, while JWS was wrillen in Java./WS can host regul;IT
HTML web paga and custom Java Server I'ages (151'), which are custom web
server programs written in Java. A Java applet is difTerent: An applet is a "dientside" program that runs entirely in the web browser, not on the web server. A
Java Ser...er Pages application literally runs on th.. web server and sends contC'TIt to
th.. web browser, whi~ an appl~ runs only in the web browser. For this reason,
we say thai SCf\'C!" progranu run on the' "back end," while applm run on the'
"front end."
Microsoft's 115 web SCf\'C!" has gained in popularity and marUt share in rcce'nt
yt'ars, thanks in pan to the' new .NET (prollOUlKC'd "dot n~") technology. The
latest ...ersion of 115 hosls Acti...e Ser.....r Pages .NET (ASP.NET) pages. which are
similar to Java ServCT Pages (lSP) in concept, but ASP.N~" pages are written in
Visual Basic or C# (pronounced uSC<'_sharp"). If you don't know much about
web servers and web applications, don't worry- we will only be wTiting web
diem programs, not :server programs.

Hosting Java Appfets


If you really find that you enjoy Java, then you may W;lnt to consider creating or
enhancing your own website' with Java applets. You can build an entire website as
one large appl~ or you can embed many different apple'lS inside a standard
HTML page to enhance your website. One ofthe' st rong suilS of Java is that you
don't need any special t)'p<' of web snver in order to use Java on your wrllsite'.
Java has been around since 1995. and web browsers h.a...e supported Java since

The Java language


I;l\'a 1.1. Microsoft InteTn.... Explol"eT and Morilla Firefox support lhe blesl
version ofJava. To update your browser, simply install Java SE 6, and lhe installer
will add a plug-in 10 your web browser aUlomatically. This is nc<:essary if you
wanl 10 run the exa mples in lhis book-mosl web browsers come wilh an older
\crsion of Java, such as 1.4.

Compiling Java Code


The e;\sicst way 10 compile a lava progrJm is by using lhe command-line
compiler. A5 you may recall from the previous chapler, you usc the javac.cxe
progrnm 10 compile a .java file inlo a .class file. You lhen usc IheappletviCW(:r.cxe
program in conjunaion with an .htm! conlainer file with;lJ\ embedded applct tag
to run your Java applet in I~ applct viewer program. ou an also open this
HTML lcsl file in a web browser to run the applet.
Rather lhan using a complex and large Inlegraled Development Environmen t
(JOE) 10 build and compile your Java code, I recommend using a simple leXI
edilor and t~ command-line tools instead. In l~ previous edition, we' used
Borland IBuilder as our IDE of choice. This lime around, we're using just simple
lext editors (with TcxtPad bring my cboice in WindoW$). This will impm~ your
programming skills and make you a more useful progrnmmeT, since you will nOI
be lied 10 any specifi( platform or IDE (such as JBl.lilder). A good texl editor thai
I recommend is TcxlPad because il is small and hus the abili ty 10 (ompile
(Ctrl+I):;Inc! run (Orl+3) your lava programs. The cost oflhe fuUy regislered
version of TcxlPad is II pallry sum compared to the (ost of a full IDE sudl as
lBuilder. Of counoe you can also usc any frtt lext edilor as wdl, lbough most do
not ha~ Ihe nice buill-in support for lava that TexlPad has.
Anolher pair of ahemalives thaI you may (onsider is Net Beans (www.nelbeans.
org) and Eclipse (www.cclipse.org). Bolh arc free IDEs wilh varying levels of
complexity :;Ind fcaluTC$ Ihal you may want to upgrade 10 whm your Java projects become too brgc to manage wilh a simple file system.

The Java Language


There arc lhousands of classes in Java, bUl we will only be using a few oflhem 10
build web :;Ipplets. Now then, I suppose el'en a word like "class" might be a
mysteT)' if you arc new to programming. A <:bss is a Klrt of container thai holds
both dal:;l and funaions. Do you ha~ any experience with the C++ bnguagrl

25

26

Chapter 2 Java Programming

Essential~

)~,........as based on C .,
by "borro"''ng" all of the best features of C~ + and
dropping the more difficult ~spects of the langu~ge. The Sun Microsplmls
progrJ.mmers who dewloped the 'a\d specifiCltion Crelted a langwge thai is
more of an evolution th~n something created. C ~ + is a powerful l~ngUilS<'
~ to build e'\'el),hing from cell phone g;t1l1e:$ to operating S~mlS to super
computeT simulations.. The poWeT of C+ ' makes it diffieull for ~nJ1('Q to
grasp, and ("\en profl$Sionals ....ho 1u\"C" spenl many vears "'orking with dJ.tabasn
and web applications rna)' be st)mied ....hen confronted by C.,.....-. It is a world
class l~nguage, and there 3re doz.em of compilers for it on ('\'ery computer
s)')u'm. Ilere is a list of software buill in C., ~:

Microsoft Windows

Microsoft Office
Microsoft Visu31 Studio

Microsoft Internet Explorer

!.inux
Mac OS

Sobris

Mozilla Firefox
Star OprnOffice
I

~ould

go on and on, listing thOUSillJds of operating systetm, producli\ity


Cimf d('\'ice dri\ers. compilers. .....semh!ers. interprel...rs. and
on and on.
~ppli,at;ons, ,ideo

Now consider )3va. lnere is jU5t one compiler for it, the lava Development Kit
(JDK), which;5 available f(lr the most popular c0111l'uler SyStenlS. Java is innovative enough to be calk'd a new languagc, but it was heavily innucllced by c ++.
Java is much easicr to program than C++. Java aUlomaticaHy handles memory
management for you-all )"OU do is allocate memory for new variables alld
objects, and thcn you don't reall)' need to wolT)' about freeing up the memory
afterward. ""':1 uses a thnology aIled garbou collrioll to remove unused
things from memory th,u }"Our program no ionS<"" ll<'OOs. To gh"C")"OU an analog)',
in the rnIm 0001\"3, )"ou don't e'\"m need 10 Cilrry lhe tr;lS/Kan out to lhe SIred for

The Jowa

LJngu~e

pickup beo!US(' the pro...ge colltclor just picks up all the trub thrown about in
your hoUS('. TIlt pro...y roIltcl.or is son oflike a link robot thaI seuma about
the hoUS(' st:It(hing for lrash to pick up. Whtn you an: done with your Chinest
takrout, just pilCh the container and your napkin, and the lillie trash robot will
find it and dean it up for you.
This co"ltIlead to sloppy progra mming if you spe nd many years programming in
Java and then swi tch to a more demandi ng language like C++, so Java makes it
possible for you to write solid rode Ihat dea ns up after itstlfif you wish to use il.
There is a dra~ck to garbay coUtion, though: You can't tdl it specifically
10m" to pick up the trash ( nriables and ob,itct.s that an: no longer used), only
that then: is trash 10 bt' pKbd up (as with the real-world ga.m.gt coIkctors J1lO$t
of the timt!).

Java Data Types


Let's now learn about the basic data types avaiklblc in Java, because you will be
using these data types throughout the book (and presumably for the rtSt of your
programming ca re-er).
Int~Numben

Jan supports many data types. bUI probably the most basic data [)'pC is tM
illltgtT. Integers rtprtSol"nt wholt numbers, which an: numbt'rs thaI have no
dimal point. There are several types of integers that you may usc depending on
the size of number you need to store. Table 2.1 shows the types of integers you
can usc and their attributes.
Since Java programs alii run on a wide variety of computer systems (th is is called
cross-plnrfomr suppon), you might bt' wondering whether these data type values

Table 2.1 kttegel' Oata T)'Ill'S

...".

Sift in Bits

byte
~~ort

'"

10"9

32 bot>
64 bios

-12310121
-32.768 lO 3l.161
-2, W,483,64lIlO 2,141,483,647
-9,223,3n,036,~.I7';,808 lO 9,223,371,036,854,775.801

27

28

ChaplE'f 2

Java Programming Essenlials

will b<.-Ih~ S3m~ on <:v~ry sysl~m. Aft~r all, a Java program can run on a litll~ c~ll
phone or it can run on a supe rcomputer, such as a Cray Red Storm system.
Tip

irlIomIilIon abouI ~ dled ad. _q600.arg Jar a ill: cI tht lOp SllO
on lhe ~ At Ihr tme 01 \hi. wntng. lhe net powerful a:np.rte" on tht
pIaIwt is IBM's B~"'" ~ a 1IWd-~ 280 lrillion apo<a~ ~ 1Qnll.

for

moll!

Java gelS around the dau type inconsiSlency in C++ by defining that data types
will be exactly the Arne. regardless of the compuler syMmI on which the Java
program is rulllling. Ifs lhe job of Ihe Java Runtime Environment ORE) 10
d~termjne at runtime how th~ current computeT syst~m will handl~ the data
types your Java program is trying to use, and it docs this scam lessly beh ind the
scenes.

Floating-Point Num~
There are t.....o da ta types available in Java for .....orking wilh f1oaling-poi nt
num bers. A floating-point r~'Prcsents a decimal value. 111e float data type stores a
32-bil single-precision number. Th~ doubl~ data type stores a M-bil doublepris.ion number. Table 2.2 shows th~ spOOfic.s of these two data types..
Th~ easicst .....ay to det~rrnine th~ rang~ for a numeric data type is to usc th~
MIN_VALUE and MAX_VALliE properties of the base da ta type classes. Alt ho ugh we
usc lowercase to specify Ih~ type of a nu m~fic vamble (byte, short, Int, long,
float, and double), these base numeric types arc aetu.ally instances of Java cla.sKs
(Byte, Short. Intl'ger, long, Flott, and Double). ThCTdore, .....~ can lake aped:
insid~ these base classes to find som~ goodics, Th~ MIIUAlUE and IlAX_VAlUE
properties will give you the range of values fo r a particular data type.

I have written a program caIIe1:I DataTypes Ihat displays these values in an appl~t
window. "Ilu.- output is sho.....n in Figure 2.1, and the source code listing follows..
This program is on Ihc CD-ROM in Ihe \sources\chaptcrll2\DauTypcs folder.

Ta ble 2.2 Floating-Point Dilta Types

''''
.......

SizE' In Bits
,.-S

"l.4llllUSE~lI

UE-l24 10 1.7'I169ltl4a6lltS1E+lO!

The Java language

- ........._.. ...._........ --_---

1I _ _ r....l .....

-.

...

'n-_

"~,

...,
.-..-.....
.~

_
. . .1
.._>l

_ ._. . . -

_ - ,.

...-.

!'"ogure 2.1
The OiUTl'P!51'fOl.P'" lhpiIyI; ~ ~ for NdI....-ric d.wo IyJIt..

i_j)Ort Ju "t.:
i_port ju. applH .
publlC chss Da taTypu extends

~pplet

IIpaint on tne applet window


p4lblic void painUGraphics g) I
IIselKt a nice 1000t
g.setFont(llft FonU ' Courier lew ' . Font.PUII. 12));
Ifdisplay .ini_ and .. d _ walues for n.-ric data types
g.drawStrin,( 81te
. ... 81te.MIILYALUE +. to
... 8yte.II....CVALUE.~. 201;
g.drawStrlll9( Short
+ Short.IIIILYALUE+ to
... Short ..... x.-VALUL 5. 35);
g.dra"Strlng( lnt
+ Integer .M IM_V ... LUE+ to
... Integer.MAl_VALUE. S. 50l :
g.~raw5tr1ng( Long
+ long .MIN_V...LUE+ lO
... Long.M... x.-V"'LUE.~. 65);
g.draw5lring(-flolt :. + Float.IIIILULUE +. to

29

30

Chapter 2 .laYa Programming E5sentials

... flou.Il.I,l...V.oI.lUE. S. 80);


: . ... Oouble.WIN_VALUE"' to
... Ooub\e.MA~_VALUE. 5. 9Sl;

~.dra.Str1"qt Double

Characters and Strings

There are two data types in Java for working with character data; char and
String. Note that char i.s a base data type, while String is au tomatically r:Qgnizable u a class (due to the uppercase fiT5t ldtn-), JaV3 tries to make programming easy for C++ programmers by using many of the same basic data
types in order to make it easier to converl C and C++ programs to Java. So we
have a base char and a Strlnq class; of course, as you now know, every data type
in Java is a cl:lSS already.
You define a chu V3riable like this:
cll.. stude"tgrade;
chlr exa.score ' A';
Thc char data type can only handle a single cha racler, not an cntire stri ng. Note
that a character is idmtifi~ wim single quotes ( ' A') ramer than double quotes,
oecrssary for strings.
The String data type (or rather, class) i.s vny easy to uSC,' and is used by many of
the Java library methods. (Remember, a method is a (unction.) For instance, you
have seen a lot of the Graphics class $0 far in this chapter becau$C it is the main
way to display things (such as tal) in an apple! window. Here arc a couple of
differmt ways to crrate a string;
String fuorlte~a. - Sld Meier's CiYll1utlon IV;
String usernl.;
userMlit - 'Joh n"' R. .... OOe";
In addition to supponing the phu operator for combining strings (something
that C programmers look upon with mvy), the String class also comes rquipped
with numerous suppon methods for manipulating strings. I won't go over ~'ery
property and method in Ihe String class here because thai is the role of a Java
reference book-to cover every single detail.

The Java Language


Tip
If yoo are enjoying Java <0 fa< and you thin\:. you will stid: with it you wi ll definitely need a
UlffiIl""!lensive lava language reference boolc.. lreoommend Herbert Sd1 ildt's ava: 11le Complete
Reference. Seventh Ediricrl (M<;(jrawHiII C\$bome Media, 20(6). A good inl'oductoly boo!< for
beginners is IoIIn Flynt's Jilva Progrilmming fOf rile Absolute Beginllef. Se<ond Edition {Thomson
Course Technology, 2006). If you want just a good online reference, you <;an't beat Sun's cross

linked

documenta~on

at iava.sun.coml,efereocelapi,

One good example of a function we've been using in this chapter is Graph; cs.
drawString. This function has many overloaded versions avaibble (overloading
is expbined later in this chapter, in the section entitled "Objeet.Oriented Progmmming") that give you a lot of options for printing text to the applet window,
but the main version I usc is this:
dra"String(String >tr,

Int x,

1nt,)

Note

We have just watched the surface of the Graphics class (and the Graphics2D inheritl'<!
class), This will be the focus of most of Part II. oovering Chapters 4 through 1D. Yoo willlea,n
about class inllel'itance later in this <:hap1e'.
Table 2.3 lists just wrueofthe useful methods in the String class. This is by no
means a complete list.
If you want to sec all of the properties and methods in a class (such as Stri ng ), the
easiest way to get a list, aside from using a reference book, is to create an instance
ofa class (such as Stri n9 s) and then usc the dot operator (,) to cause lBuilder to
bring up the contents of the class. This built-in "look" feature works with the
Java language classes as well as classes you have written yourself, as shown in
Figure 2.2.
Table 2.3 String Ck.;s Methods

Method

Description

contains
"odsWllh
"QU' 1S 19nOreC,se

Roltm< _

len~tn

replace
ld.

ReIlJms _

if .... <tring i< IMaioed in """""" .lting


if !tie sUing Md< witll a ..na;n string

C~,e< <bYlg< without 00I'I<i<lHing ~ or kJwer<as<'


RoluYl< !tie ~!tI of !tie 'lting
RepIa<e< all 0CCUIl'erICe< of a sulKlting will1 """tM< <UlHt,;ng in a 'Iring
Ro""""",, tlanl: spaces fmm !tie <ton and end of a .Uing

31

32

Chapter 2 Java Programming Essentials

'- -'--- ..-..- "_...


~~----------- '~"

....... ,..._ ..
...---"..

...... ,..._"

......... _--.-- ......


_-..:.

_-_.
..

.N

:.:;- .....
.~':".:

,,--

.
~-~=--

.,.__.
'-,'__N_
'__h
'-'-'
'__N'
'____._'__
~

~,
.: =::::'::'~,

.__,
._,'. . . _.
._-

... _.

.__ ,~~:~:~_:..i''='"=''~~~~,-,-'''

~_._,._'----'~- - -_M,'---1

figure 2.2
JBuilde< displays the CO/lll'/ltl of a dass witI1 a I"'IH'P window.
Possibly lhe single greatest advantage to using an IDE is the built in help system.
whkh is available in tools such as lBuilder and Eclipse. If you aren'l using an IDE
that provides context-sensitive help and class member lists, then you will need a
good rderence book or website.
Tip
If you ilfe looking for help 00 a specifk )a"a language term or class, the easiest way to 100<. up
that inf<>rmation is by U'i ing Google. $earth ' java <keyword,.' to quid<1y locate the refe<en<:e. For
example, the firsl Google result for "java graphics1d' will most likely be a llRL to one of Sun's
own Java "rle<en<:e pages. Or you tan jUll go straight to the 50IIrcc and s.earcn through the dass
listings 3t java.sun.oomIreferencelapi.
Booleans
The Boolean data type (an be set to either true or false, and it is useful as a return
value for methods. For instance, in some of the example programs you've seen so
far, there have been methods thai returned a Boolean based on whether the code
succeeded. You can declare a bool can variable like so:

boolean gameover fll se ;

The Java Language


HeTt is a short method lhat r,",urllS a Boolean value based on whtlher a value is
within a boundary of a minimum and maximum value:
publ te boo leln eheelollounds(1ong n I. IOIHJ IOller. long upper) I
If lYil (lo..u II II ) uwer)
return filse;

ehe
return true;
Here is an example of using the checkllollnds method 10 detmnine whether a
sprile's position on lhe scr~n (in the horizontal orienta tion) is within the
screen's boundary:
sprHeX spr1 leX + I;
H (eheekScunds(spritex . O. 639) -- true)
sp r HeX - 0;
This short e~ample assumes lhal the sprl te Xvariable has already been declared
eulier in lhe program. The spri te's X position on the screen will wrap around to
the left edge anyt ime Ihe spri te moves off the rig.ht edge of the screen. This
Boolean method an also be wnt1en like this, where lhe true is assumed in the
If S1alnnent:
If

lellteklloundslsprl teX. O. 639))

Note that [havt: Id't out the -- trlle in this line of code. This is possible because
lavaevaJuates the Tl'Ium vaJueofthe ehe<:kSollnds method and replaces the method
call with the rdum value when the program is running. Thus if the chUOllnds
method Tttums lrue, the 1r S1alement becomes this:
HIV",t)

This is why we can leave the -- trlle oul of Ihe equation. On the converse, when
you want 10 tcst for a false relurn value, you can inserl h i se in the if
statelllent or you can usc Ihe logical nl'gativc operalor (!) in the statement;
1f (IcheckSounds(sprlteX. O. 639))

The result is that if cheekSounds returns false, lhe I f statemenl will exute
lhe code that follows; otherwise, lhe code is nOI exuted. Speaking of which.
you may include a single line of code after an 1r slalemenl, or you may
include a code block enclosed in curly br.KCS il$ follows. This is especially

))

34

Chilpter 2 lilvil Progrilmmiog Essentiills


helpful if you "'alll to do more than one thing after a conditional statemelll
relurns true or false:
publ!c boolean check8oundsllong val. long lower. long upper)
jflval <lower II val )UppH) (
return fa I se;
I el se I
return lrue;

The use of curly br,ICes in this new version of chedBounds might not change anything, but it docs allow you to add more lines of code before each of the return
statements. (For instance, you may want to display a llleSS<lge on the screen before
returniog.)
Arrays

All army is a collection of \';)riabJes ofa specific data lype that arc organized in a
managcab\e container. lin array is created usiog one of the base data types. a Java
libra!)' class, or one of your own classes. To tell Java that yOIi want an arra)',
allach brnckets to the dala type io your variable declaralion:
I nt[] hlghScoreL! st ;
But there are 1,,"0 steps to creating an array because an array must first be defined,
and IlleIl memory must be allocated for il. First, )'011 define the data type and
arra)' variable name, then you allocate the ana) by specifying the number of
elemcllls in the arl<l)' with the new operator:
Int{] studentGrades;
studentGrades - new Int{301;
Note that I have ;l!1ocak-d enough memory for lhis arra)' to hold 30 elements in
the stUdl'ntGr~des array. You can also define a new array with a single line of
code:
Int[J studentGrades - new Int(301;
I don't know about you, but! cnjo)' writing beautiful code like this. I get 3 chill
when writing code like this because my imagination slarts to take offwith visions

ThE' Java LanguagE'

ofscrolling backgrounds and spaceships and llltlicrs and explosions, all of which
arc made possible with armys. But lhe real power of an army is made obvious
when you start iterating through an array with a loop. If you need 10 updale lhe
values of lhis army, you mighl access the array elements individually like this:
HudentGrades(O]

90:

HudentGrades(29] - 100:

If you truly need to set each element in ,m army individually, then an array can
still help to cut down on lhe clutter in your program. And an army will always
benefit from proct'SSing in a loop when it comes to things such as prinling out lhe
coments of the array or storing it in a data file, or for any other purpose you may
have for lhe array. leI'S sel all of lhe elements in an array to a slarling value of
zero (this is good programming pmclice);
long(] speed new )on9(100J:
for(lntl-0: t <100: !++) I

speed[l1- 0:
There is another way to creale an array by seuing the initial values oflhe array
righl al the definition. This army of five floals is defined and initialized in
memory with starting values al lhe same time.
float[ 1 rad1 oStattons - { sa. s. 91. 3. 97.7. 101. S. 103.0 ):

You can also creale multidimensional arrays. An array with more lhan one
dimension will have a multiplicative number of dements (based on the number
of elements in each dinl('nsion) becausc for every one dement in lhe firsl
dimension, there arc N e1emenls in the nexl dimension (based on the size of lhe
next dimension). In my own experience wriling games, I seldom usc more than
one dimension for an array because it is possible (and more efficient) 10 use a
single-dimellsioned array, and then index into it creatively to deal with multiple
dimensions.
Here is an example of a two-dimensional array that Slores lhe values for a game
level. The pound characlers (Il represent walls (or any olher object you want ill
your game) while lhe periods (.) represenl dirt, gmss, or any other type of image.
I presume that this is a level for a lile-baSl.-rl g.'me, where each character in the
army is drawn 10 lhe screen 'lS a tile from a bitmap file.
ChH[H] gaaeLevel - I

]S

36

Chilpler 2

{",

{.,

Java Programming eS5enlials

,. J.

,.J.

{., .... ' ....... ' .... "'J.

{.,............
'
{.,
[.,

,.

[.,

f ' .. ,

,.).
, .).
,.) .

,. I

Another common practice is to create a game level with just numbers (such as 0
to 9). Some programmers prefer to use character-based levels because they son of
look more like a game level. and arc. therefore. C<lSier to edit. I tend to prefer
integer-based game kvds because I am a big ran or a 1cvd ediling program called
Mappy, which aports k\'e1s as a comma-ddimited amJiY of numbers. Here is
how Milppy might aport the same level with numeric dilla:

2,2 .2. 2.2.2 . 2.2.2.2.


2,1.1.1.1.1.1.1. 1. 2.
2. I. I. I. 1. 1. 1. I. 1. 2.
2.1. 1. l. l. l. l. l. 1. 2.
2. J.I.I. 1.1.1. I. l.2.
2.1.1.1.1.1.1.1.1.2.
2.2.2.2.2.2.2.2.2.2

n,
, t - induclfd ~ . . . . bcd's CD-ROM. Ltd ... m. ~ ~ You un Inl

out more ~ Dlut this gfUI IeveI-liting IOOl at _~co.,*- HclweoIet m.


SlIbject 01 tilHlasfd saolling is ~ m. WlPt 01 this bod.
IIIl' dil=~ ~ ~ ~
memy to show you what is posslile. for an txhaostiw guidt to the subiKt I reI.... yoo to VIWiII
Basi< ~ Prograrnmjng fry TtctIS, Sccood fdirioll (llIomsoo Course Ted>rdogy PHI, 2(06),
Although this book fOCl/l.tS on \l8.NH it is one ot the ttw bookl that fJqllairtl how to build a tilf.
biill!d ~ from watth, and the concepts can be lIpplird to JiNa, should you wish to o:mte such

""*

.,....

Un you I11ake out the simibrity bctwcm the two game Icvds shown hen? It's all the
same data, just rcpresmlN differmtly. When Mappy aports a 1n-e: Iikc this, il
sends !he dau 10 a text file that )'OU can then opm and pute into )'OUr game's
source cOOe. To make it work. )"Ou would dd1nc an array 10 handk the d.lla Iik this:
tnt{][) gl.eLfyel1

{2.2.2.2.2.2.2.2.2.21.
12. I. I. I. I. 1. 1. 1. 1.21.
12. I. I. I. I. 1. 1. 1. 1.21.

The Java Language


12,1. I. I. I. l. 1. 1. I. 2l.
12,1.1.1.1. I. l. 1,1. 2l.
12,1.1.1,1. I. 1. 1. 1. 2),
12,2,2,2.2.2.2,2.2.2)
I,

Tip
Ooo't forget the semicolon at the end of an array dedaration, or yoo wi ll get some vel)' strange

errorl f,om the Java compiler.


However, I prefer 10 treat a game level (or other array-based da ta .sequence) as a
single-dimensional array because data like this is easier to work with as a onedimensional array. Here is how J would define it:
lnt[] gameLevel - 1

2. 2.2.2,2,2 . 2. 2.2,2,
2,1.1.1.1.1.1.1.1. 2,
2. 1.1.1.1.1.1.1.1. 2.
2. 1.1.1.1.1.1.1.1. 2,
2,1.1.1.1.1.1.1.1. 2,
2.1.1.1.1.1.1.1.1. 2,
2. 2.2.2,2.2 . 2. 2.2.2
I,

Do you see the subt le difference between this lD array and the 2D array defined
before? AliI need to know are the width and height of the array data, and then I
don't need multiple dimensions. In this example, this game level is 10 tiles wide
and 10 tiles deep, for a total of 100 tiles. (A tile is a small bitmap used to build a
game world in a 2D scrolling game, and it very closely resembles the analogy of
noor tiles in the way it is used .)

The Essence of Class


In case you haven't noticed, I've been talking about classes a lot. That's because you
can't really get around the subject when writing a Java program. The main part of a
Java source code file itself is a class. You might have seen a C program before and
you might already be familiar with the maln() function. Here is a simpleC program:
intlllain{1nt argc, char argv[]) {
printf("1 alll a C progralll . In ' ):
return 0:

37

38

Chapter 1.

Java Programm ing Essentials

Let's take a look at the same program written in pure C-t + :


'I nclUde <iostrea.>
1ntru in (int argc, char argv{]) l
std , :co ut
"I a. a C++ prog ra ll. " c: s td: :end l :
return I :
Now take a look a\ the same program written in Java:
importj ~ v a. io .:
p~b l ic C\HS S~lIp le Java

public st atic void m~jn(Strin g ar gs[lll


Systell .out. pr int In("1 all a Jav a program ") :

lXl you Sff any simibrities among these programs? You should, because they are
listed in C"o]ut;on;uy order. Now, I don't want to get into an argument with
anyone about whether Java is truly an evolutionary leap ahead of C++ lJause
I'm not sure if I believe that in the strictest sense (with a feature comparison). Hut
I do like to think ofjal'a as the next logical step above C++; it is easier, less prone
to error, but not as powerful. I)ocsn't that describe ;lny system that tends to
evolve ov{'"r timd Take the compuwr industry itself, for instance. The earliest
computers were built with thousands of vacuum tubes, which were difficult to
maintain 'lnd very prone to error; and as far as power consumption goes, 1think
the computers of old definitely usro more power than the computers we commonly usc today- but let's not talk about performanc{'", which is no contest.
The C program is quite simple and llIaybe even readable by a non-programmer
(who may nOI underst,md anything other than the pr l ntf linc, alld even then
with much confusion ). Thl' C++ program is so much gobbll'dygook to anyonl'
but a programmer. But those of us with a C++ background often describe C+ +
code as beautiful and elegant, with a powerful, perhaps even imimidmillg, lure.
The Java program is very similar to the C and C++ programs. Likl' the C+ -t
program, the Java program must 'gt"t solllething" frolll "solllewhere else" in the
form of the illport j ava, 10,' statement. This java.io is a library that provides
access to the Sys tem. Qu t class, which is uscJ for printing out text (as you
probably guessed ). But the biggest difference is thatlhe java program is located
inside a cl"sl. This class is called SaJlpl eJa va, and inside this class ('nclosed with

The Java laonguage


curly brace$) i$ a main function very similar 10 the main funclions found in the C
and C++ programs.
Whal is this SallplNan class, you may a.sk~ The troth is. n'e?1hing in la\1I is a
class, and it is not possible todo anything usdUl in Java withoul using a class. AU
source code that you write in Java will be enclosed inside a class definition.

The .a1n Function


The core of a Java application is Ihe Hin function. (Note Ihat applrts typically
don't have a lWin funclion, as I'll explain shortly.) The lWln function has this
basic format:
pUblIc Hltle voId -.aln(Strlng i1rgs[]l 1
I

The parameter (String ugs[]) allows yOll to pass information to the Java
program and is only practical when devclopinga Java application (rather than an
applet) to which you can pass paramel('rs, pre$umably from a command prompt
or shell. You alii pass parametCTS 10 a Java applel, but that is not done very often.
I once worked for a company that built vchicle tracking systems using GPS
(globlll posi/ioning ~mn), and my job was 10 maintain the: la'.. progra.m that
displayed a map with all the vchicks in the Slate of Arizona n'l()\"ing along their
routes. This Java program reivtd "chicle tracking informa.ticm from a $CrYC'<,
and then displayed it in an apple!.
Caution
Jar<a applfts don't roeed a Hi n MlcIlOII beause thm
~ inswd (such ~ Intt nI paint).

1ft

sewnI _rs tNiI 1ft Jound III

in

Let's dissect the lWin funelion 10 help you better undCTStand what it does. The
term pub! ie specifies that this (unction is visible outside the class. (Remember,
every JaV,! program runs inside a class.) The static tcnn specifies that the
function definition never changes and is not to be inherited (borrowed for use in
another fUlletion). The stHle keyword is optional and 1I0t often used in an
applel. The void term means thatlhe function docs not return a value. Every Java
application you wrile will have a Hln funCiion, just like every C and C++
program. However, a lava applet, which runs in a web brOW5CT, conlains events
instl'ad and is not in complete control in the same way that a siandalolll' Java
application (...i th a main method) is..

39

40

Chapter 2

Ja~a

Programming Essentials

However, you eml write Java classes that dOII'1 have a m~1 n function. Why would
you want to do that? A class is usually created to perform a spedfk task, such as
the handling of sprites in a game. You might write a sprite class that knows how
to load a bitmap fik and draw a spri te on the screen; then the main Java program
(with the m~t n function) will COl/Slime or use the sprite class, which itself has no
"'ain function. A class has its own variables and functions, SQme of which are
hidden inside the class itself and invisible outside the class. What I'm describing
here are some of the key aspects of object-oriented programming, or OOP. In
some cases, as when developing an applet, you may just usc the pai nt event rather
than using a Ilain function. (More on that later.)

Object-Oriented Programming
There arc four main concepts involved in OOP, though you may not use all of
them in every class you write ,

Data hiding
Encapsulation
Inheritance
Polymorphism
I'll briefly talk about each of these concepts because you will bedealing with these
throughout the book. I don't spend a lot uft ime discussing advanced concepts
like these while writing lava games, and this book is not int....ndcd as a primer on
the Java language. Hundreds of books have been written about Java programming, includ ing some very complex textbooks on the subjed used in college
courses,

Data Hiding

Onta hidillg is a key concept of OOP because it provides a way to protect data

within an object at rumime from direct manipulation. Instead of providing


access to certain pieces of data, a class defi nition will include functions (oft....n
called methods or arcessorslmmllfors in OOP lore) for retrieving and changing
data (often called properties) that is hidden within the class definit ion. An
accessor function retrieves a hiddel1 variable; a mutator changes a hidden v:uiable.

The Java Language

This way, the programmer can specify exactly what changes can be made to a
private variable through the built-in mutator functions and return customformatted data through the accessor functions. For instance, if you W'lnl to make
sure that a birth date is valid, the mutator function can restrict changes to a
certain range (such as 0 to 120). The following source code demonstrates the
concept of data hiding. [ have intentionally kept the code listing simpler by not
induding any comments.
publlc class vehlcle {
prtv~te Strln9 lI~ke:
prtnte lnt nUlIIWneels;
publlc String getM~ke() {
return . ~ke:

I
publIc boolun setM~ke(Strlng newake) I
if (newlOake. length() ) OJ I
luke - newm~ke:
return true:
I el,e {
retucn fa I se;

public Int getNumWheelsO (


return nu....hee l s;

I
public boolean setNumWheelS(lnt count) I
If (count) 0 U count <20) I
nu....heeh - count:
return true;
) el se I
return false:

Encapsulation
IlCllpslIlllrioll is related to data hiding in that it describes how information and
proce&SCs are both handled internally by a class. These two concepts are often
Ilsed interchangeably, depending on the opinion of the programmer. (l prefer

41

42

Chapter 2 Java Programming Essentials


to use the term rllC/lpSlIlariolt rather than dara hi/liltg,) I would suggest that
encapsulation invol,'es mod-ding a real-world entity. while data hiding describes
the ability to U5e private variables in a class. It's common to en(;lpsulate a realworld entily by writing a class thai describes the data and functions for working
with that data. In the vehicle class example:, I haveeocapsulattd the specifiC-limns
for a basic "ehick inside a class with hidden (or private) dala members and
public functions {or methods}.

Inheritance
Inheritance describes the ability to reuse dass definitions and to make changes 10
a subclass that relies on a base dass. For instance, the vehicle class might Ix used
as a basis for many subclasses covering a wide range of vehicles, from twowh~l
motorcydes to ISwheel semi trucks. When you are writing the code (or a dass, it
is best to pili each d;]ss inside its own wurce code file. Java allows yOll to inherit
from a single base dass.
Not .
A/though C++ ~ you III WlfriIIrom ~ ba5e dMses,. dlis leaun oftfll CiIMS_
problems tharl ~ soMs. so it IS wIdom tMd. ~1Nd 01 nUople inherilarn. J;wa * - you 10
1lIerf~_ ~ far thl' ~ and methods that shcUd be IoI.nI
WIIhin " partjaUr dtis.

.... nUupIe

For inslance, the SimpleClass program in Uources\ch"ptcr{)! on the CD-ROM


includes the source code listing for the wehlcle dass. and it is stored in a file
called vehidc.ja'-a. Also included in the SimpleOass project is the main source
code file (;llltd Simpl~ass.,iava-:andthis file "consumes" or uses the wehlcle
d"ss defined in the vehicle.jalO1 file. Additional classcscan be: written and $31'td in
their own source files.
TIp
To add a new dass 10 your prlljK~ just ae.te. new text file with" .java eXleflsion and compi~ ~
s.eparately from your main program f i ~, Th;s;s vet)' easy 10 60 using TeXlP<>d by prenirog Ctrl+\ to
(ompile)'OUr J code,

A "O'lSlmClOr is a method that is called whenever you creatt> a new class in your
program. I'm nQt lalking about typing in a new claM, but when a class is instan
tiated into an object at runtime. When a new class is created (with the new
operator). the class dmnition is wed to construct an object, ~ where the keyword comes in herr. 111<' new object is "constructed" when it is being crated at
runtime; the class is a blueprint wcd to build or construct the ob:it at runtime.

The Java Language


Definition
1lJjldl1lid~ means to CA!ate or to coostruct Within the cootext of objKt-oriented programming.
new objts a<e i nstan~ated whe<l they are created at run~me from me blueprint specified in a
da" definition (,uch as tile ~eh I c 1e das,).

When I click the OK button on the Class Wizard dialog box, a new file called
truck.java is added to my project, and it contains this source code:
pUb lIC chs, truck extends vehicle
I
public truck()
I
I
This is a nice, clean 'tarting point for a new class. Note that this class inherits
from the veMcle class (extends vehicle), and it include, a simple constructor
(pUblic truck()). Thi, constructor is called whenever you use new to create a new
truck obje<:t, using code like this:
tru ck silverado - nell truck():
The constructor i, ,pecified after the new operator in this line of code, and this is
called an emply COIlSlnlClor. If you want to pass parameters to a constructo r, you
can do so by defining another version of the constructor, which is a topic that
needs to be covered in the next section on polymorphism.

Polymorphism
Polymorphism is a complex word that, when broken down, equates to poly
(" many") and morpl! ("shape"); therefore, polymorphism refers to having
"many shape" or "many shapes." Java allows you to write many versions of a
function (or method) with different sets of parameters. When you write more
than one version ofa method, you have overloaded that method. Overloading is a
te<:hnical programming term that describes polymorphism at work.
Tip
I haw been Iffing tile terms fulKlioo and mell10d togetl1e( up to this point I wi~ refM to mell10d
ffom th i, point forward. Just note that a method i5 the same as a function. and thi5 applies to
accessor/mutator functions (terms that are used by C+ + programmers). Just r""",mber: A
property i5 a variable. while a method i5 a function.

43

44

Chapter 2 Java Programming Essentials


The complete truct c1a5$ $Ource code listing demonstrates polymorphism. Note
the construdor, truckO, which has hem overlooded once with an alternative
version with the following syntax.:
publIc tructlStrlng .. ke. Strl.... .ooel . StrIng engine. Int towIng)
You wiD probably not pus all of the data to a cbss in this manrocr all at once, U it
is not usually "cry practical. You may pus any vallle$ to the constructor tlut you
think will hdp with the initiawtion of the object that is being instantiated, but
keep in mind that t~u au methods avaibble for reading and clunging thoSC'
variables (or properties) u well.
Do you see how the default constructor includes $C\Ieral method calls to sellhe
private variables to some initial values? This is a good practice to do when
creating a class definilion, because it eliminates Ihe chance of a null.pointer
runtime errOT from occurring-which is common when working with strings
Ihal have not yel been sellO a value. I've decidtd not to include a string lenglh
check in the set functions to make the source code easier 10 read, but this sort of
builtin error handling is a good idea.
publlc chss truck utends vehIcle {
prlnte Strlng .ooel;
prlute Strlng engIne;
prlnte lnt towlngClp.clt,;
publlc truckO I
setMitfl "..ke"l;
seU"""ee!sl41;
selllode11"lIOde1 );
setEngl nel "engl ne"l ;
set T0111 ngCopicl t,tO) ;
publlctrucklStrlng_i.e. StrlngllOdel. String engIne. lnt tOil Ing) I
se tlla.e(uke);
setllode1(lIOde1) ;
seUngl nel engl ne) ;
set T0111 ngCipiC Ity! towl IIg) ;
publ1c StrIng getllodeHI {
return .ooel;

The Java language


)

public void Ictllodel(String newDdel) t


-ode I ntdOde I :

public Strlnt tetEntlr>eO t


return tn9lne:
)

public .old ICUll9lntl Strl ng IItWt1lglnt) I


tllglnt ~Int:
ptlbllc Int getTowlngCipiclty() I
return towlngcipaclty:
)

I*bllc void setTowingCipiclty{lnt ulue) (


towtngcapiClty ..lue:

Now Itt's make some changes to the main source code in the SimpleC1ass.java
file. This is the part of the program that consumes, o r uSC$, the vehicle and truck
classes. Here is the complete listing:
101portjna,lang,":
1.port java.a ppl et,";
1.port jiva,a"t. ";
publlc clUS SI~leChss utends ....pplet 1
vehlcle car;
truck llthtntng;
ptIbllC void InitO t
lIinl thl he i vehicle object
Cir - new vehiclel);
Clr.utMahl ford ):
clr.seU~heeh(4):

Illnltlallze. tlvck object "II", I constructor


llthtnlng - new trvcuoford SVYo. f ,ISO lIthtnlng. S,4l Triton '8 ' .
7700):
)

4S

46

Chapter 2 Java Progr<lmming EIS<!ntials


f l1et' s use a nice bl 9 foot

g,letfont(new foot('Verdana", foot.BOLO, Ill);


fldhphy the car lofo

9.drawString( 'Car luke: + car .getMake(). 20. 20) :


9.drawStrl ng( 'Number of IIheel s: " + car. getNulllWheel s(). 20. 40):
Iidisphy the truck info
9.drawSt r1 nq( TruCk Ma ke, "+ 11 ghtn 1nq. getMakel ) . 20. 70):
9.drawSt r1 nq( Truck MOde I: " + 11qhtn 1nq .qetMode l( l. 20, 90),
9,drawSt r1 ng( Truck engl ne: + 11ghtol ng .geHngi neO. 20, 110);
9.drawSt ri nq( Truck talll n9 capacHy: " +
1Iqhtnlng.getTolilngCapacltyl). 20. 130):

What is the most significant part of this program lhal might seem unusual or
surprising? Well, take a look at those last fe'" lil1es of code where the lruck
information is displayed On the screen, The truck is using a method called
getHake() thai is 110t evell defin~xl in thl" trud class; lhis isa 11lelhod found only
in the vehiCle class, from whkh the truck class was inherited. Thai is the real
power of inheritanct'-tht' abilily to reuse fUl1clionalit}" while enh:mcing existing
c1a~.

I ha"e added the truck class to the Simpll"Class program, which is where the
vehtcle class may also be found. You can open the Simpll"Class project from
Ihe CD-HOM in the \sources\chapterQ2 folder. Figu re 2.3 shows the oulpul from
the current version of the program up to lhis point.

What You Have Learned


This chaplCr providt."d an o,'ervi...w of th... b"sks of Jav" programming. You
le"rnt."d about th... differences between a Java :JpplicatiOl1 "nd" J"v" applet, and
how to write programs of e"ch type and then compile "nd run them. You kMl1ed
the basics of object-orienled programming "nd many other J"va programming
issues th"t will be hdpfuJ in later ch"pters. Spific:Jlly, this dmpter co\"ered:
How to write a Jav" application
How 10 write" J"va applet
How 10 compile a Java program

Re~jew

-..........

Questions

_,_..

c.......,. ...
~

' ..........."

".

'"

,''''''''_'',>0''''_
.....
,

,,_<_, "00

--

figure 2.3

The SimpleClass program now demonstrate! inheritartCe with the

tr"c~

(lass.

Review Questions
The following questions will help you to determine how well you
the subje<:ts discussed in this chapter.

ha~e

learned

I. What is the name of the JDK tool used to compile Java programs?
2. Which JDK command-line tool is used to run a Java application?

J. Which ]DK command-line 1001 is used [0 run a Java applel?


4. What are two good, fret: Java IDEs recommended in this chapter?
5. Encapsulation, polymorphism, and inheritance are the keys to wha t programming methodology?
6. What's the main difference between a Java application and an applet?
7. Which method of the Graphics class can you use to print a text message on
the screen?
8. How many bits make up a lava integer (the int data type)?

47

48

Chapt~

2 Java Programming Esst-ntiab

9. How many bits are thf're in a Java long in leger (th~ long <!ala type)?

10. What progr:amming language was lava OOSW o n1

On Your Own
Use Ih~ follo...;ng aercises to test your grasp of th~ material
chapter.

CO\~

in this

inh~rited class 10

try out

Exercise 1
Writt your own la"" class and then use it to almd an
tht concepts of inhC'ritana and mapsulllion.

Exercise 2
Modify your new dass by addingsomc methods Ihat dcmonstratc the concepl of
polymorphism by wriling sever-II versionsofth~ SOIm~ mdhod with different sets
of panmders.

CHAPTER 3

CREATING YOUR FIRST


.JAVA GAME

This chapter will give you a glimpse of whal's coming in the ne:xt few chapters
while teaching you some of the basia of game creation. The gam{'" featured here
was inspired by the classic Atari game of A5leroids. My first video game syslem
wa, an At ari 2600, and [spent many hours with il. In this chapter, you'll learn
how to creall' a varimion of this classic game, which will be th{'" basis of a more
advanced game later on when we get to Part 111. Here arc the key topia in this
chapter:
Creating an A5leroid5-slyle game
Writing key classes: BaseYectorShape, Ship, Asteroid, and Bull et
Writing Ihe main source code
Calculating velocilies on the fly

About the Game Project


Our game project in this chapter will rlln in a web browser window wi lh a
resolution of 640 x 480 and will be done entirely using vector graphics. It will
h,we some features that you have not yet learned aoout, bu t the exposure to this
code will be helpful to you. I want to introduce routo some of the concepts early
on, before you have learned all oflhe prerequisiks (otherwise we wouldn't be
able to creale a game until about halfway through the book!). You may not

"

50

Chapter 3 (reating Your First

Ja~a

Game

figure 3,1
This Asteroids done is the basis for a much more .lITIbitious game.

understand e~erything in the soun:e code for the game at this point, but you will
learn how it works in time. Figure 3.1 shows the completed game you will build
in this chapter.
As mentioned, the game is cntirely based on vc<:;tor graphics. The player's ship,
the asteroids, and the bullets are all rendered as polygons, as shown in Figure 3.2.

Definition
Vector graphics dis.plays are diff~Nlt than ou, modem monitors in that t!Ie'I d,aw s~ based
OIl enlire lines. On the othe' llaod. 01"" mode,n displays (i"aw f35ler grifPhics based On pixels.

All of the objects in the game are moved using an algorithm that calculates the X
and Y velocity values, which are used to update the object's position on the
screen. All of these' values use tloating.point math, and the result is tluid 2D
rotation animation and realistic movement (see Figure 3.3).
Each of the vector shapes in the game has a built-in bounding reclangle that is used
to simplify collision testing, which is a crucial aspe<:t of the game (see Figure 3.4).
Without it, the bullets would not destroy the am'roids, and the ship would be
invulnerable! Collision testing is what makes gameplay possible in a game.

About the Game Project

Fi'ilure 3.2
The objects in the

~ a'

all

~ed ~ ve<;l()l' gr~pl1ks.

Figure 3.3
FIOlIting-point IT\o1th algorithms give the gam.

~ 'f~l;sti<:

look and lefl,

51

52

Chapter 3 Creating Your First bv" G"me

f igure 3.4

1lQurdnl.! reaangIes _

lMd lO delKl

when to/lisiclm

O((UI".

The goal is to modify this game concepllatu on to come up with a high.quality,


polidlm game wilh a 101 of inlfie$ting gamepLay fe:ltures (such as POWeT\lPS) by
the time you've finidlm the book. The fiNI version ofGabctic War isan aradc:styk game with JTWly diffuent type of ;l5teroids, mimoltm aplosioTl$, and
poWttups for the playn'5 d1ip. Whm "'-< 5t3rt working in earnest on Galactic
War, well nuke a tnnsition from ,ector graphics (based on polygons) to nsluized graphics (based on bilmaps).
But befote you can run, as the old 53ying goes. you have to learn how to walk.
Thischapter teaches you how to walk, and you will gl'lldually improve the game a
little at a time, starting with the next chapter.

Creating the Game


This game is d ivided into five classes. Does tha t seem like ... lot of classes for just
your first game? [ thought about thai for a while, consideri ng this might be too
much code all at once. But I think you will enjoy it. This is ... complete game, for
thll! most part, so you ClIn aaminll! it-pore OVeT the lines of codll! in Holmesian
Slyle (with a magnifying glass. if you wisb ) -IO learn the secreu of how a game is
made. You are presmtm with a m~ayof sorts-a complete game. Your task is

Cruting the Game


ITVersc-engineeT the situation to determine, Step by step, what ~ents led up to
the romple1e game. The main cbss, Asterotds, contains Ihe main source code for
lhe game. Four additional cbsses are usai:

10

lueYectorSh.pe
S~lp
B\llin

Creating the Project


You can type in the code for each of the dasses (a nd the main sou rce code file,
Asteroids.java) and then compile each file into a .dass file using the Java
Development Kit (lD K) command-line tools. The compiler is called javac.exe.
You can compile a file by simply typing:
javac Asteroids.java
and IiUwise for the 01heT source co& files.
I recommend using TatPad if you are a WindoW$ ustT because of its very
ron>"C'nitnt support for the JDK.. ....'here you ClJl compile your Java program with
QrI+1 and run it with Ctrl+3, Just note that in order to run an appkt-bued
prognom, you must creale an HTML ronlainer file, as described in the fi1'$t
chapter,

The BaseVectorShape Class


The three main objects in the game (the asteroids, the bullets, and the player's
shi p) are all derived from the BueYectorSh.pe class. I origi nally wrote this game
without the base dass, and in the end, all three of the game objects (the player's
ship, the bullets, and the asteroids) ended up sharing most of their properties and
methods, so the 8ase~ectorSh.pe class was a way to clean up the code. In the
end, I put a lot of usefu l methods in this class for handling the needs of thi5
vector graphics game. lIy doing this, I have usai the object-orienled fealure
called inhtritllnu."The Asteroid, Ship, and lullet classes are all derived from
8i1se~ectorShilpe, which contains code thai is sham! by all three. As a result, the
co& for the three su~ is quite shon in each C2St.

53

54

Ch apter]. Creating Your Fi rst Java Game


This g:lme detects w llisioll s betwee n the a~teroids. bu llets. and player's ship, so
each vecto r shape in the game includ es it s own bou'lding rectan gle. While the
getBounds() method is not found in the BaseVectorShape class for reasons I'll
explain in a moment, this method docs use the geU< l and gety() methods from
the bas<' class to cakulate the boundin g rectangle. Thi s class basiGl ll y w ntain s all
Ihe variables Ihat will be used to move the ObjfftS around on the scr~....n, such
as Ihe X and Y position, the vel ocity, the fa cing and mo,-ing angles, and thc shape
itselr (which is a polygon ).
Tip
What il booooing 'cd<lnglc (01Ii~on clet lion? This phrase clesoibM the prOU'SI of detecting
whefl objects collide with each other in the game (wth <IS a builet h,tting an astl'loid) Uling
rect.lngular shapes thai surround or cOOlain tile shape. AI a r~ult the shape is boorld within thaI
lcd<logle, SO to lpe.lk.

i llport ja_a .awt .Shape ;

f................

..........................................................
hse vector shape class for for pol)gonal shapes

pubHt class 6~seVectorShape I


f/varla bl es
private Shape snape;
prIvate bOol ean al i ve ;
private dOub l I' x ,J ;
prIvate doub l e _elL velY ;
pr Iva te doub l e IIOveAngl e. faceAng 1e ;
I f ~ ccessor Ile t hods
public Shapeget5hape(} I return shape; )
pu bl Ic boo l ean IsAlheD I return al1ve ;
publ i cdoublegetX() (returnx ; 1
publ ic dou ble getY() I return) ; 1
pub1 ic double getVe1l0 I return vell ;
publ it doub l e getVelY() I return velY ; I
pub1i c doub l e getMoveAngl e(} I return IlOveAngl I' ;
pub1i I' doub l e getF aceAngl e(} I retu rn f aceAng l I' ;
f/~utator ~nd

nelper IIIl'thods
public voId setS h ~pe(Shape shape) 1 this . shapee shape ; l
pUblIc void setAllve(boolean alive) I this alive - al he ;
publ1t voi d setX(double xl \ thls. x - x; \
public void IncHdauble. j \ l ttlis ,J> .... j ', \

Creating the Game


publ i c void setY(doub l e y} I thl S.1 y: I
publ i c void lncYCdoubl e i) I thl S.1 <- i; I
public void setVelX(double velX) I thls.vel~ - vel~:
public void IncVelX(double 1) 1 thls.velX <- I; I
public void setVe)Yldoub1e velv) I thls.velY - velY;
public void incVelv(doub1e 1) I thls.velY <- 1; I
public void setFaceAngle(double angle) I this. hceAngle public void IncFaceAngle(doublei) I this.faceAngle<-i ;
pub11 c vol d setlloveAng1e (double ang1 e) ( this .lIOveAng1e public void lnclloveAngle(~ouble I) l thls.lIl()veAngle <- 1:

angle:
l
angle;
I

IldHault constructor
I
se t Shape(null ):
setAllve( fal se):
setx(O.Ol:
setYCO.Ol:

6He~ectorShape()

seUel~(O.O):

seUelHO.O):
setMOveAngle(O.O) :
setFaceAngle(O.O) :

The Ship Class


The Shl p class handles the shape, position, and velocity of the player's ship in the
game. It includes its own bounding rectangle, which is calcu lated based on the
custom polygon shape for the ship. The Ship class inherits all of the public
properties and methods from the 6ase~ectorShape class.
I. port java. awt. Po lY90n:
Illport j aVI awl. Rectangle:

I....
..........................................................I

pub! Ie class Ship extends BaseVectorShape I


IIdeflne the ship polygon
private lnt(] shlpx-I - 6. 3. O. 3. 6. 0 J:
private lnt(] shlpy ( 6. 7. 7. 7.6. -I ):
IIbounding rectangle
public Rectangl e get8ounds{)

55

Sf;

Chapter 3 Creating Your

Fi~t

Java Game

Rectangle r;
- 6. (int) getr() - 6. 12.12);

r - neWRect~ngle((lnt)getX()

return r;
ShlpD (
sel5hape (new Polygon (Shi px. shl py. sh Ipx. Ieng th) ) ;
se tA1ive{ true) ;

The Bullet ctass


The Bullet class defines the bullets fired from Ihe ship. II is also derived from the
BaseVectorShape cbss, so most of Ihe functionality of Ihis clar.s is provided by
the base clar.s. All we r.::ally ne<.'d to do for bullets in Ihis game is to define a
rectangle that is one pixel in widlh and height. This small shape is used to calculate
the bounding r~'Ctangk returnt'd in Ihe getBounds() method. While we're only
drawing a rectangle the size of a single pixel, we will still Ireat it as a rtangle, but
when it's time to ched; 10 see whether the hulle! has hit ,m aSleroid (using
collision delcction), then we'll do it slightly differently than the way in which we
compare collisions belwttn the player's ship and the asteroids. Instead of
checking for an intersection, we'll see whether the bullet is "contained within" an
asteroid.
I_port Java ~wt.;
Illport Java awt.Reetangle;
/

.
.......................................................... ,
Bull et class - polygonal shape of a bull et

publ Ie class Bullet extends BaseVectorShape I


IIbounding rectangle
public Rectangle getBounds() I
Rectangle r;
r ~ new Rectangle( (Intlgetx(). (lntl getv(). I.
return r;

BulletO (

Ilcreate the bu\ let

sh~pe

1);

Crellting the Glilme


setSll.pel_ RKhll91elO, O. l. Ill;
selJ,.11nthlsel;

The Asteroid Class


Tl1e Asterol d class also inl1erits from easeVectorSn ape and provides three of its
own new methods: gelRotnlonVelocity, setRotltionYelocity, and geteounds.
The rotation vclocity value is used to rotat e the asteroids (whkh is a cool effect in
the pme). ~ get8O\lnds method returns the bounding rectangle for the asteroid
and is similar to the $;lime method l'ound in the Ship and 8ullet classes.
Il11POrt
Il11POrt

jiIY
jiIY

t.'olygon;
t.Rectingle;

....
..........f

f ~
Aste ro1d c1lss . for polygonal asteroid snlpH

publlc c!lSS AsterOid extends Bl$e~ectorShlpe I


fldeflne the asterOid polygon shipe
prhlte Int[) Istx - ('lO, ,13. O.lO.22, 20, 12, 2, -10, -ZZ, -161;
prlute Int[) ISty - ( la, U,I7 .211.16, ,20, ,22, ,14, '11, lO, -5J;
fI", tltlon speed
protected double roUel;
,",bile doublegetRotatlon~eloefty()I return roUel; I
public .old setRotatlon~eloclty(doubley) (roUel - y;
flbo undlng rectangle
publ1c Rectan~leget8ounds() I
Rectlngle r;
r-MWRectingle((lnt)getiO' 20, lint) geUO 20. 40,(0);
retUNi r;

IIdeflul t constructor
AsteroldO I
setShlpe(new Polygonllstx. Isty. Ish. leng t h);
seUlhe(true);
SHRotat lon~e loc l tytO .0);

57

58

Chapter 3 Creating Your First Java Game

The Main Source Code File


The Ol3.in SOUf(e code file for this game is found in 3. file ",lied Asteroids.p..... I
3.01 pro,iding the eomp~e source code listing hen' so you C3n eumine it in
dcuil while re:tding mv el:pLanuioll$ of each ~hod:tlong the W3.y. I recommend you study the source code to get 3. good fed for how this S"mt works.

i..,or~ j . pplet.'
1..,ort j.v.wt.;
I~ort j.va . wt.event.,
laport j.va . wt.geoa.:
I.port j . wt . 1a.ge :
I.portj . utll. :

.
.....................................................
Prl .. ry chn for

t~e

gaat

public class Asteroids utends Applet

l~plMeflts ~urnable.

Ithe .. In ~hrUd be<:_$ the glat loop


Thrnd gawloop,
/use this a$. dcuble buffer
Buffer!'dl..ge backbuffer;
flthe ..1n drawIng object for the bact buffer
l>r.ptlfc$2Dg2d:
fltoggle for dr.wlng

bou~dlng

bOU$

boolean sho"Bounds false:


/fernte the astero1d arr.y
Int ASTEROlOS 20:
Anerold[) .st new ,l,$teroid[ASHROIDS1;

bullet arr.y
Int 8lJUHS 10;
Blll1et{] bul1e~. _ Bu'let(BUltCS):
1nt clirrent8ll11 et 0;
flcrute the

Keyt'Hener I

CrUl ing the G.""",


IIt~e pl.yer'S s~lp
5111p skip - nev 5blpO;

Ife r ntt the Id ent ity t ..nsfor. 10.0)


M f lneTr ans f Orll Ide" tl t y - newAfflneT r~ nHor.(} ;
Ilerntt a ran dOil nu~r generator
hndOil rand - newRand.:-():

Applet i nit(} Event


~

appkt tnttO f'Vm l i$ run wbm the appkt fi rst sian s up and is us.ed 10
initialize the game. The code first crcatda double buffeT. upon which all gr.lphias
will be rendered in order 10 produce a smOOlh screen refresh WilhoUl flicker. The
player's ship and the asleroids are iniliali'Cd, and then the key listener is st:lrtcd.

.....................................................,
apple t Inlt nent

public wold InitO I


/luntt ne bilek buffer for s_t~ grapllics
baekbuffer - nev hfferMINge(6'lO. 480. hffer~INge. TYPLUCRGB):
g2d - backbuffer .createG , a ph Ics( };
Il set up t he shi p
shlp . seU(310) :
slllp . setY<2 ( 0):

/lset up the bullets


for (Int n -0: n(lUlLHS: n++) I
bullet[n] - nev B"lletO;

li ereate th e asteroids
for (lnl n - 0; n(ASTE ROIOS; n.... ) I
an[n)- ne. ASlerold();
as t [n). setRoUt IonYeloci t,( ralld. nutlnt (J)+I ) :
an{n). setl{ (double )rand. nut Int (600 )"20) :
as t{n). set'l (double )r and. nut Int (UOj .. 2ilj :
an{ nI. setltcveAngl e( r and. nut Int(31iO) ) :
double ang- asl[n] .getllcveAngle(} 90:
as t[n} .se t VI!! I X( ea \(Ang I e llc~el{ ang) ) ;

59

60

Chapttr 3 Crt.ting Your First Java Gamt


ut[n] . nUtl He. IcAn91 dIowtY( Ing') ;

{lHut tht ultr Input Iisttner


Idd~tyLl$ttntr( tM $);

Applet updateO Event


1M appltt's Ilpdttt() e\'mt is triggered whenever the 5CrttIl needs to be rrlrrshed. This game does not call the updlttll method from the game loop yd,
although a future version of the game will make this change, which provides
better <:ontrol over the scrten refresh process. This met hod docs aU of the
drawi ng to the applel window by first drawing graphics to the back buffer. This
buffer is then copied to the applel window during the pil nt() event.

The identity tnnsform is ttl<: stllrting point of a vector-tMscd transform that


allows vector-based shapes to be rotated and IIlOVrd around in the game. First,
you start at the idmrityand then move the shape and rot.1t it from there. If you
don't start off with the identity bcforr manipulating a shape, then it will be
moved with the previous shape rather than on its own.
/

.
.....................................................
Ipplet updlle

t~tnt

to rtdr,w tht scr een

publiC wold up<!ltt(6rlph1cs g) I


lIHlrt oft transforws It Identity
gld. ntTr lnl fo...(1deflt I t,) ;
{ltrast the blckgrour>d
gld. lttP,I nt( Co Ior .8LAC() ;
gld . f111RecttO. 0, getS1ze().wldth. getShe() he1ght) ;
{lprlnt so-e Stltus 1nfo.... t10n
g2(l.lttColor(Color .l/llll[);
g2d.drawSttlng( 'Shlp: .......th.rour>dhhlp.getxO) .... . ..
.... tll.!un(ltllllp.'etH}l. S. 10);
t2d.clrawStrlngt ' Mawt Ingle: " .. Kath.round!
slllp.gtUlo.t....,gleO)+90. S. 25);
g2d dra.Strlngl "Flct Ingle:' ......th.roun(l(
slllp .gtHlctAngleOl. 5. aD};

Creating the Game

IIdr'll t~e g gr.phlcs


drawShlp( I;
drall6ulleh();
dr'llA.sterolds( l;

II rep.a ln t the .pplet 1l111llow


p.alntlgl;

Drawing the Player's Ship


Tht drall$hl p() mtthod is callffi by the updatel I tvent to draw tht player's ship
onlO Iht back bufftr altht COITtCI X and Y location. !kfore drawing, tht idtntily
trall$fonn i$ stt so thattht $hip's local coordinalt syuem i$ usN, rathtr than tht
prnious vtor'scoordinatts. Remember, a tnnsfonn afftcts an objt's X and Y
position. The idtntity is tilt staning point {O,O}.
When the ship is first dnwn, il is actuallyctntered at tht origin, with tht shape of
the sh ip being drawn from --6 to +6 in the X and Y axes, So tht ship is abou t
12 pixels square in size. If you don't draw a vector around the origin, then
rotation will nOI work al all, becau.soe rolatioJ1$ occur al the origin-or rather, at
the idmtity Ioation.

dr.IlShlp called by .pplet upd.te ewent

.....................................................

publ1cyolddruShlp(11
g2d. set Trans for.( Ident I tyl ;
g2d. tr.nshte(shlp.geUI I. shlp.getll I);
gld. roUtelllulI. tllRlidl ans (5111 p. 911!tfaceMgl e( ) II ;
g2d. setCa Ior(Col or .OIWlIiE 1;
g2d . fill (sill p. getSII.pt!:( II ;

Drawing the Bullets


Tht drawllulletsO method goes through the array of bullets and draws any
~ that nted to bt drawn. Thi$ only occurs if a bulkt is alivt using the
hA.llveO mtIhod. Then tht bulla is transfolllltd to iLS position on tlit Kt'ttfI,
and tht shape {which is a tiny rt'CIangk} i$ drawn.

61

62

Chapt"r 3 Cr"ating Your First Java Gam"

drawB" 11 ets ca II ed by app 1et upda te event

............................................1

public void

dr~1I6ul

lets() (

Illtera te th rough the ~rray of bull eh


for (Int n - 0 ; n < BULLETS ; n++l I
Ills thh bullet cu rrentl y In use?
If (bullet["].1s~live()11
Ildra w the bull et

g2d. se t Trans f or.( Ident i ty) ;


g2d. trans late(bullet{n],geU() , bullet[n]. getY(;
g2d. setCo 1or (Co lor, MAGENT ~) ;
g2d . drall( bu l let {n], getSh~pe( ) ) ;

Drawing the Asteroids


The dr allAsteroids() method draws all of the asteroids in the ast[] array.
depending on whether they are ali,c. When the playcr fin'S a bullet and it hits an
asteroid, that asteroid's alive variable is set to false, SO the asteroid is no longer
drawn to the s<:reen-and it is also ignored by the bullets after that. An interesting option in this method will draw the bounding r~"<:Ctangle around the
asteroids if you have toggkd bounding on by pressing the B key.

drall~sterolds called by applet update event

...................................._1

pub1 ic voId draw~steroids{} I


ffiterate through the asteroIds array
for (Int n - 0; n < ~STEROIOS ; n++l l
Ills this asteroId being used?
if (ast[nJ. ls~lhe()) {

Ildraw the asteroid


g2d .setlra ns fon( 1dent1ty);
g2d . trans1 ate(ast(n] .geU(). ast[n] . gety() ;

Creating the Game


92a.rOUte(lIitll. toR.!dlJnslnt[n) veUlonAnvle( II);
92d. setCo10r(CoIor . DAtlLGAAYl ;
V2d. flll (ut[n).vetSh.pe( I);

Screen Refresh
The p.intO lVent occurs when the applet window needs to be refreshM. This
mdhod is calkd by the: IIpditeO mdhod and simply serves the purpose of
drawing the back buffer to the: applet window.

I.... ~
.....................................................
Ipplet 1I1lldow ,elUlnt event -dr," tbe !lact buffer

public void pllnt(Gr.phlcs V) l


Iidraw the back buffer onto the applet Window
g.draw!lUge(blckbuffer. O. O. thts};

Thread Events and the Game l oop


1"hc'n-an: thrtt thread events that an' pan ofa program ..ilm you impkmmtthe
Rllnn.ble interface in a lava applet. Runnlble tdb Java that your apple!: will
support mon: than one thread. A thread issort ofa mini program that can run on
its own. You create a new thread in the stlrtl I lVent, and then destroy that
thread in the stop() event to keep things running smoothly.
The most interesting thread lVent is called run(). This event method contains the
code for the game loop, which is a while loop that son of powers the game and
keeps it running at a consistent fnme nte. This even t calls the V.....Updlle()
method, .... hich processes the current frame of tbe game by moving obje(u
around on the screm. testing for coUisiol1$, and so on.

I................
thrnd surt eYent . st.rt the VI. loop rllnn1ng

.....................................................,

pllbl Ie void stirt() l


fferute the gUleloop thre!d for
vneloop new Thread(thtsl;
VI.1 oop.start();

rUltl~

IIpdltes

63

64

(h"pter 3 Creilting Your First J"" Ga.mt

thru d run e.ent ( ga ll\' loop)

....1

pUblic wotd run() I


lIilcqulre tM current thread
Thread t - Thrud.currentThrud{ I:

l!keep goIng as long as the thrUd H .1he


wh11e (t -~ ga-eloop) I
try I
/{updilte the g 10<lp
gil.Update( 1;
lltil rget f....rate h SO fps
Thrud. $1 eep(ZOI:
I
catch ( 1nterruptedExcept t on e)
e.prlntStacHr.ce( 1,

I
~pillnt();

t....
thrUd $lap event

public .otd stop{) I


IIklll the gil.loop tnrud
gloop-null,

Game loop Update


The g Update{) m~ is alIed by the gilme loop thread when iI's lime to
proces.s tl\(o game for the Dat appkt window rdTc:sh. The game loop is timed to
hit around 50 frames per $Ond (fps), and the window refresh occurs "her
91.Update is run. Nomlally lhe game loop will run as fast as possible and only
lhe screen refresh will be til-d to a specific frame rale, bill in th is first game. thai
difference is nOI imponant.
{

(rnting the Gilme


prtute wold 9.upd.te() I
update5hlp(l;
updateBu II eh( I;
updateAs terOI dsl 1;
checkCol1 hlons( l;

Updating the Ship


The update$hl p() method updates the ship's X and Y position using the velocity
variables. This method also "W3rps" the- ship around when it crosses an edge of
the' $CffI (in which case lbe sha~ is moved 10 the opposite side of the scrrc'n).
This is a technique used in many classic art2dC' games.

I............
Update the ship position bllsed on weloctty
**
publ1c void updHeShlpO r
f/update Ship'S X position
Ship. inCX(Shlp.get~el X( l);

IIl1np irOul'Id left/rlght


If (shtp.~tI() < -10)

sht p. sell(9etSi le( ) .1I1dth + ID);


else If (shlp.\IetIO >getSlleO.llidth + 10)
shtp.sell( -10);
f/upd.te shlp's YposH Ion
ship . 1ncHshlp. gelVeIH)};
(Illrap around top/bottOll
If (shlp.\IetH) ('ID)

shl p. sen( getS Ilei J. hel lIht + 10);


else If (shlp.9etY() lIetSlzeO.helllht. ID)
shtp.setH-ID);

Updating the Bullets


The updateBull eh() method updates the X and Y position for each buJ1etthat is
currently alive USillg the velocity variables. When a bullet hits the edge of the
scrn, it is di5abled.

65

66

(hilple, 3

(,eilling You, First JilViI Gilme

I'

public voId upCHehl1ets( l I


lI.ave uch Of 1M bulleh
for tInt n - 0: n IUl US: ".....)
'lis th'$ bullet l>t11'l9 u~ed?
f (b"lletln).I~ ...lhelll
flupdUe bullet ' $ x 1'0$1 llon
bullet(n]. lncl( bulltt[n] .get Vtllll) :
II bullet dlsappeHs at lef t /right edge
If (bull et(n ].gttX() (0 II
bulltt[n] .geU() ) getShe() .wld t h)

lIupdate bullel's J po$ltlOfl


b"llet[n] . tncYlb"let{nJ.getYelYl I:
Ibullet d sappun at top/llolt.. tdge
fibullet:n).geU()<O I
t>ullet[n J.geU t) ) getS1ltt) .lIelg~t)
bJllet[n]. HtAl het hheJ:

Updating the Asteroids


The updateAs terol ds() mdhod upda les the X a lld Y position of each asteroid
lhat is currently alive based on the vel(>(it y variables. These X and Y values and
velQCities are all sel to rnndom values when the game slans up. The asteroid are
wuped around the edges of the screen. One inlcresting thing about Ihe ;lStcroids
that differs from the ship and bulJeu is tt.;.t the ;lSteroids are rotated b) d r.mdom

Creating the Game

number of degrees cach frame, causing them to spin 011 thc sen."cn. Th is is a prrtly
nice effe.::t that adds to the quality of the game.

f
.....................................................
Update the asteroids based on velocity

pub I i c vo; d upda te~. terol ds () {


fhl<Jve and rotate the asteroids
for (Int n - 0, n <~SHROIDS; 0++) 1
fli. this asteroid being used?
If (~st(n].is~1ive( (
f/upd~te the asteroid's X.alue
ast("]. I "cX( ast(n].getVel X();

I/ warp the asteroid at screen edges


if (ast(n].geU() < -20)
ast{n]. setHgetSi ze() .wldth + 20);
else if (ast[n].getX() >getSlze( l.wldtn + 20)
Ht[n]. seU( -20);
/Iupdate tne asteroid's Yvalue
Ht[n] . IncY(ast(n] .getVel Y());
Ifwarp the asteroid at SCreen edges
<-20)
ast[n].setY(getSize( 1.height + 20),
el se if (ast(o].geU() ) getSize( l.hei ght + 20)
asUn].setY( -20);
1f (ast[nl.geU()

Ilupdate the asteroid's roution


asU" 1. I ncMove~ngl e(a st [nl, getRota t i onVe loci ty( )1,
II keep the angle wHhln 0-359 deg r ees
if (ast[n].getMoveAngle() < Ol

ast{nl. setMoveAngl e( 360 - ~st(n] ,getRotati onVelocHy(;


else If (ast(n].getMoveAngle() ) 360)
as t [nl. selMoveAng Ie( as t [n], getRou t i onVe Ioc Ity( ) ) ;

67

68

Chapter]. Creating Your First Java Game

Testing for Collisions


We haven't discussed collision detection yet, but I think you will gctthe hangont
hcre broiuSt this chectColll slonsO mcthod is str:aightforward, First, there is a
loop that g<it$ through the asteroid array (Ist[ 1), Inside this loop. if an uteroid
is a1i'"e, it is tested for collisions ....i th any aeti~ bulle1$, then it is tested for a
collision ",ith the ship. If a collision occurs, then an aplosion sound efftct is
played, and the asteroid is disabkd. If the astaoid collided ",ith a bullet, the
bullet is also disa.bkd. When the playe-r's ship is hit, it is reset at the untt!" of the
scrttn with zero vdocity. A ooI.Iision occurs when one shape O\'erUps allOthc:T
shape, which is ...hy we use the IntersedsO and contalns() methods to
determine when a o;;ollision occurs. Sptcifinlly, cont.lns() is mc:d to sec whethc:T
the bullet has hit an asteroid, while Intersects() is used to see whethcr an
asteroid has hit the ship.
The key to the collision code is a method in the Shipe objo:d called C(lntalns()
that accepts a Rectangle or a Point and mums truc if there is an ovcrlap. This
method makes it possible to perfonn bounding rtdanglecollision dettction with
just a few lines of o;;ode ba:ause the shapes already havc built-in get8(lunds()
methods available.

...................

rut uter(llds f(lr C(lllislons with shlp(lr

b~llets

,
public voId checkCol1islons() {

IIlter.te through the asteroids arrlY


for (Int _0; _<ASTEROIDS; ..... 1 I
IIh this asteroid being used?
(nt[_}.lsAlheO) (

if

(he(k f(lr (llll$lon with bullet

./

for (Int n - 0, n < BUllETS: "....) t


III s till s bullet \leI nq used?
if (bullet{nj,l$Alht()) I
llperf(l'" tile colli st on ttst
I f (IS t(_J ,9Ct8tNndS( I. contal ns(

C'eating the Game


bulleHnl getXCl. bullet[n] get1()))
bullet[nl. setAl lYeCfal s~):
Ht[1II1 .setA) lYe( fal se),
co"tlnue:

check for coll i s Ion with shIp

./
1f (a st [_l. getSounds ( ). intersects Cs hi p. getSou"ds ( ) )) 1

aHC_l. setAllveUal se}:


sh1 p. seU (20) :
sh1 p. set1(240):
shl p.setFaceAngle(O);
sh1 p.setVel X{O):
shlp . setVelY{O):
continue,

Keyboard Events
This game uses the keyPressed() evem to detect key presses. while keyReleased{)
and keyTyped() are ignored (although they must be in the source code listing
because of the KeyL 1stener interface). The most important parts of this method
are found in the code following the thrust and fire keys. which arc mappe-<! to
the up arrow and CIt! keys. (The Enter key and spacebar can also be used
to fire.) When the up arrow is pressed, this adds thrust to the ship, causing il to
move.
Definition
An algorithm is a mathematkal expression that causes one of the vanat>1'S ;n the expression to
dlange in a consistent w/tf. Amovement algorithm causes. for instance. the. vanat>e 00 a" x - ~
<;<,>ooj;nate plane to change SO mat it consistently ;ncreases in value, rnc.wing whatever object it
represents horizontally across the saeen.

69

70

Chapter 3 Creating Your First Java Game


An advanced movement algorithm is used to move the objects in the game,
which is covered in the n<:xt seclion. Movin g the ship muSI look as realistic as
possible- so you can apply thrust to the ship, rotale to a new dire<:lion. then
apply thrust, and Ihat new angle or movement is added to the current velocity
va lues. The result is a "ery realistic zero-gravity motion for the ship. Some
progralllmers like to use a mass/acceleration algorithm to mow a space ~hip.
That is a good method, where the mass (or weight) of the ship affects how fast it
can mOIe.1 have simulated this effect using a velocity algorithm illste:ld, which.
again, is covered in the n<:xl sec tion.

.....................................................
key lIstener e ven t s

public voId l:eyReleased( KeyEvent k)!


pub l ic voId l:eyTyped( KeyEvent k) { I
publ ic voId keyPresse d( KeyEve nt k) I
int keyCode - k. getKeyCode() :

case KeyEven LH....lHT:


//left arrow rot ~ tes shIp left 5 degrees
s hip . jneF~ceAngl e ( 5) :
If (ship . getFaceA ngle() <0) shl p.setfaceAngle(360 5) ;
break:

II

case KeyEvent . VICR1GHT :


r Ig ht ~ rrOlr rotates ship right 5 degrees
s hi p. ineFaceA ngle(5) :
If (ship.getFaceAngle() >360) ShIp setFoceAngle(S) ;
brea k;
case KeyE vent .VK_UP ;
Ilup arr Olr odds thr ust to sh Ip (1110 nOfll<l l s pe ed)
sh Ip. setlloyeAngl e (shl p. getF~ceAngl e( ) gO) ;
ship . ine Vel X(c~l cAng l elloveX( shtp . getllo veAng l e() J 0 .1 ) ;
ship . inevelY(c~l cAnglelloveV(ship . get lloveAngl e ()} 0 .1 ) ;

break ;
Sp~ce con be used t o fj re
KeyE yent . VICCOHTROL ;
KeyEvent. VICENHR ;

I /Ur I . Enter . or
c~se
c~se

lre~po"

Creating the Game


clSe KeyErent. VI(..SPAn:
IIflre i bullet
cllrrent811)) et....:
If (cllrrent81l11et ) BUtLETS I) currenlhllet - 0;
bill Iet(cllrrentBllll et l. seU) i ~e( Irlle l ;
IIpolntbullet In sa. dIrection ship is facIng
bu II et[ curren t Bu11 et l. seH(ship. getU ) );
bullet[currentBullell. sHY( ship. getY( l);
bu II etC currentBu II el l. sellloveAngl e( sh Ip. geH aceAng1e (l . 90 l ;
IIf1re bullet al IMle of the ship
double angle - bullet(currentBulletl.geUioreAngle( l:
double su - sh1p.getYe1J1 I;
double sy - shlp.geltelYl I:
bllllet(eurrentBulletl. stUelI(su" eil(:An9lello~eI(an9Ie) 21:
bulletlcurrentBulletl. seUe1Y(svy" eilcAn9lello~eY(angle) 21;
break:

Calculating Realistic Motion


The most fasdn:lting part of this game is how the mo\'ement of the pbyn'$ ship.
the bullm., and the asteroids a~ all coni rolled by two methods thaI mum
lloa.ling-point values for the X and Y update for the object.
O.flnition

The ea l eAngleMo~eX() method uses cosine 10 cakulate the update value for X,
returned as a double. The clleAngle"o~eY(l melhod uses sine 10 calculate the
update value for Y. also returned as a double. These small methods accept a single
pal1lmder (the angle IMt a game object is facing) and rdum an cstimalN X and
Yupdate value in pixels based on tlut angle. I can't S1rcss enough how wonderful
these two methods are! In the past, I M\"C miN mainly on the brute force (and
imprecise) method to move galm objects (usually called sprires) o n the SCrfl. I
would set the elocltyl to I and elocltyY to 0 to cause an object to mo\'e 10 the
right. Or, I would SCI ~elocltylloO and ~cloeltyY 10 -ilocause the game obje<:t
to mo\'c up on the screen. These veloci ty variables, along with an object's X and Y
values, would cause the objeclto move around on the S(rl'Cll in a certain way.

71

72

Chapter 3 Creating Your First Java Game


I have writun many games that llSol'd this type' of movement code. Invariably,
these: games include: a lot of switch statements to account for each of the
directions that an object might ~ being. For i/Utance. if a space-ship sprite has
right direo:tio/U of travel. then J would ....rite a switch statement thitt considered
the case for each direo:tion (0 to 7. when 0 is rxmh and 4 is south), and then
update the X and Y values based on the ship's direo:tion.
No longer! These: wonderful methods now akulate the vdocity for X and Y
based on an object's orientation as an angle (from 0 to 360). Not only does this
result in a more realistic game, but the source code isaetuallycleaner and shorter!
As far as realism goes, this code supports every angle from 0 to 359 (where a circle
is composed of36O degrees). You can point the spaceship in this game al an angle
of I, then fire a weapon, and thai bullel williravel JUSt slightly offfrom due north.
The biggest difference between this new method of sprite movement from my
previous game is that I previously used integers, but now I am using floatingpoint variables (doubles).lbis allows the velocltyX and velocltyY V3riables to
refled anyofthe 360 degrees of movement. For an angle of 45 degrees, velocltyX
is Sd. to 1 pixel, while vetoel tyY isSd. 10 O. The cardinal dirtions (north, south,
easl, and west) are similarly predictable. But when dealing wilh an angle such as
17 degrees, the velocity variables will be Sd. to some very unuswJ numbers.. For
instance. welocltyX might be 5d to something like 0.01, while nloct tyY is 5d to
something like 1.57. These: num~n don't equate to actual pixcl-Jeo.'eI Il')()\'('ments on the scum in a single frame. but ....hen you consider that the game is
running at SO fps or more, then these values add up, and the ship or other game
object is moved O\'er time in the correct diTedion. Since the vtor transform
method eJlpts floating-point values for X and Y, these velocity values work just
line with the part of the program thai draws things on the screen. It is fascinating
to watch, and we will be using this te<::hnique throughout the book.
Now, without further ado, here are the velocity calculation methods in aillheir
simplistic glory:

I........ ......
~

calculate X-.:Iv_nt value based on dIrection anvle

....1

public double ulcAnVlellovdldouble anvleJ r


return (double) lllath.coslall91e 'Ilath,PI/I80IJ:

Review QUf!Slions

...................................................../
p~bltc

double CllcAnglelloveT(double Ingle) I


(double) (llilth. sin(lngle IIiIth. PI 1 ISO)) ;

~turn

What You Have learned


This chapter threw a lot of new concepts your way withoul fully explaining all of
them, bUI with the goal of giving you an opporlunity to euminc a nearly
romplcte game and Stt bow it was craled from Sian to finW!. This Asttrouustytt gaITl(' will bt ('I\hanccd in substqutnt chapttrS into an exciting arcade-Slyle
gamt with a scrolling background. Specifically, you learned:
How to usc: the Graphics20 class
How to usc: a thread as a game loop
How to draw vector graphics to rnakt gamt objects
How to move an objca based on iu velocity
How 10 tt$! for collisions bct",-cm gaITl(' objKts

Review Questions
The foJlowing qU~'StiOllS will help you to determille how well you have learned
the subjects discussed in lhis chapter. The answers arc provided in Appendix A,
"Chapter Quiz Answers."
I. What is tht

n.3IT1('

of the mc-lhad Ihat e.tlcubles the velocilY for X?

2. Wbat is the base class from which Ship, Asteroid, and Bullet are inherited?
3. Which dassic Alan gam.. inspired th.. gaITl(' df'VC'iopcd in this chapttr?
4. \\!him typt of collision testing does this samt ust?
5. Which method of th.. Shipe class does this game uSC for collision tesling?

73

74

Chapter 3 Creating Your First Java Game


6. Which geometric shape class do the Ship and Asteroid classes use?
7. Which geometric shape class does the Bull et class use?
8. Which applel event actually draws the screen?
9. What is the name of the interface class used to add threading support to the
game?
10. What math function does calcAngl eMoveX use to calculate the X velocity?

On Your Own
Although this game will be enhanced in future chapters, you will learn a lot by
making changes to the source code to add some of your own ideas to the game
right now. Use the following exercises to test your grasp of the material covered
in this chapter.

Exercise 1
If you apply a lot of thrust to the ship so that it is moving very quickly across the
screen, and then rotate around backward and fire a bullet, that bullet will seem to
stand still or move very slowly. This is be<ause the bullet is based on the ship's
velocity. This isn't very realistic. Modify the weapon firing code in the keyPressed
event method to fire bullets at a fixed rate regardless of the ship's velocity.

Exercise 2
The ship tends to rotate rather slowly when you press the left or right arrow keys,
making it difficult to hit asteroids that are closing in on the ship from all
directions. The rotation angle is adjusted by 5 degrees each time the keys are
pressed. Modify the game so that the ship rotates much more quickly without
changing this 5-degree value. In other words, you want it to rotate by the same
value, but do these rotations more quickly.

PART"

..JAVA GAME PROGRAMMING

This5ond pnt oftht book will cover the important lopia you need (0 know in
order to write an appln-based game in Java. including graphiC$, sound. music.
Uybo.:Ird and ffiOUSl' input, timing. and so 011. Hen: ne the chaplCTS in Pan II:
Clapler 4: Vector-Based Gnphics
Chapter 5: Bilmap-Rased Graphics
Chapter 6: Simpk Sprites
Chapler 7: Sprite Animalion
Chapler 8: Keyboard and MouS/: Input
Chapler 9: Sound Effects and Music
Chapter 10; Timing alld the Game Loop

CHAPTER

VECTOR-BASED GRAPHICS

The previous chapter really pushed the limits as far as the amount of information
cover! without thorough explanations beforehand. I wanted to immerse you
in the source code for a game right up front before fully explaining all of the
concepts to give you a feci for what is involved in cTt'llting a real game. The
Asleroidsdone was not a greal game, and nOI ewn vel)' good-looking, but it was
functional 'ava has a robUSI and feature-rich set of classes for working with 2D
V1or graphia and bitmaps (cxplaiDed in the naI cmplcr), making il pos.sible
to draw rectangles, polygons, and olhcr shapt:S very easily. Here are the key topia
in this chaplcr:
Drawing and manipulating vector graphics
Using the AfftneTnnsforll dus
Applying the lranslation, rotation, and scaling of shapt:S

Programming Vector Graphics


You have already been exposed to a significant number of features in Grapht cs20
and other classes in jn.. ~wt (the Abstract Window Toolkit), such as RecU"gle
and Pol,go". Thc core of Java's 20 graphics engine is the Grlphl cs20 class. This
class is incredibly vCTS3tile for working with I'ector graphics and bi tmappl
graphics. For il1$tana, Graphlcs2D has 10 methods for drawing images in a
variety of ways! In my opinion, this is somewhat of an o\'crkill just to draw

78

Chapter 4 VtorBased Graphics

ilrulgcs on 1M screen. But Java is wdl known for its ven;,atillty and wnvmitncc.
This class knows how to draw m:tangla and many othtr shapts. But it can do a
lot mort than just draw-it can abo 1l'lOVt. roUtt, and scalt shapes!

Working with Shapes


Ltt's writc a shon program 10 dcmonstrate. The RandomShapes program is
shown in Figure 4.1, and the sourct code listing follows this paragraph. r have
highlighted all of the imponant lines of code in bold text, and you'll learn about
the classes, properties, and methods that have been highlighted.

..

......................
tllpOrt jua ....t.;
tllpOrt jua . ppl~t.;
tllpOrt jua.a>lt.~.;
IlIpOrt jua.llttl. ;

public class

Rande-S~.pes utends Appl~t

lI~er~'s t~~ s~ape

private

used for drawIng

S~.pe lh.p~;

fi9ure 4.1
The

Jlf09rJfII ilusums Iht 'NplIlcslD ~

Progr,mrrung Vector Gr,phics


fllpplet hit e>ent
pUblit >old init() l
shipe-new Rectlngle20.00uble(1 O. j .O. 1.0,1.0):
fllpplet paint e>tl\t
p"blit >oid palnt(Grlphles gl I
Ifereate In instlnee of Grlphits20
&rlphles20 g2d - (&rlphle s20Ig:
lisave the Identity tr~n$for.
Aff1neTrinsfol'llldent1t1 - new Affindrlnsfol'll();
Ilcreite I rlndo- n~r gen.eutor
Rlnda. rind - nl!W R.l1\dooI1 I:
IIsue the window wldthlheight
lnt width ge tS lu( l. wl dth:
i nt hel ght getS he ( ) . hei ght :
IfUll the background with blld
,2d.utColor(Color.Iucrl:
,2d.f111It1ctlO. O. wI4th. lltl,htl:
for(lntn-O:n<300:n++)1
IlruU Grlphlcs20 t o the identity trlnsfor.
t2d. SllTrlnsfol'll( ld ent l t1 1:
IIWI_e. rotlte. Ind sule the shipe rindoaly
t2d. trlnshte ( rln<l.llf:lltlntl) S w1dth. rln<l.nntlntll S he1ghtl:
g2d. rotitellllth. toR.ldiusl360 rind .nextOovble( ll):
924. sule(60 rand .nextOouble(), 60 rand .nutOoubl eO) :
Ildrlw the shipe with I randoa c<llor
92d ..,tColor(,," Color(ran<l.nntlnt ( Ill:
g2d. fill (s hipe ):

This program uSfil Ihe Grlphics20 class to translate. rOlate, and scale a Shape
object randomly. which retulu in the screen being filled with random rectangles
of v;lrying sizes and orientations. This simple program illustrates lhe basoe roncept bdtind lhe Asraoids-sty!e game from lhe previous chapter-dlat Java

79

80

Chapter 4 Vecto r- Based Grilphks


provides the toolset for manipulating 2D graphics, and it's up to you how you
will use these versatile tools.
The R.lndomShapcs program defincs a Shape object (called shape) and then uses
that basic obje<:t to create a Rectangle2D like so:
shape - nell Rechngl e2D. Double( -I .0. -!.D. 1.0. 1.01;
This works, even though the shape objox:t was originally created as a Shape be<ause
Rectangl e2D is derived from the Sh~pe class. In other words, Rect angle20 inherits
from Shape. This makes it poss ible to use the Graphics20 method flll to draw a
filled redangle, even though it was defined originally as a basic Shape. For each
class, such as Rectangle, there is a noat ing-point version, such as Rechngle20.
Classes such as Rectangle utilize imeger values, wh ~ c Rectangle20 uses noots and
doubles. You can also use the Point and Polygon classes in similar fashion.

Working with Polygons


The Polygon class is a bit different than Paint and Rectangl e be<ause it allows you
to define the shape your:;elf using X and Y value pa irs. You can construct a
polygon with just a single point or a polygon wi th four points to duplicale the
Point and Rectangle classes yourself. Or you can define custom polygons, such as
the asteroids and ship in the previous chapter. The asteroid shape (shown in
Figure 4.2) was defined like th is:
private int(]astx - I-20 . -13. 0.20.22. 20. 12. 2.-10,-22,161 ,
private lnt[ 1 asty - I 20. 23.17 . 20.16. -20. -22, -14, -l7, -20, -51:

Figure 4.2
The aste<oid sl'iapc,

Programming Vector Graphics

...
e.

figure 4.3

n. ~ Jdl9ln wi tit lllOClflrd in 1M ltailclc'h~lS

pn:l!JiI!Il.

two arrays define the X and Y points for the polygon. We caD a point a
and the plural form is I'I'rtices. When you are creating a polygon in this
manner, keep in mind that the X and Y arrays must pair up, since every X must
go with a Y v'llne (0 make a vertex.
YCTtl'x,

When you're ready to draw a shape, whether it is a rectangle, a polygon, or


something eJs,e, you have two choices. You can usc the Ill} () method to draw the
shape with a filled-in 00101". Or you can usc tnc dr,.() method 10 draw !he outline
or bonier of the shape in the CUTTmt color. ~ color is set with the setColortl
method beforehand. Sometimes il can be confusing ...t1cn)'Ou are Irying 10 define
the shape of a polygon using tbe two arrays of X and Y point$, so you may want to
design the polygon on paper or in a graphics C'ditor first. Figure 4.3 shows the
design of a five-sided star-shaped polygon.
Seeillg a diagr.lm of the image can really help, especially when yOll have a
complex polygon in the works. Here arc the arrays for defining this polygon.
Note how the poinu directly cOlTC$pond to the values in the figure.

prt.,telnt[]xpolnh-l 0.10.-1. 1.101:


prhate intO ypolnh 110. t. 10. 10. ,2 I:
Let's write a program to demonslrale how 10 create and draw polygons. The
IbndomPolygoru program will usc the five-sided slar polygon with random
rolation and scaling. The output of the progrnm is shown in Figure 4.4.

..........................................................
Ran~olllPolY90ns progra ..

81

S2

Chapter 4 VKlorBaSt'd Graphics

lllp(lrt
ll1pOrt
111port
1.port

jau.a"t.:
jna.applet .
java .utl 1. ':
j ava.awt. geOll. :

pub11C c]on hndo_Polygons

e~tends

Applet (
O. -10. 7. 7. 10):
prlute lot[] )'polots (-10. -Z. 10. 10. -21:

prlvatelot[]~po1ots1

lI~ere's t~e s~ape used for d.."log


prlvate Polygoo pol)':

lI appln lott ueot


p"bllc void lottO I
pol)''' netl Polygon(xpolntli. )'polnts.

xpolnts.lengt~):

lIapplet paInt nen t


p~bllc void palnUGraphlcs g) I
IIcrute an 1nstance of Graphlcs20
Grlphlcs20 g2d (Grlphlcs20) g:
llsove the Ideotit)' Iransfo..
Afflndrlnsfo.. Identlt)' - nelf AfflneTrlOsfo..( I:

Programming Vector Graphics

IIcrute , r,ndOll nu-btr gener,tor


IIndllII r,nd - n~ IIndOll():
Ilsue the "'ndOll "Idth/helght
lilt " Id th - getSla( I. " Idth:
lilt kl ght - getSlze( ). height;
1/ fl II tk backgraund " I th blick
gld. 5~tColaf (Ca1ar . IUCI) :
gld.f111hctfO. O. wldth. heigH):

farlilltn-O: ,,<300: n++) r


II reset Graphl cs20 to the i dent Ity trlns far.
g2d. sHTran$far.( Identl ty):
IlltOve . ratate. and scale the shape randa.ly
g2d. translate ( rand. nutlnt() I "I dth. rand. outlnt{) I height) :
92d.rata te(Mlth . taR, dllRs(360 * rlnd.nutoa~blel)) I;
92d. SCll el 5 * rand. nutOaub1e{). 5 * rand. nutOaublee :

IIdr ." the shape " I th I randOll cal ar


gld.setCala r(_ calarlfllld.nutlnt( );
9ld.filHpalYI;

Rotating and Scaling Shapes


The preceding programs have used vedor rotation to rotate rtangk$ and
polygons by a random value. Now I want 10 give you a little more direct exposure
to Ihis feature by writing a program thai rolates a single polygon on the screen
using Ihe arrow keys and, alternately, the mouse buttons. The scale factor is set to
a fixed value of 20, which you can change if you want. Figure 4.5 shows the
output of Ihe RotatePolygon program.
There arc a couple of notable differences betwccn this program and the last one.
This program jU$1 draws a single shape, so Ihcrc is no need to SCI Inc identity
tr.msfonn before drawing. This program implcmcnl$ tnc ley LI stener and House'
ll$tener inlerfaces, which mans thai lhc program must U5C all of the methods
dcfiiW in these inlerfau dasoses, even if you don'l pbn 10 U5C them. II's an odd
quirt thai is inhcrmt to how intcrfKc cb:sscs worlc. bcaU5C they an abstract..

8l

84

Ch<!pttr 4 Vector-8ased Graphics

Figu,. 4.5
TIlt P.olaIef'dItgon prow... _

,) Wr-Wpod

.
..........................................................,

RoUUPolJgon progra

l-slort
I.port
l-slcrt
illpOrt
ll1pOrt

java .awt .;
java,awt.event.
Jav a.a pplet.;
J av,) . ut 11 . ' ;
JaW,). , .. t. geOll, ;

pubHc chn Rot,)uPolygon utt1lds Applet ll1plmenu leyllsteMr. Mo"uLlsteMr

I
prlutelnt[]xpolnU-{ 0,-10, -1, 1,101;
prlute lnt(] ypolnts - (-10, ,2, 10, 10. -21:
Ilhe r e 's the snape used for dralling
prlute Polnon poly;

lipolygon rClt,tlon W,)rhble


lntroUtlO1l-0;
Ilapple t inlt event
public void lnltl) I
Ilcrute tne polygon

Progr(lmming Vector Gr.phks

Ill nlth l la th e l lHene rs


Iddltrlhunlr{thh l :
Iddllolltell st_r(tll1 s ):

/lt pp le t pt ln t eve nt
pu~1ic ~ol d pt l nUGr aph1cs g) I
/lcreUe an ins ta nce of Gnpll1cs2D
Grapll1cs2D gld. ( &rtpll1cslD ) 'I:
/lsue th e Iden t ity trans fo r.
AfflneTransfor1l Identity new AfflneTransfor.():

IIsue the window wldth/lleight


Int width getSlze( ).wldth:
Int height ge t Slze( 1.height;
Ilfi 11 t he bac kg round with bla ck
g2d.setColor(Color.BLACK);
g2d..tlllRecUO. O. width. hel'lht);

/IrIOn . rotlte . l lId selle the shape rlnde-ly


gZd.transhu(wldth I Z. height 12);
gN.sclle(20.20);
g2d. rohU(Nlth. tobdl Ins ( roht1 Oft) 1;

IIdr .. the sh.pe wi th. r.ndo- color


gZd.ntColor (Color .RED):
g2d. til Hpoly):
g2d.ntColor(Color.ILUE1;
g2d.drlw (poly l ;
II hand le keyboard events
publ Ic voi d keyReleas ed (KeyE vcn t kl (
public void keyType<:!tlcyEwent k) I )
public void hyPressMlKcyEwent k) I
switch n.'lctteyCod.eOl I
cne KcyEvcnt.U...LErT :
rotation--;
If (rotltion <0) rotltlon - 359:

8S

rep.l ntO;
bre.k;
(lse le,E_ent. n ... RI6HT:
rOtitlolt++;
If ( rotitlOf1 ) 360) rot.tlon" O.
rep.lntO;
break;

Ilhandle lIOuse ewents


public ~o1d lIOuseEntered(Mouse E~ent.) l I
public ~old lIOuseE~lted(llouseE_ent.) t I
public ~old _seRt)eased(lIouseE_ent.) I I
public ~old WlusetllckedtllouseE_ent.) t I
public void WlusePressed(HouseEvent.) (
swl tch( . getButton()) I
cue lIousehent. BUTTONl:
rotltlO11:
If (rotltlO11 <0) rotatlOf1" 359;
repalnt O;
bruk;
cue HounE.ent.BUTTON3:
rotitloP++:
If (f1ltatlO1'1) 360) rotation" 0:
relllllnt():
break;

What You Have learned


This c~pter provided a bridge from the materia.! in which you wae immtrsed in
the previous chapler 10 the new oonccp15 you will learn in the next chapter,
covering the basics of vector-graphics programming. The next step in graphics is
to draw bitmaps, and then regular sprites, followed by animated sprites. We have
much 10 learn in upcoming c~plers! Here is what we covered in this chapter.
How to use Ihe 6raphlcsZD class 10 manipulate vector graphics
How to transla te, rota te, and scale veclor shapes

On Your Own

Review Questions
The following questions will hdp you to determinr how ..'til you havc lcamnl
lhr subic'cts discussed ilt this chapler. n.c answers are provided in Apprndix A,
"Chapter QUil. Answers."
I. What is the primary class we'vc been using '0 manipulate vector gl"J phics
in this chapter~
2. Wha, is lhe name of the Appkt (''I'cnl Ibat rd"rrshcs 1M scrm?
J. W1I<1I is the name of the Gr3phialD mclhod lltat dm..s a filled
rungld

4. Define the words comprising the auonyrn A'NT.


5. What class makes it possible to perform transb,ion, rotation, and scaling
of shapes!
6. Which 6raplllcslO mclhod dr;lW5 a polygon?

7. \V1lich tr;lnsform mclhod moves a shaJ)(' to a nrw location!


8. What method initializes the kryboard listenC1 inlerface!
9. What method in the
value!

R~ndo.

class returns a double-precision floating-point

10. \V1lich KcyLhtcner evcnt detects kc-y presses?

On Your Own
U$C' thr following exercises to tesl your grasp of the malerial covered ilt 'his
chapter.

Exercise 1
ThC1C arc nu.ny cnmple progranl$ in Ibis chapter lhat could be modified and
aperimmted upon. Ld's tweak the RandornPolygons progr.lm-modify Inc
program 50 thaI it dr;lWll IWO different polygons instrad of jusl a singk onr.

87

88

Chapler 4

Vector-Based Graphics

Exercise 2
Modify the ROlalePol)'gon program .'iO Ihal it "'ill rotale based on mou5C.'
mo"enwnt instead of llUlIon clicks. You will rwed to implement the House
IIottonl1stener interface (and events) and call lhe addHouseHotlonltstener
method 10 gain access 10 lhe .ausel'\ll.ed evenl. In Ihise'enl, )'ou can lrad: mouse:
nto,ernenl and rolate lhe polygon accordingly.

CHAPTER

BITMAP-BASED GRAPHICS

Jan. has a robu5l aDd fature-rich 5d of ~ for working with 2D bitmap~ gnphics (also known u mstrr grtlphics), allowing you to load and draw
bitmaps vayeasily. Bitmaps are the k<ys to buildinga good 2D game with images
rather than vector shapes. Here are the key topics in Ihis chapler.
Loading and drawing bitmap images
Applying transfonnations to bitmap images
Drawing opaque and transparent images

Programming Bitmapped Graphics


I mentioned before thaI there are 10 methods for drawing bitmap images in Java.
Actually, six of tho$e methods are found in the but Graphics dass, while the
remailling four are found in Graphics2D.1 think you will lind the four Graplltcs2D
rmthods more useful, 50 WI' WOIl't spclld any time working with the legacy
\ersions.
The moSI amating Ihing about the Graphtcs2D dass is how its methods for
manipulating 2D graphics work equally well wi th vectors <Hut bitmaps. This
means you will be able to translate, rotale, and scale bitmap images just as easily

.,

90

Chapter S Bitmap-Base<! Grapnics

as you ha\~ manipuLltM \~or gl'ilphu thus far. This awaome functionality will
tnlllsble well into tM subsequent chapters on sprite and animation prognomming.
The real difference when working with images il; that you will ntoed to create a
separate AfnneTr~"~fo ... objcctto manipubte the INge objt, rather than going
directly through Graphlcs20.

loading and Drawing Images


You can ~ me get I..ge( I method to load a bitmap Ilk stored in manydiffcrmt
fOrm.llt$, with the most common being PNG (Ponabk Network Graphics) and
BMP (Windows Bitmap). This method il; found in the main App l et dass and can
be usffllO load a bitmap file for usc as anwork in your game. The method for
drawing a bitmap is similarly straightforward: It is called drawl. agel l.
Let's write a program that demonstrates how to load and draw a bitmap image.
We can usc the getl . age O method to load an image file, and then usc draw
lllolge( ) to draw it onlo the applet window. FiguR" S.l shows the OUlput from the
Drawlmagc progr<1m. I have highlighted the important lines of code.

Figure 5.1
The DrlWlmage progo-am loads a bitmap file and drirW$ it.

Programming Bitmapped Graphic;

This ~ily castle image was rendtfed by Reiner "'olein using Caligari trueSiCe. He oIlm
a large amount of roya1ly.fref game artwor'<.. such as 1M ust\e, at tos website. www,rffierslileetde
~

Gelman site wiIh an English ~.

.
,'

'

"

'

11lport JUI awt.':


I~o rt Java appIU. " :
I.rt Jan lift. ' :
public chss DrawllUge utends Applet I
!lluge nr1able
prlnte luge luge:
prlnt! Dill getUlll(Strlng fl1enl. ) I
Ullu rl-null:
try (
uri - tht l.getClus() getResourcelfl lenil.):

I
catch (Exception e) (l
return uri:
lIi1pplet Intt !Went
public .old Init() !
I.ilge - gUIr.age(getuRl("ciistle

png~)}:

lIi1pplet pllnt !Wfflt


public .ald palnt(lirlphlcs g) I
IIc reate In lnsunce af &raphtcslD
GraphtCllD gld _ (GrilphlcllD) g:
111111 the bilctground with bhct
g2d. utto Ior( Color .al.ACll:
g2d. fl IUKU D. D. getSlle(). wi dth. getS Ile( ) .lItl gM) :
IIdrn the luge
g2d.drawluge(1.lge. D. D. this):

91

92

Chapter S

Bitma~6ased

Graphics

Figulll S.2
The RaodomlmigfS program driJWS imigfS at laOOom Iotatioos. with random rotation and lUling.

Applying Transforms to Images


Now I'U demonstrate how to apply a transform to a simple bitmap image.
Remcnl:lC'r, a transform is a pnxesli that manipulates the position, rotation.. or
scde f.actor of:l shape or image. This will makt- our sprite code in the upcoming
ch:apttn rnlly fun bKause the sprite images will be manipulated with thtst
transforms :as well Sioce this code is similar to the (Ode for manipulating vtctors.
it should look at least somewhat familiar, ~n if you don't fully understand it.
One differenee when working with an image is that yo u must d efine a separate
Affi neTr,nsform object for manipulating the IlIIage object bausc the Gr,phl cslO
transforms are designed to work only with vectors. Figure 5.2 shows the output
of the Randomlmages program, showing a space-shi p image l:IC'ing moved,
rotated, and sc.aJed.

,
.
......................................................f
I . r t J,v, .,lIt.;

IlIp(Irt J'u, 'liP1et. ;


1_1I0rt
.utll.;
I_port J,.,.,wt.geo- .
i_port J,v,.net

j,.,

Programming Bn.n.pp('d Gr.phics


pub11 c c 1a ss RlndOlllllages extends Appl et I
1I1.lge variable
prlnte luge Illage:
IIl dent1t7 trlns fOl'Sltl on
Affllll!Trusfora Iclentlt7 new AfflneTransfora( 1:
prhlte URL;etU RL(S t rlng flle nl M) (
UR L uri null ;
try I
uri Uls .gnClln( l.'ietResource(fllenIMl :

I
cnd lhceptlon e) ( I
return url ;
Iflpplet lnlt nent
public wold ln1tll (
Illage - oetillage (,nURl( -spa cnh1 p. pn,- l l:
IIlppl et pllnt u enl
pUbl1c wold pllnUGrlphlcs gl (
IIcrtite In Instince of Grapll1 cs2D
Graplll cs2D , 2d - (Graph lcs2D ) , :
llworkln, transfol'll object
Af11 neTrlns10r. trlns new Affl nelrlnlfor.( ) :
/I rlndOll nullber generltor
Rlndoe rand ~ new IlIndOll( I;

II IPIll et vi ndow wldth / hef gilt


lnt width - ;etSlze( ) .wldth;
lnt height getSlzel) ,height:
llfill tile blIckgNlund wi th bllck
g2d.selColorlColor.SLAU):
'i2d.flllRect{O, O. getSlzell.wldth. getSlze/l height);

/ldrlw the ililge lIu1tlp1e tlMS


fo r (lntn-0:n<50 ; n.... )(
trans. setTrusfol'll l l dent l t7 ) :
llaowe. rotate. sClle the hMge rudOlll7

93

94

Chapter 5 Bitmap-Based Graphics


tra ns. tr anslate ( ra nd .next Int ( )Swl dth . rand . ne~t Int{ )Shel ght) ;
tra ns. rotate( Math .to Radl ans (360 rand .nextOo ub1e( ) ;
dou ble seaIe ra nd .nextDo ub1e( )+1 ;
trans.scale(sce1e . scale);
IIdraw the ll1J.age
112d . drawlla ge(llalle. trans . this);

Transparency
Although you can load and draw a bitmap at this point, lhe code you've seen so
far is very limited. For one thing, the get Imilge(} method can't load a bitmap file
out of a Java Archive (JAR) file. JAR files will become very important later, in
Part Ill, when we build the Galactic War game. Since the game is so large, with so
many bitmap and sound files. it takes a Jong time for the game to load over the
web (unless you have a broadband connection). You'll learn how to create and
use a JAR file soon enough. All I'm concerned abou t right now is that we are
using code that will be compatible with a JAR, so that Java can read files out of the
JAR as easily as it reads the raw files from the web server (or the directory in
which your program is located if you are running it locally).
The Abstract Window Toolkit, known as AWT, provides a class caBed Toolkit
that knows how to load a bitmap file . It's smart enough to look in the current
URL path where the applet is located (something that you must pass to the
lleti llage{) method) . You can usc loo l~\t in your own programs or you can
instantiate a global 10011:.1t object and then Uf>C itthtoughoul Ihe game; there are
many options. Let's take a look at how this class works;
Toolkit tk - 100lkit .1letDefaultToolk lt() ;
I.~ge

shl p - tk. getl lllage( sur_destroyer. png'):

First, I created a Toolkit object by returning the object passed l>3ck from
Too Hit.getOefau 1tToolkitO . This method returns a Toolkit object that
represents the state of the Java program or applet. You can then usc this Toolkit
object's getlm~ge() method to load a bitmap file . Since we want our applets to be
JAR-friendly so games will run on the web as efficiently as possible, I will use the
getURL() melhod again;
l.~ge

shIp - n.get llllage(getURL< ' star_destroyer .png)):

Traospareocy

Opaque Images
L.et'$ $Ian with what you have already ltamed up to this point-how to load and
draw a bilmap wilhout any tralUpolrmcy. At this poinl il doesn'l mailer whether
you use the Applet or the Tool kt t 10 load a bitfll<lp file beotuse!he end result will
be the $;lITle. I le:ive it 10 you 10 decide whkh method you prefer, and I will use
them bolh interchangt<tbly. L.et'$ wrile a $hon program to $tIVt as a ba5is for
discussing this topic. The output from the BitmapTest prognm is mown in
Figure S.). I ha~ highlighted the key ponions of code in bold in the Iisling thai
follows.

.....................................................
BltaapTeH progra_

lI.po rt
I_port
I_port
t.rt

java.allt, ,
java.applet,:
java. ut t I. ':
java ,net,';

publ1e clan BttaapTest extends ,""pitt tlll)le_mH Runnable I


Int screenlltdth - 6(0;
Int screenlletght - (81);

9S

96

Chapter 5

Bilmap-Based Graphi(s

l..ge1..ge:
Thread g._loop:
RlOIdOli rind - new RaOldOll{):
prlute URl getURUStrlng fllenil ..) 1
URlurl-nul1:
try I
ur I - this .get(lus ( ) .9I!tlleslKlr(e( HI enil_) ;
I
ciltch (hcepUon e) I I
return uri:
public void Inl10 I
Tool kit Ik - Toolk1t.getDdilUltToolklt( ) ;
I .. ge - tk.geU..ge(gnURU-i1Suroldl.png-));

pUblic Yold ltutCl (


gil_loop - n"" Thrnd(lhh);
gil.loop.stut() ;

publlc ~old stopCl {


g...eloop-null;

publlC ~01d runO {


Thrnd t - Thread .currentThrnd();
wMle (I - gUleloop) {
try (
Thrnd .s l up(lO) ;
}

Ciltch (!fturruptedXception e) (
e.prlntStilctTrilce() ;
}

repillnt():

publ1c void updilte{GrilpMcs gl


p.llIl(g );

Tr(lnsp;trency
po,lbllc yotd INtnt(6r'llhtcs 9) t
6r.p~tes2D g2d
16uphtes2DI,:
tnt "tdtb - scrnnWtdt~ . tIlllge.getWidt~(thtsl - I;
jnt hetgM - serunHelg~t - j,qge .getHetght( U,lsl . I;
6

Q2d.dra"I~Qe(1.ge.rUd.nutlnt(.1dt~l. r.nd.nextlnt(he19~tl.

th1s):

This shon program loads the bitJrnlp image shown in Figure 5.4. In many pmgnmrning bngu;oges and gnphia; libraries, you must specify.ll transpamlt pil;d
ccMor to ~ uxd for tnll1SJQrmcy. In the aamplc shown here, the bld rqion
around the edges of the astm>id would ~ considered the Utranparmt ZO~" of the
image. This trnll$parmt color is black in the exampleshown here (with an RGB value
0(0,0,0), but other colors can be used (or the transparent color too-the color pink
(255,0,255) is often used for the transparent color b<.'cause it stands Out so well.
la.... uses a more advanced method to handle trall$parmcy, as the next section

apbins.

Transparent Images
Java is a sman language that handles a lot of things for the programmer automatically, including the drJwing of t ransparent images. This really makes life
easier for a Java game progmmmer because many game librJries use a transpuent pixel for transparency ill$tead of a mask layer. So instead of dealing with
transparency in code, it's handled in the source anwork.lf you supply lava with a
transparent bitmap file, it will draw that image transparently.

f igu re 5.4

This

op;>QOe

b4tmap Image contains no

tr~~rl'l'q

i1tormation.

97

98

Chapter S Bitmap-Based Graphics

Most Jav;l progr<lms u.5<' the PNG foomt beau.5<' it offen deemt compression
and transp,areocy information without sacrificing image quality. You will need to
u.5<' a gr.lphics editor, such as Paint Shop Pro or GIMP, to con\"en images from
....hatever source format they are in (most likely the BMP format) to the PNG
format, along with the ma~k layer that makes tr.ln~parcncy possible.
Figure 5.5 shows the asteroid image loaded into Paint Shop Pro wilh a new
transparency layer. The checkerboard background behind the asteroid image
shows the transparent region.
Ti p
I " - lIlfd IIIiIly 9"Iftc: toiun,. iUdl is I'Iinl: Shop I'fQ. GIMP. -.I ftcIshcp. N\trlNlJI fty
... otammcaIy ciffe"ellc. fty shM' 1loe SII'Ie biSio: 1OOI5et. ondodlg 1loe dly III sel: a
~ . . . and ...... a file 0UI1ll a PNG WIth .. alpha dIameI. 1lle inslructJonl ~ ~
wi t.. SIII'IiI.- b other graphic eoitm; lOU siIr'9'Y _ ~ each mal in m. program'i _
i)'item.

..

_ _~
.. - - -- _.
~~~~
D"'.bY. 1 . _ _ '
'"I "' .
"
t~"',...

. '\
~.

,.

".
,
,

Figure S.S
1lle iSterold Image IIoJS been !to'tn a tr~1 mask t....

--_

Tranwarency

Figu.. S.6
An ilpha dwJnellayer

~ ~..xlIlO

d>r

~teroid

in"!Jl' 10 Ijit i1l1'ampa~.

Ln'stau the same program you just typed in for BitrnapTesl and run it again.
Only this time, it will load up a new \'et"Sion ofthe asteroid I.png fik that has bttn
ediled to support lranspareocy. Figure 5.6 shows tht OIItput from Iht TranspartnlTest program. 1be soura rode has not ehanged (refer earlier 10 the
BitrnapTesl program listing), oollht PNG fill' has changed, which accounlS for
the difference!

Working Some Masking Magic


Ltt's take a look at how you actually crtate a masked PNG image. I'm using
Paint Shop Pro becausc it's very easy to usc. If you want to usc this program,
you can down load the trial version from www.jasc.com.SinceCorelacquired
the company that created I)aint Shop Pro, the link may be forwa rded to
www.co rel.com.[.ve used Corcll'ainter too, and it is alSQ a useful tool with
similar fu nctionality. Let's take a look at the asteroid image loaded into the
program (see Figurt 5.7).
To add a transparency layer 10 an imagt, you need to locate tht Magic Wand
tool available in masl graphics editors. Afttr SC"lccting Ihe tool with your
mouse, dick somewhere in the black region (or on any pixellhat isn't p.art of

99

100

Chapter 5

Bitmap-Ba'led Graphics

.... ~ w.

r
-

..-.

Figu re 5.1
A 3D rt/ldilion of a dumpy asteroid (coorlesy of Edgar Ibarra).

the game object). This should loom the edges orthe game object and highlight
everything around it (Stt Figure 5.8).
Now tha t you have a sdmion available, you need to inven it b<!use this
selediolJ wil1aClual1y exclude the image. In Paint Shop Pro, click on the SdKtions menu and choose [nverl (sec Figu re 5.9). Figure 5.10 shows the result orthe
inverted selecti on.
Tip
If YIlU ' - a ~
and -..Md lite 10 odIde - . , ~ 01 it in ordtr 10 ~_
bculcliq oI ... lNI
YIlU Qn Idd ~ h ~ by'" dilbIg: with ... Magic wad
tool imidr pclI'1ior6 01 thl'
10 add . - ~

The next step is to create a new mask Layer in the image to represent the transparent portion. You can tell Paint Shop Pro 10 generate a mask based on the

Trilnspilreocy

.'
,

Figure 5.8

The cuter edge of Iht ~leroid ilflo1!ll'

- ....
.~

Figure 5.9
Pl'!piriog to inven Iht wlection.

been ...lected willi Iht M. Wand IClOl.

101

102

Chapter 5 Bitmap-Based Graphics

"':'
. """
, ,;";,,,'~~~--- -- '''4
~ "

III

..

- .,-.

.~

'------~ " .~

.... _.

."'
Figure 5.10

- .....-

The boond.Jry of the asteroid is IlOW Sl'lected.

selection you've made in the image. To do this, open the Layers menu, select New
Mask L1yer, and then Show Selection, as shown in Figure 5.11.

>I,
Although I am basing mis Morial on the ex(ellent grapl1k ed~or, Paint Silop ~ most professional graphic editors support layers and prol'icle similar fcatur,..; to mose found in PSP. The GIMP,
for instance, is a freeware griljlhk editor wim (omjlilrable features and is available on many
platforms (Windows, linux, and 50 on). Download The GIMP (GNU Image Manipulation Program)
from www.gimp.org.

In Figure 5.12, the transparency has been created based 011 the masked selection,
The result looks very nice; this asteroid is ready for primetime! You can load this
image into your Java applet and draw it, and it will automatically be drawn with
transparency so the outer edges of the image (where the black pixels used to be)
will not overwrite the background of the screen.

Trarnparency

.....

. ..

f igure S.11
CrNting ~

new mom IIy8 out of !he wIection.

figure S.12
The flteroid image now lias a masffil

tr~ency

1Iy8.

103

104

Chapt~'

5 Bitmap-Based Graphics

What You Have learned


W~ \'Iill wnlinue to \'I~rl \'lith tr;lIlspu~nt imagu from this point fO""'3rd. so
you ha\'~ Ieuncd a \Try irnponant tool in thisdupterthat will make it pol&ibl~ 10
crC'at~ utrmlely atll'3eti\'~ pffieS. Spt'CifiooIl\'. you kamed:

How to dl'3w bitmap images


How to trJnsJal~, rotal~, and scale bitmap imagl!S
How to draw bitmaps with transparency

Review Questions
Th~

following questions will MIl' you 10 determin~ how well }~U ha\'e Ie:lrned
subjccu discussed in this chapter. n.e answ~rs u~ provided in Ap~ndil[ A.
"Q,..aptCT QUil AIlSI'I'CTS."
th~

I. What is the primary clus. wc'\'~ bttn using to manipulate bitmappcd


graphics in this chapt~
2. What method initializes

Ih~

keybo;!rd listener interface?

3. What Graphics20 method is used to draw an image?


4. Which Ja\'a cia$:> contains the get!luge() method?
5. What cia" makes it possible to perform translation. rotation, Jnd SClling
of imagesl
6. Whi<;h Gr.phlcslO method dra\'l"S an ima
7. Which Innsfoml mnhod mo\'es an image to a '-'Ior;;otion?
8. What isttM-

n.an~

ofthc "transparency" channel in a J2-bit P:-lG im;a

9. "'Jut is the Applet class method used 10 load a resource from


10. Which keyLI stener e'em

d~te(\s

<I

JAR!

key prtoSSes?

On Your Own
Usc the following exercises to test your grasp of the materi<ll w\'ered in this
chapter.

On Your Own

Exercise 1
ThcTc are IIklny example programs in this chapter thaI eQuid be modified and
expeOlTlt'ntM upon. TwtoIk tht Randomlllklges program. Modify the program
so lhat it !ooids and dnws two diffeTmt im.1ga nndomly instead of just a single
im.1ge.

Exercise 2
Modify the Dnwlmagt prognm so thaI it will scale the imagt largtr or smaller
with tht uS(' of the keyboard plus (+) and minus (- -) keys.

lOS

CHAPTER

SIMPLE SPRITES

Up to this point you have learned about a lot of jaVll classes that are useful for
m;lking a game, particularly the Graphl cs20 class. The previous two chapters
provided the groundwork for this chapter by showing you how to tap into the
Graphlcs20 class to draw vectors and bitmaps.. At this point, the source code for
evm a simple bitmap-based game will tend to be too compliated and too
difficuh to manag<- without a better way to ~ndle the objects in a game. What
you need at this point is a new class that knows how to work with gameobjectssomething known as an /ICt(Wor a spri'''. The goal ohhis chapter is to develop a
Wily to handle the game objec1s moving around on the sclftn. Here are the
specific topics:
Programming simple sprites
Creating a Sprite class
Learning about collision testing

Programming Simple Sprites


A sprite usually represents an animated graphic image that moves around in a
game and is involved with other images. The difference betwccn a regular image
and a sprite is often that a sprite will encapsulate the image data as well as the
methods needed to manipulale it. We will creale a new class later in this chapter

'"

108

Chapter 6 Simpl", Sprites


callfil laagHnt I I" which will ~ able to load and draw a bitmap, and we will
then cre:;!!e a new Sprite class that will use laageEntlt,.
I would !ike to build a p,air o( cbsses to simplify sprite programming. We will
Sprite cbs.s in this chapter aDd ~n add d~ Anlaat~Sprlte cbs.s in
the next chapter to haDdIe animation. lbe new SprHe cbs.s that I'm going to
show you herr mightlw de5Cribed as a lam"" dus. What do I mean by "heavy"?
This is not a simple, abstract class. Instead, it is tied closdy to the Applet and
Graphics2D objects in our maiD program. You would not ~ able to use this
Sprite class on its own in a Java application (a standalone program) be<.:aus-e it
relies on !he prescntt o( the main apple!: to function. Although it is possible
to write a Java game that runs as an application rather than as an applet, our
focus here is on web games. So, our new Sprite class will work well in this
environment.
c~te ~

A spri te cannot draw itself without the Applet and Graphles2D objects in a main
program. Although the Sprl t e class could usc methods such as getGraphl cs() to
pull information from the main apple!, our examples use a double buffer (a back
b uffer image used to update graphics smoothly, without nickering the 5O:rn).
The 8iSeGa_Entit, class will handle aU of the position, vdocity, rotation, and
other 10gislkaI properties. while laageEnt I t, will mallt use o(them by providing
mrthods $\Ieb as tr.nsfo,..( J and 4rav( ). 1want to simplify the Sprl Ie cbs.s 5(l it
doesn't expose all oftbese propttties and methods., but provides a simpler means
to load and draw images. This simplification willlw especiaJIy helpful in the IJCl[t
chapter be<.:au.se animation tends to compliOlte things..
A useful sprite class should handle its own position and velocitydata, ralher than
individual X and Y vaJut$ (or these propttties. The sprite's position and velocity
will be handlfil by the 8iSeG._Entlty class. The Sprite class will not inherit
(rom llaageEnt I t,: instead, it will use this class internally, like a regular variable.
I also want the accessor methDds to resemble simple properties, while the llIutator
methods will be in th e usual "set" format. For in stan",~, I want the Sprite class
to have a position() method that returns the position of the Sprite obj ect,
bu t it will use a setPosltion{) melhod to change the X and Y values. For
instance, I want to lw able to access a sprite's posi tion and velocily by wriling
code like (hi$:
sprite. pes it i on( ) .Il )
sprlte.pesltion().l( I

Programming Simple Sprites


On top of these requirements, we should not be concerned with numeric data
types! I don't want to typecast integers, tloats, and doubles! So, this Spr i te class
will need to deal with the differences in the data types automatically and not
complain about it! These aTE! minor semantic issues, but they tend to seriously
clean up the code. The result will bea solidly built sprite handler. First, let's take a
look at a support class that will make it possible.
Tip

AA Mcessor method is a method that returns a private variable in a das~ A mutator method is a
metllod that dlaoges a private variable in a dass.

The PointlD Class


The Point20 class is a helper class with the sole job of storing and providing X and
Y values in a variety of numerical formats. There is a lava library that also
contains a POlnt20 class, but it w;IS unsuitable for our purposes here. However,
there should be no conflict because that other class is contained within a package.

1..

*0 . . . . . .

Point20 Cl ass

**......

..................1
Polnt20 utends Object l
private doubi e ~. y;

ChS5

lIint constructor
int y)
setX(x);
seU(y);
I
Ilfloat constructor
Point20(f1o~t x. flo~t y)
setX(x);
sety(y);
I
Iidouble constructor
Point20(doublex. doubley ) {
setX(x);
setY(y);
Polnt20{int~.

IIX property
double X() { return x;

109

110

Chapter 6 Simple Sprites


public yold SHI(double x) ( thls.x ~ x: I
public yold SHllflolt xII thls.x~ ldoublel x: I
""bllc yoid sHI(lnt x) I tbis.x ~ (double) x: I
' " prolH!rty
double HI (rebrny: I
public yoid setHcIoub1e yl I this., ~ Y: I
public yold seU(flolt Yll thls.y ~ ldOliblel y: J
public yold seU(lnt yl I this., ~ (doublel ,: I

Basic Game Entities


The 8He~ectorShape class was introduced oock in Chapter J for the Astemu/sslyle game. We will uSC: a very similar class for sprite programming in a fulure
version of Galactic War (beginning ill Chapter II, "Galactic War: From Vectors
10 Bitmaps"). Here is the code for Ihis class.
public cllss 8neGa.eEntttJ utends Object l
IInrl,bles
protectell boolun,l he:
protected double x.J:
protectell double Yell. Yell:
protecte<l double .aYe",",Ie. flceAngle:
lI"ussor .ethods
public booleiln ls,l,llYe(l1 return ill lYe:
public double 'leU(J I return x: J
public double geU() I return y: J
publlcdouble'letVeIX() I returnwelX:
public double getVellO I return well: I
public double getllove,l,nglel) ( return .awe,l,ng1e ;
public double getfilceAng1e() ( return fi u,l,ng ' e :
II_uta tor ftthods
public WOld setAI"e(boolun alive) I this ill"e-illiwe; )
public WOld seU(double x} I this.x - x: I
public woid \ncHdouble \} I this.x .... \: I
public woid setY(double Jl I tbis.y - y: I
public void IncY(<louble 1) I thls.y .... 1: I
publ Ie vol<l setVellldouble nlll ( this. veil - YeIX:
public voi<llncVelX(double III thls.YeIX .... l: I
pub1 Ie wol<1 $ftVe1V(cIouble welTl I this. nll- YeIT:
pub I Ie wol d 1ncVeIT(cIouble I) I this. Yell .... I: I

Programming Simple Sprites

pub11 c voi d setFace~og1 e(double


public void incFace~og1e(double
pub11 C voi d setHove~og 1e( double
public void incHove~og1e(double

aog1e) I ~h1 s . fa ce~og1 e - aog Ie;


I) I thls.hceAogle .... i; l
aog1e) I thi s . lIIOve~ng Ie - ~ngl e ;
I) l thls.MOve~ngle .... j; I

Iidefault constructor
BaseGameEntlty() (
set~1 Ive{hl se);
setX(O.O) ;
setHO .Ol;
setVeIX(O.O);
setveIHO.O) ;
setHove~ngle(O.O)
setface~ngle(O.O)

;
;

The Image Entity Class


The ImageEn t i t y class gives us the ability to use a bitmap image for the objects in
the game instead of just vecloT"based shapes (such as the asteroid polygon). It's
never a good idea to completely upgrade a game with some new technique, which
is why some of the objects in the first version of Galactic War will still be vectors,
while the player's ship will be a bitmap. When you reach Chapter II, you will
have an opportunity to examine the progression of the game from its meager
beginning to a complele and complex game with sprite entity management. I
always recommend making small, incremental changes, play-testing the game
after each major change to ensure that it still runs. There's nothing more frustrating than spending two hours making dramatic changes to a source code file,
only to find the changes have completely broken the program so that either it will
not compile or it is full of bugs.
The ImageEnt I ty class also inherits from the BaseGameEnt I ty class, so it is related
to VectorEot i ty. This class is awesome! It encapsulates all of the functionali ty we
need to load and draw bitmap images, while still retaining the ability to rOt'lte
and move them on the screen!

10*................

..........................................................
ease galle I_age cl ass for bi t _apped game ent I ti es

import Java.~ ..t.;


1mport java. a..l. Gra ph Ics2D;

111

112

Chapter 6 Simple Sprites


jllpCrl jiIYa.awt.t_.:
jllPOrt jan.app1d. :
IllPOrt jan.nd. o:
polIbllc class IIliteEntlt7 u tend s &as e&a_Ent1 t7 I
Ilnrlables

pl'1ltecte<llllite lllige:
protected Applet applet:
protected Affll1l!TranHonl at:
protected Graphlcs2D t2d:
Ildefau I t cons tructor

l.ateEn t lt7(Applet a) I
applet-a :
setl.age{ nu1 1 l :
setA l 1ve{true):

public Illite tet1Ilite() ( return lllige:


public vo1d sellllite(Jllite Illite) 1
this. Illite - Illite:
double ~ - applet .tetSheD .wldtM2
width( 112:
double 7 - applet .tetSlzeD .Mltht/2 heltht( )12:
at Mf1I1l!Transfonl.9l!tiranslatelnstance(l. 71:

\lllbilc lntw1dthO (
If (Illite I-null)
return lllige. tellli dth( applet) :
el se
return 0:

I
public lnt height( l {
If (1.age I-null)
return l.age.getHelght(app1etl :
else
return 0:

public double tetCenterlO I


rdurn geU( I + width() I 2:

Programming Simple Sprites

pubHC double gUCtnterYO I


return gUYO + \l.tlgMO / 2:

public yo1l! sU6r.lplllcs(6r.lphlcs2D,ll


,2d g:

prlnh URl getURLISlrlng fllenallt) 1


URlurlnull:
try I
uri ~ thls.gttClass{) getRuource(fl1enallt):
I
catch (hcepl101\ ell
return uri;

Pl/bllc yold lo.ldCStrlng fHena ..)I


llIa~e awlu.gulllage(guURl( flltnalltl):
II~lle(getillage().gulildth(applet) (. D):
double ~ ~ applU.getSI ze() .II1dth/2 - "ldth( 112;
double y - app let. getS I ze( ) . hel ght/2 hel ght( lI2 :
at Affl ne Trans for. getT rans la le Instance( x, y):

public void transfor.() (


aLsetToldent Ity():
at. translale( I IntlgetiO + "ldthO/2, (Int)getY()
at. rotate( IUth. toRadlJns( guFueAng IeC ) I) :
at . translatel "Idth()l2, htlgM( 112):

helgM0l21;

P\lbllc wold drawl) I


g2d .drall lllage( getlllageO, at, .Ipp1et) :

IIboundlng rectangle
publIc Rect~ngle gel8ounds() I
Rectl ng le r:
r ~ new Recling I e( (I ntlgeU (), (\ ntlgttY ( ), ,,1 dth() , hel ght ( ) ) :
return r;

113

114

Chapter 6 Simple Sprites

Creating a Reusable Sprite Class


Following is the source code for the new Spri te class. This class includes a 1011 of
features! In fuct, it's so loaded with great stuff that you probably won't even know
whattodo with it all at this poin!.1 am not a big fan of inheritance, preferring to
build core functionality into each class I usc.

Collision Testing
The Sprite class includes several methods for detecting collisions with other
sprites, and it also provides tests for collision with Rect~ng l e and Poi nt20 objects
as a convenience. Remember that I wanted this Sprt te class to be intuitive and
not cause the compiler to complain abou t silly things, such as data type con
versions? Well, the same is true of the collision testing code. There are three
versions of the CoIl i desW1 th() mcthod in the Spri te class, providing support for
three different parameters:

Rect~ngle

Sprite
Polnt2D
This should cover almost any game object that you would like to test for a
collision. Since these mcthods arc built into the Spri te class, you can call them
with a single parameter, and the internal data in the sprite itself is used for the
SKond parameter that would normally be passed to a collision routine.

Sprite Class Source Code


This new Spri t e class does not inherit from anything other than the base Object,
although it uses Im~geEntlty internally for access to that class's excellent
support for image loading and drawing. Why doesn't this class inherit from
B~seGameEntlty or ImageEntlty? Those classes followed a logical inheritance
chain but also included a lot of features that do not need to be in the core of the
Sprite class, that's all. We still have access to those properties and methods if
we want to use them, by using an ImageEntlty as a private variable, but we get
around the problem of having to deal with private/public access and inheritance.
Inheritance is a beautiful concept, but in practice too much of it can make a
program too complicated.

Creating a Reusable Sprite

C~

..........................................................
Sprite tIns

<_por t jUI .I Mt .:
IllPort JUI.applet.:
p~bllc

clau Sprite utends Object I


prlnte IN9l'nt1t1entlt1:
protectell Polnt2D pos :
protected Po lnt2D vel:
protecte d double rotRHe :
protected lnt cu r rentState:

Ilconstructor
SprlteCAwlet a. Graphlcs20 g2dl [
entHy ~ new IflIgeEntltyCa):
ent 1t y .setGra ph Ics( g2d) :
enl1 t y. se Ul l .e( fa l se) ;
pos new POlnUDlD. OJ:
weI new Polnt20(D. 0):
rot~te ~ 0.0;
currentStateO;

IIl ol d blt_a p ' 111'


publlc void IOldCStrlng fllenUle)I
etltlty.1oadl H lenalle):

Ilpe r fonlaf'l ne trans fOnll t Ions


pu bllc Yol d t rans for_C) (
ent Ity. seU ( pos. X( l) ;
ent1ty . setHpos . Y( 1);
en t 1ty . trans fOnll I :

IIdrlW tke hlilge


public .oldd raw(J (
ent1 ty .g 2d.dra M[_ageCutl t y get I_agel l .ent l ty .at .ent1 t y.appl et);
IIdrawbounding rec t angle around sprite
PIlbllc vold draw8ounodslColor cl I

115

116

Chapter 6 Simple Sprite5


entfty.<j2d. nColorl
entit,.92c.dr..,l<jel8<.

liS

the positIon besed on ~eloclty


public vold updltePosltlonO 1
pOI . setltpos.X() + vel.XO);
pos.setY{pol.Y() + vel.H ;

lIupd~te

rellted to .ul,..tlc rOUI on heeer


public double rOUtfonbte{)1 return rotbte; I
publlC ~old setRotHlonRiteldoub1e rite) roU.te- rite;!
public ~old updlteRot~tlon() 1
set F~ceA"g1el f ~ceAn<j1 e ( ) ... rotR~te) ;
If (hceA~ <jle() <0)
letF.ceAn<jlell60 rotR~te) ;
ehe if (hceAngle() ) lEO)
setficeAn<jle(1"( 'Il,te :

II-el~ods

IItenerlc sprIte sUte url.ble (~1 he. 6ud. coil 'ded. et c.)
publIc Int sutel 1 I return currentStlte; I
pub1Ic ~oid setSUte( i nt s t~ te) ( currentSt~ t e - stl te ;
Ilreturns I bounding rectlngle
public Reclugle getBounds{)! return entlty.getBoundsll ;
I/sprlte posttlon
publlcPolnt211pos'tton lreh"'pos;!
publIc voId setPos; t iOllIPolntlll post I thl s.pos - pos;
II spr I Ie lIO.e.ent vel oc tty

pub1 ic Polnt20 velocIty!) I return vel ; I


pub1 ic vo id se tv elocHy(Pol nt20 vel) I t his . vel - vel;
Ilreturns U.e center of l~e sprIte IS' Point21l
publ c PD'nUll certer(
return( ntll Pol nUll( ttl: ; ty. getCenterl( ). entity. 9ttCtflterl \ I)

IIgeneric ur1lble for selecthel, usln<j lpr'tes


pub It c boo1tiln 111 ~e() I return ent' t, t sA1heO ;

pu blic .oid se tAlhe(boo l ean .li.ell entlty.leUlhe(a1i.e) :


llface angle i nd i cates ..hi c~ 4i reet1on ~pr i te is fac 1"9
publ ic double fUeAl>gle( I I return ent itl.tetFaceMgle( I: I
publ ic .oid setFace gle(double anglel I
ent Itl . setFace gI e( ang Ie) :
I
public . oid se tFaceAngle(float angle) l
ent! tl . setFaceAng let (double) angle):
I
public .oid setFace.....gle(lnt angle) I
ent1 t, . setfaceAngl e( (double) angle):

11.,.e angle l/ldicates direct lOll sprite Is .,.'ng

publl c doubl e .a.eAng1eO I return ent Itl .getllo.ung1 eO :


publ i c .oi d setHoweAllgl e( double Ingl e) I
en t lty. sHMoweAngle(angle) :

I
public . oid setllo.e gle{float angle) I
ent Itl .seUlowe gle( (double I al\91 e):

I
public .oid setHoweAngle(lnt angle I I
ut I ty .setKo>eAn gIe ( (double) ang1 e) :

llrehrns the source Illige Ifldt~//lelg~t


pub lie Int Illigellldt~() I return ent It)' .If' dt~(): I
public lnt ladgeHeightIJ (return entlt)' . ~elg~t(J:
for coil islon If l tn a rectangul ar shape
public boolean collideslli t h(Rectangle rect) I
return (rect. Inter~ectS(get80u/lds(J II:

Ilt~eck

I
I fc ~ etk for coli i sion with another sprl te
pub11 c boo1e. n co1li des ll l Ul (Sprtte spd t e) I
return (getBou"lls( I. Intersects (sprl te. getBounds ( ) 11 :

I
IIcheek for coli h i OIl with a poi nt
publ ie boolean coil idnll HMPolntZO point) I
retu rn (getBo un d1( ). con t ai nl (poi n1. X( ). poi nt

Y()) 1:

118

Chaplft 6 Simple Sprites

publiC ~pplet IppletO 1 returne"tlty.lpplet; 1


pub II c Graph 1cs2D grlph I cs(} I return e"t 1ty . g2d;
publIc luge l..get)I return entity. luge; 1
PIlbl1c wold setlu9t( luge 'ugt" I ent 1ty. set lugetluge);

Tip
6 II feJtI.ft nssing &om I!le Sprl te diss..: this pcwot we wiI 90 _lhil
next dIIpIer.

.........._

.. _

sW!ecl

Testing the Sprite Class


Let's gi\~ 1M Ill!'\'> cbsl;cs ..~\~ dc\"dopro in ltus d1iptn I tal run. The follo..ing
program (shown in Figurt' 6.1) dr;tws a badcgmund inlaU:and then driwS a ~pril{'
randomly on the screen. This tcst progrlUlI uses a thread and the Runnable interface
in order to draw a sprit{' repeatedly on th{'SCIl'('ll without user inJluL W{"U study this
kalurt' more lhoroughly in Olaptn 10. "Timing and ttlt- Gaml' loop." when w{'

r19uA! 6.1
1hlo SpitloTest

~ogram

demonstrates haw to 1M lhf

Sp'H~

cliss.

Creating a Reusable Sprite dass

kam more about threads and the pmt' loop. Sl:udy this short demo prosnm wdI,
bcaUK it ckmonstrates ~ps the lim high-speed enmplt you.~ $em thus far.

I
~
p rg gr~

Spr l teTnt

1l1port j~u . llf t.;


tllPllrt JUI.llIt. i-.lge
ll11Pl1rt jUI,lppleL ' :
'.rtjul,,,ttl, :
tlllPllrt jlu.net. :
publlc cllst SprlteTest extends "'PIll et 1.,.1e.ents Runnable (
lnt scree nll id t h - 640:
l nt sc ree nHe19ht - 480:
IIdouble buffer objects
hffertdl-.lge bACkbuffer;
6npkics2D 92d;
Sprite utero1d:
1-.l9tht1tJ' back!lround:
ThrUd IIll1t l oop:
Rand a" rind - new Rando-() ;
public voId i nit() {
IIcrute the back buffer for s.xlth gr aphics
INlckb<offer - new 8uffereill-.lge( screenll i dth, screenHt i ght.
kfferedlute. TYPE....I.T_IlG8) :
g2d -lNlckbuffer .creat~nplltcslI:
1I10ld the bACkground
background - new lugeEnti t~( t his) ;
blc kground . load( "b1"espl te . pn g" ) ;
1/101d the uterold sprite
astero1d -11ft Sprlte(thls, !I2d) :
asteroid.10<lld (uterol d2, JMI9- ) :

publiC void stlrtO I


gillt loop - newThreldlth is) :

119

120

Chapter 6 Simple Sprites


galll('lOop.s t~r t(l ;

publicvoldstop(){
gall\l'loop - null;

public void runO I


Thread t - Thread.currentThrudl J;
while (t - -qallleloopl I
try (
Thrud .sleep(301;

I
cnch (JnterruptedE~ceptjO"e) (
e. prl ntStack Tracel ) ;
I
repalnt();

public void update(Graphlcs g) l


Ildraw the backg round
g2d .d ra",lllge(background.getl.uge(J . O. O. screenliidth I .
screenHeight'l. thlsl;
int",ldth - screenliidth . asteroid. 'llagelildth() . I ;
int height screenHelght Hteroid. IlIIageHeiqht{} 1;
Pol nt2D pol nt new Poi nllO (rand. ne~t Intt "'I dth 1.
rand

"e~tlnt(helght)l;

as tero Id. setPos It' on( pol nt) ;


as tero j d. transforM( 1;
as tero Id.drawl };
paint{gl;

public voId palnt(Graphlcs 9) (


Iidra wthe back buffer to the screen
g.draw!luge(backbuffer. O. O. this) ;

R~~iom

What You Have learned


This significau l chapler produced a mQnum~ntal new version of Gaklctic War
that is a foundalion for Ihe chapters to come. The final vestiges of the game's
\"CCIorbased roolS ha~ hem discarded, and the game is now fully implemented
wilh bitmaps. In this chapter.)'Ou learned:
How to create a new, powerful Sprl te class
How 10 dl'll'<:t sprite collision
How to write ~ble methooh and classes

Review Questions
The following questions will help you to dctermine how well you have learned
the subjects discussed in this chapter.
I. What is the name of the suppo" class created in this chapler to hdp the
Sprl te elass manage position "nd ~Iocity?
2. During which keyboard event should you disable a key-press var iable when

detecting multiple key presses with global

variables~

3. Which thrtt types of parametCTS a.n you pass to Ihe collldesVltM J


method~

4. Which Java class provides an alternate method for loading images that is not
tied to the applct~
S. Which Ian package do you need to impo" 10 use the GTlpll1cs20 class?
6. Which numeric data type ~ the POl nt20 class (creatoo in Ihis chapler) use
for internal storage of the X aud Y VOllues?
7. Which data typt$ can the Potnt20 class work with allhe constructor 1evel~
8. Which sprile property dC'lennines lhe angle at whieb the sprite will

mov~

9. Which sprite property determines at which angle an image is pointed.


regardless of movement dirl'<:tion~
10. Wh ich MftneTransfor. method allows you to translate, rotate, and.scale a
sprit~

III

122

Chapter 6 Simple Sprites

On Your Own
Use the following exercises to te51 your understanding of the malcrial cO"erc<! in
this ch;Lpter.

Exercise 1
The SpriteTesl program demonslr:ues the usc of the Sprite dass. Modify the
program so thaI it draws multiple instances ofthe asteroid sprite on Ihe .screen,
each moving and animating differently.

Exercise 2
Modify the SprileTest program C'o'en further by adding collision testing, such Ihat
Ihe asteroids will rebound off one another when they collide.

CHAPTER

SPRITE ANIMATION

This chapttT adds a significant new feature to )'Qur box ofJa.... tools-the ability
to load and draw animat~ sprites and apply thai knowkdge to an rnhancrd new
sprite clau. You willieam about the different wa)'510 store a sprile animation
and how to acc~ a single frame in an animation strip, and you will s a new
class called Ani.atedSprite with some S('rious new functionality that greatly
extends the baS(' Sprite class. Here are the key topics:
Sprite animal ion techniques
Drawing individua.l sprite frames
Kccping track of animation frames
Encapsulating sprite animation in a class

Sprite Animation
the years I have seen many techniques for sprite animation. Of the many
algorithms and implementa tion$ I've sludird, I beliC'V(' there an: two CSSCTItial
ways to animate a graphic object on the screen-by loading individual frames,
each stored in its own bitmap lile. or by loading a single bitmap containing rows
and columns of animation frames.

()o,-er

124

o..pter 7 Sprite Aoim;,tion

Animation Techniques
First, there is the ~"or<t method. This type of animation involves loading a
bitmap image for each frame of the animuion in s.equmce, and Ibm animating
them on the scret'n by drawing each image in order. This technique tends to take
a long time to load all of the animation frames, especia lly in a large game with
many sprites. There is also the system overhead rfq uir.-d to maintain so many
images in memory, even if thq are unall. Figure 7.1 show, an enmplc.
Drawing an anillliltion sequence is somewhat of a cha1knge when loading individual fmmes because of the logistics of it. How should you store the images-in
an array or a linked list? I've.setn some implementations using both methods, and
ntithcT is very friendly, so to speak, becat= the code is so mmplialted.

The second sprite animation technique is the tiled method. This type of animation invol,cs storing an entire animation sequence inside a ~ingle bitmap file,
also known as an 1I11;rWI/;otl Slr;p. Inside this bi tma p file are the many frames of
the animation laid out in a single row or with manycolumllSaod rows.. Figure 7.2
shows an animation strip on a single row, while Figure 7.3 shows a larger animation with multiple columns and rows.

Drawing Individual Frames


The key to drawing a single frame from an anillliltion sequence stoTt'd in a tiled
bitmap is to figure out where each frame is local.-d Iligoritllmiallly.lt's impossible
to manually code the X and Y posit ion for each frame in the image; the very

Figure 7.1
An ri'nation

~ wiIh

figure 7.2
An animation

~trip..,,;m

I t _ SlOItd ..

a 'iingit row.

~ bmnap

filrl.

Sprite Animation

Fi\Jure 7.3
No iIIIimatiotI 5ll1l.mh ..... cdurMi il'Id two rows.

thought of it gives me hives. Not only would it take houn to jot down t~ X,Y
position of every frame, but the bitmap file oould easily be modified, thus rendering the manually c:akulated poinl$ irrelevant. This is computeT scienc:e, alter
all, $0 theTe is an algorithm for almost everything.
You can eakulate the column (that is, the number of frames IlcrOS3) by dividing
the frJme number by the number of eolumns and multiplying that by the height
of each frame.
frllleYe (frllieHullber I colU1II15) helghl;
This will give you the correct

row down into l~ image where your d~ired frame

is located, but it wiD not provide you with the actual column, or X value. For that,
you need "similar solution. Il1$Iead of dividing me frame numoo by oolumns,
we will use modulus:

frjlltl ( fralleHuilboer 1 col.-5 ) II1dth;


A5 you might have noticed, this looks "Imost CX3C1ly like the fonnula for calculating fralleY. Now we're multiplying by width and using the modulus character instead of the divi$io" character. Modulus returns the remaitrder of a
division, rather than the quotient itself, If you want the Y v,llue, you look at the
division qllotient; if you want the X value, you look at the division rt'maimlrr.
Here is a complete method that draws a single frame OUI of an animation
sequence. TheT(' arc a 101 of parameters in this method! Fortunately, they are "II

12S

126

Chapter 7 Sprite Animation


clearly Iabel"d with descriptive names. It's obvious Ihat we pass it Ihe source
lilag e, the destina tion Gr aph l cs2D objecl (which docs Ihe real drawing), the
destination location (X and V) , the number of columns across, the frame number
you wanl to draw, and then Ihe widlh and height of a single frame. What you g<"t
in relurn is the desired animation frame on the deslination surface {which can be
your back buffer or the applet window}.
pub II c yol d draw Fra lle{ lu ge source . Grap hf n2D dest .
Int destx . In t desIY . Int C015. int fraae . Int Width . Int hel ghtl
Int f ra ll('X - (f rame S co l s) widt h;
Int fra ae~ - (frallC I cols) ' heigh t;
des t . drawi llage( source . desIX. dest y des t u.,.1 dth . des tHhel ght .
fraOle X. f r alle~ . f r all e~ .....ldth . frarne V+height . t hIs) :

Keeping Track of Animation Frames


Acquiring the desired animalion frame is just the lirsl step toward building an
animated sprile in Java. After yOll have figured out how 10 grab a single frame,
you muSI Ihen decide what 10 do wilh it! For inslance, how do you tell your
program which frame to draw, and how does the program update the current
frame each lime through the game loop?
I've found that the easiest way to do Ihis is with a simple upd,lle method that
increments the animation frame and then tests it against the bounds of the
animation sequence. For instance:
eu rren t f ra ae .... anhu t IenOl rec tI on :
(eur rent Fralle) totalFrame5 -Jl I
eur ren t FrallC - 0;

if

else If (cu rrentfr alDC <0) {


eurrentFrame - totalFra:ne5 I ,

Take a dose look at what's going on in lhe code here. First, the current frame is
incremented by all arbitrary value stored in a variable called anlJla t 10nOI reet Ion.
This will always be either -lor t to animate forward or backward. Then. the
next line ehed::s the upper boundary (tot a IFr allles J) and loops back to 0
if the boundary is crossed. Similarly, the lower boundary is checked, ~lting

Sprite Animation
currentrra-e to the upper boundary value if necessary. The end result is thaI
three VlIIriables are needed:
currentfra-e
totalfra-es
Int .. ttonOtrectton
You would WlInt 10 call this upcbte cod... from the Ihread's run( l ~t method.
But, speaking of th... thread, tMt does bring up an important issue-timing.
Obviously, you don't WlInt every sprite in the game to animate at exactly the same
rate! Som... sp rites will move very slowly, while olhers will have fast animations.
This is re;lllyan issue offine.tuning the gameplay, bUI you mllst have some sort of
nu:<:hanis1l1 in place for implementing liming (or each animated sprite st'pamtdy.
You can accomplish this by adding a couple more variables to the mix. First, you
will net<! to increment a co unter each time lhrough the game loop. If that
counta reaches a certai n threshold value, then you reset the counter and go
through the process of updating the ..ninution frame as before. Let's use v;ui
abies called fra-eCount and fra-eOelaJ. The frame dclay is usually a smalla
value lhan you would expecl-sudt as 5 to 10, but usually not mudt more. A
ddayof JO in a game loop running at 50 Cps means that the object only animates
at S fps, which is "ery slow indeed. I often use values of 1 to S for the frame delay.
Here is the updated animation code with a dclay in place:
fra..co.nt++:

1f (frCount) fr._OellJ) (
fra..count.o(l;

currentrr _ ant ..ttonOt rectlon;


If (currentFra.... ) tot.1frs\) I
currentrra. - 0;
I
else If (currentfra-e < Ol {
currentF ralie - tolal fra.s\;

Testing Sprite Animation


I'd like to go Ihrough a compktl' enmple wilh you so thesl' COI\Ce-p1$ will fed
more md to you, .. nd so that you can see thl' dramatic result when a sprite is

127

128

Chapter 1 Sptite Animation

Figu re 1.4
All animated blIII with 64 11"-.

animaled. ~ AnimalionTest program loads a massive &I-frame animation


St'quenct (shown in Figure 7.4) and animates it on lhe streen while moving the
sprite :1Tound at the sallie time. Since we are slicking to lhe subjl of animation
in this chapter, the program doesn't attempt to do any transforms, such as
rotation. But can you imagine lbe resull of an animated sprite Ihal can IlOO be
rotated? This program will help 10 determine ....tLat we need to do in the
upcoming animation class.
The output from the program is shown in Figure 7.5, where the single animated
sprite is bring drawn over a b."kground image. Following is the cooe listing for
the AnimationTesl prognm. I havc highlighted Uy portiol15 of code (Ihat are
new to this chapler) in bold lexl.

.
.....................................................
/

IlIpOrt JilYi. i.t . ':


IlIpOrt jiIYiI.i1pplet.:
IlIpOrt JilYiI .ut 11.':

I.port

jIVII . awt

. I.age

pUbllc clin AAI ..ItIO~Tut ute~ds .l.pplet


stitle l~t SCRHIlIIIDTH ~ MO:

l..,l_~ts

RuMilble I

_,
_.
....

figure 1.S
1hlo AAunationTel program.
stattc tM SCR[[NHElGHT - 480;
Thrud gllle1 oop;
Rando. rand new Randall();
IIdoubh buffer objects
Buffered I.age blckbuffer;
Graphtcs2D g2d ;
I/background tllolge
lllolge background;

IIsprtte nrllbles
IlIolge blIll:
1nt balll 300. blIllY" 200:
1nt speedl. spHdY:
lIutllolttOll nr11bhs
1nt currentfrl." D:
tnt totalfr. .I" 54:
tnt u1l1olt1onD1rKttOll- I:
tnt fr..count .. 0:
tnt fra.tlell, .. 5:

Sprite Animation

129

130

Chapter 7 Sprite An imation


private
URl

Rlget"Rl(Str;~g fllen,W)

~rl a n~ll;

'" urI -

t~h.gftCllSsl)

.9ftllesourcelfl 1e<lilw):

J
Cilte~ l{lceptton el /I
return ~rl;

public yotd tnHll 1


Tool ki t n Tool ki t. getlli!fau l troo l. t tl ) ;
I/crul! the bilCk buffer for sllOO t h grilphlcs
b,ckbllffer nelf 8ufferfdllUgel SCREEII1l!OTH. SeRE'
8~ffere~:II.ilge. TTP_:U_R&8I;
92d bilnbuffer .crenfCrilp~ic$():
IlNd t~e Nckgro~nd ilU~
Nckgrouno tk.ge" :ugl (~t Rl ""oOOgrilin.png"
I/l0ild the billl ilnlailtlon strip
billl tk. getilUge( gftURLl aXb' 11. png )) :
speedX. r,nd.nutlnt(6)+l;
speedY . rilnd.~utlnt(6)+1:

public void surtl) 1


gil'lle 1ClOP n.... ThrUd~ til Is' :
9,w100p . stirt():

publlc votd stopl II


gilwloopn~ll:

public yold runll r


Ihreild t Thrud . cu r re~tlhreild() ;
lfht l e{t-gilll-e l oo p) (
try l
ThreilO. $1 eep: 20) ;
cnch

f ~ntfrrUjlad.cept len

el I

~.[ I G~l .

Sprite Animation

e.prlntStacHrace() ;

I
glieUpd.tet);
fellllntl);

publlC wold ga.UpdlteO I


II$H: If It's tl. to ani ..te
frlle(_t++;
If (frl.cOUllt > frOell1) {
frUII(OUlltooO;
lIupdlte the ,nl..tlon fn.
currptfr. . . . I"i ..ti ollOi rKt f 011;
If (currentFr > totllfn.s 1) {
cllrrentfr O;

J
.1st If (currentFre. (Ol (
current Fr totll frl.S );

lIupdate the ball pO$!tion


ballX .... $peedX;
1f (ball X <0) II (ba 11 X > SCRHMWIOT~ 64)) I
speedX '- 1 ;
ballX .... spudx;
I
balU .... speedy;
If billY (0) II (bill T ) SCREEN~E1GtlT 64)} I
speedY '- I;
bllly .... speedT;

public wold IIpd'te(Grlghln gl {


IIdr," the blctgro~lId
gld.drllll..ge(bickground. O. O. SCRWIIIIOTH I. SCRE[llH[lG/lT-1 . this);
IIdr," Ut current fre. of 1"1 ..t1011
drillfr,..tbll!. g2d, bllll . b.llY. 8. curr.ntFrl., 64, 64);
g2d .$ft(olor(Color .BLAell;

131

132

Chapter 1 Sprite Animation


,2d.dr."Strlll9("PosltlO11: tbllll1t . . " tbllilT, S, 101:
g2d.dr.vStrln9( "Yeloclty: " + speedl + "," t speedY. S. 2S):
92d.dril"Strln9("AnllNtlon:" tc"rrenHr.... S, 401:
pil1nt(9):

p<lbllc ,old p.lntlGr.phlcs 91 I


Ildr." the b.ck buffer to t~e screen
9.dril"tuge(bilckbuffer. O. O. t~II):
IIdr." is1ngle frillle of i1n1.i1110n
pu b11 c vol d dril" Frilae(lililge source. Grilp~ l c$ 20 du t ,
Int x. Inty. Int col5, lnt fUlIe,
Int "ldt~ . lnt ~e19~tl
Int fx (fullel coli) tlf1dU:
Int fy. (fullel coli) t ~e19~t:
dest.drilwtuge(so<lree, x. Y. llWldth. )'+fIelg~t.
fx. fy, fx...... ldth . f)'+flel9ht. this):

Now, after reviewing this code, you mighl be wonderi ng why we aren't using lhe
lU!ileEntHy and Sprite cluscs from lhe previous chapler, since they would WI
down on so much oflhis code. That's a good question! The reason is this: I don 't
like 10 build 100 m3ny dependencies into class definitions. As I explained in lhe
previous chapter, 100 many links in an inheritance chain om lead to problems,
This progr:am is completely Idf-contained, withoul nd of any atra classes.. A
single, self-conlained example is far more educalional than a short program filled
Wilh object variables dl'Clared from (Uslom classes. Al a certain po inl, we wi ll do
thaI. BUI righl now, let's focus on just one subject withoul any dependencies.

Encaps ulating Sprite Animation in a Class


There arc some significant ncwpia:es of cock in the AnimalionTesl program Ihal
we'll definitely nd for Ihe upcoming Galactic War projed (in Part III), All of
the properties can be slllffed (lhat's slang for rnalpsullllM or wrappdJ into a
class, and we can reuse thaI heautiful dril",frillleO method as well. One really

Sprite Animation
great thing about moving drnFrllle{) inlo a class is thaI moSI of the paramelers
can be eliminated. as they will be pulled out of Ihe class in lernally. Selting up an
animation will require a few steps up fronl when the game Slarts up. but aflet
lhat. drawing an animaled sprile will be an automatK process with just one or
IWO melhod ails. Note. also. that we're inheriting from the basic Sprite class
from the pl'n'iow chaplet! I'D highlighl the important code in bold.

,
.
.....................................................,
AlilutedSprl te ellss

l-.or,
l-.oH
111ll0rl
laporl

Jan.awt. ;
Jan.applet .;
java.avl.luge .';
Jan.net. ;

class AnlaatedSprlte extends SprIte {


/ltnls Il1ige holds the large II led bHaap
print!! luge an1.'.age;
Ifte.-p luge passed to parent draw llethod
Bufferectluge tMPluge;
liiraphlcsZD tMPSurfaee;
/leuuOll properties
prlnte lnt eurrFr. tatF,..,;
print!! lnt anl.olr;
print!! 1nt frCount. frOeIl,;
prlnu lnt frll1dth. fr1llllght;
prlnte 1nt eah;

pu~lle

public AnluudSprlte{Ajlplet aPlllet. 6rJphlcs20 g2dl (


super(applet. g2dl;
eUrrfriUle - 0;
totFralles - 0;
anl.olr-I;
frCount - 0;
frOelay - O;
frWidth - 0;
frHelght - 0;
eols - 0;
prlnte URl 9l!tuRUStrll>g fllen_1 1
URlurl-null;

133

134

Chapter 7 Sprite Animation


tryl

uri thl s. getC Iass () geUesource( HI enalle1:


)

citch (Exceptlc.n e) II
return uri ;

publtc lold load(Strlll9 fllen Int col ...s. Int rOOlS.


Int width . Int height)
J/IOid the tiled .nINt1on bll.llap
Tooltlt It - Tooltlt.9t!tllefi\lltTooltltO;
anl~Noe tt.oeUN,e(9t!tURl(flleni.ll;
ntCal ...s(cal.-s);
setTaulfra.s(colu.nsrows);
setfr1l1dth(wldth) ;
seU ralleHel,ht (hel oht l;
flfr IN,e Is pissed to parent class far dra"lng
t"pIN,e - nelf BufferedIN,e(,,14th. height.
Bufferedlaage. TTPE.JIlT..AR6Bl;
tNpSurhce teepINoe.crnte6r.phlcs();
super .utlaage(teIlpINge):

Intcurre ntF rillle() I return currfraltt : )


Claid setCurrentFra.( Int (ra.) ( currFralle frallf';

p~bllc
p~bll

public Int fra.1l14thO 1 return fril l 4th: I


public lold setfra.llldth(int "Idth) r frllidth - "Idth:
publiC Int frilleHelght() I return frHelght: I
publlc lold seU. . .Height( tnt height) 1 frHelght -height:
publtc Int totilFra.s() I return totf..lles; I
public lold setTaulFr_s( Int tot.l) I totfra.s toul;
publiC Int inl..t1onOirectlon(} I return ,nllOlr; I
public laid setAniutlonOlrect lon( Int dl r) I anllOl r dlr; I
public Int fra.-(ountO I return frCount: I
public lal4 setF...-(o~nt(tnt COllntl t frtaunt COllnt; I

Sprite Animation
public Int fra~eDelay() I return frOehy; I
public vOid setFra~eDe1aY(Int delay) {frOehy - delay;
public 1nt columns() I return cols. I
public void setColwllns(1nt IIU ~ ) {cols - nu...
pub1Ic vol d updateAn lll~ t Ion(} I
frCount++;
I f (frameCountl) ) fr~IIleOel ~y()}
setFrallleCounHO) ;
Ilupdate the ~nl.~tfon fraille
setCurren tF rame( currentF rame () + anlllM t Ion01 rect Ion ( ) )
if (currentfr~IIleO ) toUlFramesO - 1) I
setCurrentF rame (0)
I
else if (currentFrameO (0) (
setCurrentFrame(to t~ IFraooes() - 1);

public void draw() I


/lc~lcuhte the current fraDe'S X~nd Yposit10n
1nt fralleX (currentFraeO S co IullllS () * fra_eWldth () ;
lnt fralleY (currentFraDe() I colu..,sO} * fraIleHelght();
/lcopy the frallle onto t ne tellp

i~age

O. O. fraIleWldth()-l.
fr n eHelght( )'1. fralleX. fralleY. fUIleX+fraIleWldth().
fraIleY+fr neHel ght(). applet() 1:

te_pSurflce.drawlnge(an\.1~ege.

/lpass the temp I!lage on to the parent chs\ and draw It


super. set Inge(te_pluge) :
super. transforll();
super .draw();

Testing the AnimatedSprite Class


I'm not going to list the complete source code for the AnimationCla;s exa mple
program, because it is essentially the same as the AnimationTest program, except
it uses the new AnlmatedSpr1te class. There is one difference, though. By

13S

136

ChapI~r

7 a

Spril~

Animation

_.
-'.'.

figure 7.6
Testing lht AntalttdSprite daiS.

inheriling from Sprite, the Anl ..tedSprlte class allows yo u 10 rolatc Ihe animated sprileS ...itik Ihq arc drawing! This is duc to thc functionalily provided by
l..geEnt 1t)', a core support d~ for sprite drawing. I will show you the key
portion of lhe program that differs from the AnirrulllionTCSI program (covered
cartier in the chapter). Figure 7.6 shows the output of the pr~m. which you
can open up and run from the CD-ROM if you wish.
Hcre is a list ofall the dasses used in the AnimationClass program. You can copy
theSt: files from the previous chapter. The new projttt is called AnimationClass.

Anl.. ttdSpri te.jeva

Anl.. t ionClns.java

Basee;..Entt t)'.jeva
'..geEnl II)' .Java

PoinllO.Jau

Sprl Ie . java

What You Have LearnlJ'd


Here is the source code for the fnlt() event method in the AnimationOass
program. The TC$t of the program is similar to the AnimationTcst program, so I
won't repeat the entin: code listing hen:. The important thing is that you
undcrsland how to load an animatkm using the An1 .. tedSpr1te class. Go ahead
and open the Animal>OnOass.java file: to sec the complete code listing.
Illp rfte urhbles
Anf ..ttdSprltt' tNll;
publiC yo1d hftO I
IIcrute the back buffer for s..oth grapll!cs
tNckb\lffer - new Bufferedl..ge(SCRHIIVIDTH. SCRE[.I+EI&HT.
Bufferedlaage. UPE....lNT_RGB);
g2d - back buffer . ereateGraph! cs t );
If\Md toe background huge
Too 1~ it tk - Too \kit . getDeflu I tTao 1kIt() ;
b,,~ground - t~.getl ..ge(getURLt .oadgra!n.png);
IIload the ball an1..t10n strip
ball. new Anl..tedSpritelthis. g2d);
tN 11. lOld( xb.l l. OIIg". 8. 8. 64. 64);
tNll.ntPoslt10l1(new Poht20(300.200);
tNll.SltFr.lIeOch,(l);
tNll.SltYelocit,("", Potnt20<l.1)I:
tNi l.sttil(ltat1onlllte(1.O);

What You Have learned


This chapter tackled the difYkult subject of sprite animation. Adding support for
animation is not an (';ISy undertaking, but this chapter provided you with the
knowledge and a new class called Ant_.tedSprite that will make it possible for
you to write your own games without reinven ti ng the wheel every time you need
to load an image and draw it. Here are the key topics you learned;
How an animation is stored in a bitmap fi le
How to load and draw an animation strip from a single bitmap file
How to animate a sprite with timing
How to put il :all togdhcr into a reusable cWs

137

138

Chapter 7 Sprite Animation

Review Questions
The following questions will hdp you to detnmine how well you h<a~ lc-amed
th~ subjts discIWe'd in Ibis chaptet.".
I. Wh<al is th~ n.aJ1ll' ofth~ animation class created in Ihis chapl~r?

2. From which class docs Ihe new animation class inheri!?

3. How many frames of animalion were Ihere in Ihe animated ball sprile!
4. \\Ihm do you call an animation thaI is stored inside many files?

5. What do you call an animation thaI is all stored in a single fil~!


6. Whal type of parameter docs Ihe MI.. tedSprl U. utVelocHy method
accept?
7. What arithmdic opcntion is used to calculale an animation fntJ1ll"S Y
position?
8. Whal arithmetic opmtlion is used to calcuLale an animation
position!

fram~'s X

9. What is a good class to use when you need 10 creale a bitmap in memory!
10. Which Anl.atedSprl te method dr.lws Ihe cu rrent frame of animation!

On Your Own
l1lC: following exncises will hdp you 10 det.ennine how well you h<a~ undn'Aood
lhe new material introduced in this chaptet.".

Exercise 1
The AnimalionTesl program is really LaJ1ll'. I ml'3n, whit.. balls paslw wilh a
red X, being drawn ov..r a strange W<Xld -grain background! Locale a really
great image Ihal you can use for the background, and loo k through Ihe
\Media folder on the CD-ROM to find a better animat ion sequence to usc in
place of the whit.. balls.

On Your Own

Exercise 2
Now that you Cln do fuU-biown animllkm. it's timt 10 rombint thai awtSOmt
Clp,ibility wilh timt-provm rollisian dC1td.ian in ordtr 10 add SOmt aaual
functionality to tIM: AnimationTm pmjtct_ Modify 11M: program and Clust tht
animalro sprite 10 dt$troy each othtr whtn thq rollidt.
ntW

139

CHAPTER

KEYBOARD AND MOUSE INPUT

1lJe ktyboard and mo~ are tM only rWistic devices for usn input in a wdr
based game developed as a Java appkt. But even when oonsidcring a standard
Windows-based game developed in DirKtX or another library, the keyboard and
mouse are by fu r the most oommon forms of user interaction in a game. This
chapter covers the important subject of handling user input. lll.'rc arc the key
topics you will learn in this chapter:
listening for keyboard events
Testing keyboard input
Displaying ke'y presses
Reading mouse motion
IXtecting mo~ bUllons
Testing moux input

listening to the User


Java provides an interesting way to intemct with useN through a series of lisreller
methods. You tell Java that you would like to listen to keyboard input events, and
then Java sends keyboard events to your own listener methods, at which point
you can check tM key codes to figure out which keys have bn pressal. or
released. TI'C W1Iy lava tdls your prognm that a key has bn pressed {or that the

'"

142

Chapter 8 Keyboard and Mouse Inpu t


mouse has moved) is through an interface that your program uses-or rather,
implements. Your program musl use the l..plemen t s keyword to use an interf.1ce
class. This is a type of class that just includes methods your program needs to usc
(or implement); the class doesn't really have any funct ionality on its OWTl, This
type of class is called an illlerfirce becausc it rqm'scllls a blueprint of Ihe methods
your program must usc.

Keyboard Input
Th, KeyL1 stener intl.'rface Iis\('ns for l.'Vl.'llIS gl.'llcrated by the keyboard and sends
those events to the callback methods implemented ill your progr;lI\l, Th~ methods
are called keyPressed, keyRel eased, and keyTyped, and these thn"C ml.'thods all haw
a singlc parametercall&l KeyE~ent. When wriling a program to uS!' the Keyll stener,
you modify the class definition of your prognml Il.ing the l..plelllents keyword:
public class KeyboardTest extends Applet 111l11e.ents KeyLlstener
Tip
The interesting thing about the Implements feature of lava dasses i,!hal you

(3/\ implMlenl
multiple interlaces in yoor program by sepa<ating the interlace dal5 names wil!l rommas.

You may rC'C31l seeing thl.' l oplements kl.'yword used before wit h the Runnab1e
il\lerface (which added threading support). When you need 10 add more than
onl.' in\l'rfacl.' class, you can separate Ihem with commas.

listening for Keyboard Events


Your program Ill't'dS to then all the addKeyL1stener mcthod to initiali].e the keyboard listen~'T SO that key evcnt5 will be M'nt to your program by lhe )av-J Runtillle
Environment. 11,<: soil.' 1"1I1Ulletl.'r of this llle1hod is Ihe itlSWlCe of YOllr program's
class. represented by the I,..yword this. You use thl s as 3 ""y to identif)thc current
class in a block of code wilholll rderrinillo that class spifially bynamc. II is lbUaUy
best to call addKeyll stenerlthlsl in the Inl t me1hod within your program. (nC'C31J
that lnlt is alltolllat'C"Jlly called when your app1ct-b.tsc<ll'rograrn starts rll ilni ng.)
Next, you must impkment the Ihree keyboard events in your program to sat isfy
the Keyll stener interface:
pUbl1c v(lid ~eyPressed(keyE~ent e)
publlc void keyRelusedlkeyEvent el
publlc vold ~eyTypedlKeyEvent el

Keybwrd Input

Table 8.1 VIrtUal

krt Codes (Partiallistl

KIr)' Code
,~

(IT

'tLRIGII"
p

' ....U

''-'"''

U..O. ' ....9

IV... 'U
Y(.fl .. U..Fll
ItL(P_lFT
I(.~P_RIGHT

v(.xp..up
VUP_OOWN
VtLnrR
~(.8ACtLSPAC(

V(.TAB

.......----.--...............
Description

_\:I!ypodlefl
H.-.ic 'ooypocl riottl
H......nc I:e,pad""
H......nc kfWild dowrl

......
. '" '"
[nm key

Thert' art' two ways to drtenni~ thr kry that has brrn p~ or rdused using
the KtyEnnt paramdrr. If you want 10 ck1:rrminr thr charact.... codrofa key, you
can usr thr gtUtyChu mrthod. which mums a char. If you want 10 know
whrthrr a key has brrn prrssrd t-rd on rnr key codr irutrad of thr charad.....
you an usr thr gttKtyCodt mrthod irullrad. If your program is listrning to
thl' krybo;trd and you press the A kry. thrn geUtyChlT will mum "a" (or "A" if
you arr holding down Shift), while getKeyCode will return a virtual key code
called VK.....A. AJI of the virtual key codes are wntained in a class called KeyEYent.
Table 8.\ shows a partial list of virtual key cooes for the most commonly used
keys for a game.
Not.
WIlen roo want 10 get the 1leys beill\l!ypo'd for "", ;n. chat mes~. lor insl<lnce. \lIeo you wi ll
wanlto 1M the keyTyped eYenllOoflich Il!lInS ASCii charilCtefS. Most of your lJol"",,, input wil l
come from 1he keyPressed h'tnt, which prtMdes tty codes.

Testing Keyboard Input


lLI's wrile a program to Itst krybo;trd input so you will have a compkteaample
of how this works with Ja~ code. I have highlighted 1M important conde in bold.
You can .see rnr output of Ihis program in Figurr 8.1.

143

144

Ch<tpte.8. Keybo<t.d <tnd MOI,/ifIlnput

-.

Koy.- 81

...

ll1port Jav, alll,';


illllOrt JUI Iwt.nent.*:
l~ft jlYI Ipplet.:
p~ol

111

ltytKl,rdl

~t

utend, AW1et

I~l.ents

llQ'Llstetter I

nt ItyCOdt;
char ktyChar;
p~bl

IC YOld In1t!) 1
addleyL1ltener( this) ;

public Yoid p,lnH6r,ph;c 9


g.drlwStrlll91 "Prns I ker ... ". 20. 20);
1.c1nWStrt",("ker code; + te)'Cocle. ZO o 50):
1.c1nWStrhl("ker char; " + te)'Char . ZO o 10):

public voId kerPrusMtlerEvent e) {

Mouse Input
klJ'Cocle e .,etKerCode() ;
klJ'Chr
r tp4lnt{);

IIklJ'llelnsed Is not lIeln, used


pu;blll; wold k'rbhued(kerEwent e' { }
pu;blie wold kt7T)'lItd( rerEwent el {
kerCh,.. e.,cUe}'Ch.r( l;
rep,lntO;

This is the bare-minimum corle you need to provide k~ybo;lrd support to yOllr
Java programs. so you might want to jot down this page number for future
reference (or save the code in a file that you can easily find).

Ti p
A VIItUII Ry o:ode is plttIoo:m-nMrai . . . b Ry. Whfn you wnte <oct. 10 work with
~ W1UtI Ry o:ode (wdllllo Yl;..LErn you (In be ~ 1het lhe Ry wiJ be deIemd on
.., pltllorm (WJncloM,. liu. Mec. SoIW" tnd KI 011).

Tip
The I nl t() IIItlh<xl is 'PfrltI1IItlhod .. til ~ m. you willlOI find ... sttndtloni! JtA
~trn (in 0IIlef words,. tIl~. The In I t () medlod is lhe rnt big m. IIrIS when til
fIlPIel surts up.. n- Ire _a1 other twIllS in til fIlPlet wdllllo p.1 nt( I, m. I will bpltin
llIo ' " 90 along. 5uIl'a it 10 SIJ!he p,lnt(' t'IftIl ~ lhe lIPPeI wn;Iow. K1lhis is ot\efl
whm progIilIlIflItfS wiI write II oIlhe code lor game..

Mouse Input
Tapping into the mouse handler in lava is similar to the process or programming
the keyboard. as you might have suspa;ted. Java handles mouse input using
evenlS that arc generated by the Java Runtime Environment ORE) and passed to
your program when you implement a mouse listener,
Tip
Tht ~ Runt-nr Uh.'..'...... or . . IS Sllbsef 0/1Ile .... ~ l(jt (D::J 1het 1$
~ to iImt you 10 IItw ~ to .. ~ wi 0/ dllIo.ses 1htl you (In l6l! to lUll ......
~

80Ih 1ht . . tnd ft ME

Ire

inducled in ...... V. 6,

Jlr1l"'ided on 1ht ~

145

146

Chapter 8 Keyboard.nd Mouse Input


first step you must taltt to incorporate moU5t t\'mt handling in your
program is to call two functions that will tdl the IRE to begin se:nding your
program mouse: events, Since we'll bt dealing with two interfaces for the mouse,
you must initialize both mouse: handlers, Th is is similar to the function you
learned abou t for initiafuing the ktyboard handler, You put thcK functions in
the "ppltt's Illlt t\'tnt mtthod 50 Ihat thq- art sure to ~ called when tilt
program starts up, You'll rte:al.I from the keyboard KCIion eariier in this chapter
that the this keyword represents the current program: in mort thnicaltCTll'ls,
this represenls the primary obj1 thaI was created base:d on the _aln class
definition in your program,

"fhc,

Tip
No abjKt is nat I d8s; il: is tlw rtSWl 011 dIss. Thri 011 dMs IS I bloepllII: b' I procMt. ifd
., abjKt IS tlw product itself lhal !'IS bftn oonsIrIICIed.

publiC ~old In\tO 1


IddMouselhtfne r (thls) :
Id dllouseHot tonll stener( thIs I:

Reading Mouse Motion


Java provides an interface class for mouse motion and bullon press events that is
similar 10 the keyboard interface, The Housell stener class is an IlbSlrlletciass that
provides your program with an interface, or blueprint, with five methods that
you must impkmmt in your program (rcgilrdless of whether you will U5t all of
them):
public void ..usetllcted(llo"seE~enteJ
public void

-ouseEntered(HouseE ~ent

eJ

publlc void ..useE~ited(lIou5eEventel


public void ..usePruse-d(llousehent eJ
public void -ouseRelused(llouseEvent eJ
The HouseL Istener interface keeps tTack of the mouse: buttons, the mouse:
position in the applet window, and the moU5t loc.ation when the mouse cursor
mo,'cs i"to and 0111 o/the appltt window,

Mouse Input
There is another, completely different interface class for mouse mo~'emem.
You can read the mouse's position during a bullon or enter/leave event with a
HouseListener, but re<:eiving events for actual mOllse morioll on the window
requires another interface. To receive events for the mouse's movement across
the applet window, you must use the HouseHotionL1stener interface. There are
two events in this interface:
public void llO useOraqqed(HouseEvent e)
publie void

mo~seHoved(HouseEvent

e)

Detecting Mouse Buttons


Some of these events report when a mouse bulton is clicked, pressed, or released.
The only methods thm do not deal with the mouse buttons are lIIouseEntered,
mouseExited, and mouseHoved, all of which deal with the mouse's position
and motion, regardless of button status. The remaining events (. ouseCI icked,
llousePressed, mouseRe1 eased, and mouseOraqged) all have to do with the buttons.
As you might have noticed, all of these events have a single parameter called
HouseEvent. This parameter is actually a class, and the IRE fills it with information for each mouse event. You can look inside this class to get the mouse's
position and button values. For the mouse's X and Y position values, you can use
HouseEvent.getX() and HouseEvent.getY(). The parameter is usually defined as
(House[vent e), so in actual practice you would use e .getH) and e.get() to
read the mouse's current position.
Likewise, House[vent tells you which button was pressed. Inside HouseEvent is a
method called getButton() that will equal one of the following values depending
on which bullon is being pressed:
BUTTONl
BUTTON2
BUTTON3

The getButton() method b lIS<:fUJ if you unly care about detecting a single bUllon
press. If, for whatever reason, you need to know when two or three mouse
buttons are being pressed at the same time, you can use a different method in the

147

148

Chapter 8 Keyboard "nd MOU5e Input


HounE.ent dus called geUlodltlers( 1. This (unction will repon multiple e\'mlS
in th~ MouseE.ent dass, such;l$ tm, (ollo..ing;
_

8UTTOMUUS~

6UTTON2_NAS~

8UTTON3.J\ASk

Thtrt art I1Uny mor~ 1ll<lSkC'd ,..... ues (that is. ,,,lues that are bit-packed
into a singl~ ...riabk) in t~ Houn(went class that you can cx:amill(' u.ing tM
ge\.ModltlefS(1 method. But i( all rou care about art tht usual It(t-<!i,k and righl~
dick C"tnts' you can mak~ u~ o( get8utton(:

Testing Mouse Input


I wouMlikt to show rou a program ulled MouseTest that demonstrates all o((~
mouse tvmlS that )'Ou Iu..t just lamed about. To build this program, ~'Ou
should creatt a ntW "'eb applet projt ulkd MouseTest, :;md ther! remo"t all o(
the automatically gencrated cod~ to })(' rq>l.1ced with tht (ollo..ing rod~ listing
inst~ad. This program uses the Graphics dass' d'''wStrlng method alld a bunch
of variables to dispkly the status of all the mouse ~"Ven(s individually. Figure 8.2
shows what the program output looks like. Note the important p.arts of the code
listing in bold.
Tht first part of tht program includes the applet's dass ddinition (with the
needed int~rfaces (ollowing tm, 1..,Ie.ents keyword) and ... riabl~ dfdaratiol1$.
-ousttest:
IlIPOrt JUI.awt.":
l.,mrt JUI.awt.event.",
I_port Ja... applet.":

packl~

publiC cl iSS HouseTut utends Apple!


IlIIll_nts MouseLhtlMr. MousiYlDtlonltstener I
l6echre s.-e a:l~se e,ent v..hotes
lnt el1cb'.. cllck:f:
lnt presu. pren:!,:
lnt relusu. releuey:
Int enurx. entery:

Mouse Input

:::=";:~m
_ _ II:lllO,301
_lftOOOd
_ _ 1I11:'1.156
II: J ICI,.Z)l
_ < t__

II:'I~

_ _ 4l161.Z!17

lftt ultJ'. exltJ';

tnt drlgJ'. drlQJ';


lnt ... vex. -ov.,;
lnt llOustbutton;

The lnl t( 1 ('Vent method is the first method that gelS run in an applel. So .his is
where)'Ou would initialize )'Our game objectsand variables, and this isalso where
)'Ou add !he listeners lOr any input devicf$ 1M prog.ram ~ to ~. If your
program f:er seems to be ignoring the ~rd or moUK, check 1nIt( 1 10 make
sure you have added the appropriate listener.
III.HIlllle the applet
pUbl1c voId lnHO I

IddMoueLlstener(th1s) ;
IcldJIoIIselkltl onL1stener ( UIs):

The pllntO

~t method is CotIled whrnevCT the appkt needs to refresh the


window. Since pllnt() comes with a parametCT ( Grlphics gl, we can usc this

149

ISO

Chapter 8

Keyboard and Mouw Input

Objecl 10 draw onto the screen. 'Ibis program uses the Graphics dr!wStrlnli()
melhod 10 display texl on lhe applet window.
IIrednw t~e applet wlr>dow
wotd Pllnt(GTlp~ics g) t
9.drnStrtn'l( "House clicked " -ousebutton .. . at "
c)tckx. ". .clicky. 10. 10};
9 d.. "Strtng("Ilo~se entered It ... enten. ". ..
enter)'. 10. 251;
g.drlwStrtl\9( "House exited It " .. exlh .. "." . exit)',
10 . 401;
g.drawStrtng(IIO~le pressed". -ousebutton at
pressx. ". press)'. 10. 551;
g.drlwStrtng( "House relelSed " -ousebutton" " at ...
relelSu" ".... relelSe)', 10. 70);
g.d.."Strtng("House dragged at ... d..gx . ". .. drag)'.
10. 8S);
g.dra"String( House -owed at . .. -owex .. . ... -owe)'.
10.1001;

p~b1ic

The next portion of code includes the checkButton() method, which I have
wriuen 10 support lhe mouse event handler in lhe program. This c~eckButton()
method chcclu lhe current button that is being pressed and KlS a variable
(-o~sebutton) to a V;t.1ue representing the pressed button.
IlcustOll .ethod called b)' -ouse ewents to report but ton status
private wotd checkButton(MouseEvent e) (
lichee! the -ouse buttons
swttch(e. get8uttOllO) {
Clse HouseEwent.BUnOMI:
-ousebutton -I;
bruk;
Clse lIouseEwent. BUTTON2:
..unbutton - 2:
bruk;
CI se lIouseEwent. BUTTON3:
lIOusebutton - 3;
brUk;
default:
..llSebuttool - 0:

Moose Input
The -ouseCl icked() event is part of the ~ouseLtstener interface. When you
implement this inlerface, you must include all of the mouse events defined in
the interface. or the compiler will generate some errors about the missing
events. This evenl is called whelK'VeT you click the mouse bUllon on the applel
window-in which case both a press and rdease ha.s OCCUfTed. This event is nol
usually needed when you program -ousePreued() and -ouseRelused()
yoursel f.
publ ie yoid 8OusC'Cllcked(lIousehent el {
IIsne the 80ua position y'luu
el1eu. - e.getI() :
el1ekJ- e .geU():
Ilget ,n updlte on buttons

e!IKkButtOll(e):
IIrefrUh the

5cr~n

(call the paint eyent)

repli ntO:
The ncxl two mous.e event methods, mIlseEnteredO and 8lluseJ.ltedO. an"
called whenever the mouse (Ul'$Or f'rlteTS or leaves the apple! window. Th~
events arc nol often needed in a game.
public votd

8Ou5eEntered(~euseEYent

e) (

- e.geUO:
entery - e.getnl:
relNl1ntO:
enter~

I
publ ic Yeld lIIOuseE~lted(~euseE.enl e) I
ulh-e.gelX(l;
ulty-e.9ttY(l;
reINl1nt/):
The lIOusePressed() and lIlQuseRelused(} evenl methods are called whenever
you cl ick and release the mouse bulton, respectively. When these (vents occur.
you can get the CUlTf'rlt position of tile mouse a.s well a.s the bullon being prascd
or released.
publ1c Yeid 8OusePrused(lIousehent el I
pruu-e.geU();
pressy - e.getYO:

151

152

Chapter 8

Keyboard and Mouse Input

ehtekButton(e):
replht(}:
I
pybllt vOId llOuseRelused(llouseE.ent el I
rellun - e.geU( I:
relnuj' -1.geUO:
clllckBulton(e) :
rlpahtO:

1hcl'lousdiot lonllstener inlerf.1a defines the next lwocvcnes-useClugged()


and lIOuseKoved( l. 'These ...mtsan helpful when you just want to know when the
mouse: is moving o"cr the apple! window (and when it is moving while the
bUllon is being MId dmo.n).
public void llOuuClriqqedlMotlseE.ent el I
drag.- e.geU(}:
dragy-e.getY():
repaht(}:
I
p~b 11 e vol d llOuseHored(ll(lu~eE.e"t e I I
llO.u-e .geU():
IlOvey- .geU();
repalntO:

What You Have learned


This ehapterexplaincd how to lap inlO the keyboard and mouse listeners in order
to add user input 10 )"Our Java programs.
You lC'arncd how 10 detect key prascs.

You learned about kcy cod~ and character values.


You lC'arned how 10 mid the mouse's motion and buttons.

On Your Own

Review Questions
The following questions will help you to doetermine how wt'll you have learnM
the subje.;ts d iscussed in this chapter. The answers are provided in Appendix A,
"Chapler Q uit Answers."
I. What is the name of the method us.ed to enable keyboard events in your
prognm~

2. What is the name of the keyboard event interbee?


3. What is the virtual key code for the Enter

key~

4. Wh ich keyboard event willldl yOIl Ihe code of a pressed

key~

5. Which ke}-board event wiD tdl you when a key has been

rdeased~

6. Which keyboard event will tell you the dW'2CtCl" of a p~ key?


7. Which XeyEyent method returns a key code value?
8. Whal is the name of the method used to enable mouse motion

events~

9. Whal is the name of the d;lS$ used as a parameter for all mousc event
methods~

10. Which mouse event reporu the actual movement of the mouse?

On Your Own
Use the following exercises to test your grasp of the material covered in this
chapter. Arc you rearlylO put mouse and keyboard input to the test in a real game
yd~ These exercises will dWlenge your understanding of this chapter.

Exercise 1
Modify the KeyboardTcst program so that pressing numeric keys I to 9 will
change the font site used to display the key code and character values. To do this,

IS3

154

Ch3pter 8

Keybo3rd lind Mou~ Input

U5e the Gr.phlcs class in the plint event, which has a method called sttFont that
you can impltmmt like thi$:
g. $etFont( new Font( Ar leI'. Font. NOJlllAl. iiI ue II ,
I will gi\'e you a hint: The ktyoode for I i$49, so you can subtrac! 40 from tht" k~
coot" to arrive at a good font site.

Exercise 2
Mooify tht" MOUKTest program so thaI a point is drawn whenever the user
presK$ a lllOU5e button. You can U5e tht" Grlphics e!au's ftll Reet Jm1.hod and tht"
lllOU5e position ",n..bles. (Just draw a rtangle ..ith four oomtl'S that au one
pixel aran.) If you au {ming confidml with your llC'W }a", programming skills.
try using the setColor method to change the oolor of the points.

CHAPTER

SaUNa EFFECTS AND MUSIC

java has a rich sct of features for recording, mixing, and pbying sound sampk-s
and MIDI sequences using a variety of classes that you will learn about in this
chapter. You will learn about Java's rich set of sound support classes for loading
and playing audio files in a variety of formats through Java's sound mixer. You
will then learn about MIDI files and how to load and play them through Java's
MIDI sequencer. Here is a rundown of the key topics in this chapter:
Loading and playing digital files
Loading and playing MIDI files
Writing some reusable audio classes

Playing Digital Sample Files


Java's Sound API provides a package for working Wilh digital sample files,
which has methods for loading a sample file (AIFF, AU, or WAY) and playing
it through the sound mixer. The package is called javax.sound.sampled and
includes numerous classes, most of which we will ignore. Some of these classes
provide support for recording sound and manipulating samples, so you could
write a complete sound editing program in Java that is similar to full-blown
sound editing programs. One good ex-lmple is Audacity-a freeware, open-source

,ss

156

Chapter 9 Sound EHe<:1S and Music

..
,
..._----_

,..,.-=
.... .-

. ..

'.".:
- -, ' ~- - ~

I:

Figure 9.1
Audacity is ~n ex<ellem freeware ""'00 editi",! program with many advarlCed features.

sound editor that is available for download at http://audacity.sourceforge.net


(see Figure 9.1).
The Java Sound API supports the three main audio file formats used in web and
desktop applications:
AIFF

AU
WAY

The digital sample files can be 8-bit or 16-bit, with sample rates from 8 kH:t to
48 kHz (which is CD quality). Java's Sound API includes a software sound mixer
that supports up to 64 channels for both sound effeds and background music for
a Java applet.
Tip
for tile latest information about the Java SOOOO API, poinl\'O"r web br0WS8 10 java.sun.com!

produttsljavamedW"",OO.

Playing Oigital Sample Files

Getting Started with Java Sound


The fim step to writing SOInt Java sound code is to include Ihe javu..sound.
sampkd package:1I !he 101' of your progr:am:
lllpOrt Jun. ~ound.$",pltd.:
If you are using JBuilder, you will see a pop-up Intnu to ~Ip you narrow down
the claM names wilhin J' .n, which can be very eduCltional. You 'U see Wt when
you type in lllpOrt jun.$ound . JBuilder will show you the two c~ availabk
in jun,sound., which are u.pled and .ld1. By adding ," to the end of the
import statement, you are telling Ihe Java compiler to import every clas.s within
javax.sound.sampled, of which there are many.
In fact, when working with the sound system, you will need access 10 severol
classes, so it is convenient (0 imporl the associated packages at the start of a
progrorn so those classes are easier to use. For instance, without importing
javax.sound.sampled. you would need 10 create a new sound sample variable
using Ihe full class path, such as:
JUU.SOtond. ullPltd .A.udlol~put5tru.slllllle
JUU. sound. slllp 1td .AlIClloSy5tM.')etA.ud 1ol~llUtStru.t ~ew Fi lel "_hoo.

II"');

Could you imagine: what it would look like if you h<Jd 10 write aIlof your code like
this? It would be illegible for I~ ITlOSI pan. Here is wtull~ code looks like after
you have imported javaLSOund.sampltd ',
AudfOl~ll\It5trei.sillllle Aud lo51~tel.getAudlol~
putStrU.(lIew

Filet _hoo.

1I. . " l} :

AudloSy5tM and Audlolnl)lltStre,. are classes within the javax.sound.sampled


package and are used to load and playa sample in your Java applet. Later in this
chapter, when I show you how to do background music, you'll gCl the hang of
using some classes in a pack.1ge called javax.sound.midi.
Cll u tlo n
You may run into a problem w;1h Iht audio portion of your g.l<TIoe, where your 'iOUrre code SHtI'lS
wriltl'f1, without bugs. bill you ~till get IlllUSUaI et'I'ln One 01 Iht most common
SOlnK 01 problems when worIcw.g W11h aucio elm is an ~ lie IonnM tm:lf. ~ tnle
01 oaplIOn is c*d UnsupponedAudloFl1 eXceptlon IIld will be ~ later in !his

10 be well

If the program's flow runs through the UnsupporadAudloFll ~[xc~ptlo" block


in your error handler. Ihen the audio file may be encoded ...ith an Ul1$upported

157

158

Chapter 9 SOund Effects and Musk

r_._
_
---...
,,--_._1._-

_~I_

"'-1-1- _1_1_1
3

f---r---'
~ .

Figure 9.2
~

!he IligiqII

ionrIIt settwlgS WI Auo:Iicity.

file format. The other, more obvious, problt:m is truu the lile it~lf might ~
mis.sing.
You em dlt<:kand oonvtrl audio fiks using the fn.tw;tre Audacity program that I
mentioned earlier. Just load up an audio filt !lut you suspt<:t is mc:oded in a
weird format, and then savr the lilt to a new format. Figure 9.2 shows the File
Formats tab in the Audacity ~ diaJog box. Here you can dwJgt the dt6.u1t
lilt fornut for aponing audio lilrs from the Filt: mtnu. If you choo5l:' the Other
option from the drop-down list, you will ~ presc:nted with evell more audio
formats, but most of them are obsolete. ( For instance, you can save to Creative
l.abs' old VOC format, which was popular in MS DOS games many years ago.)
Some of the custom formats require an additional download of a plug-in for that
panicu lar sound forma\.
The key to sound prog~mming is a class called .I.udlolnputStrea.. This class is
used to load a sound lile (which can be an AIFF, AU, or WAY lile) from either a
local file on the current ~ ~n~, ....' htre the applC1 is located, or from a remote
URi U1ywbere on the InttmC1. An input stl'Cillm is a source of data. You can
cl'Cillte a new iruilanCt' of the cbss like so:
.I.udlolnputStru. UlllP1t:

Playing Digital Sample Files


This statement is usually spedfled as a global variable within the class, defined up
at the top of the class before any methods. You can define this variable as private,
public, or prote<:ted. (The default, if you do not specify it, is public.) In objectoriented terms, pllblic specifies that the variable is visible to other classes outside
the current class, privtl/e means the variable is hidden to the outside world, and
prO/ccred is similar to private, except that subclasses (through inheritance) have
access to otherwise hidden variables define<! as protecte<!.
The code to load a sound from a file or URL is usually calle<! from an applet's Inlt
method. The method used to load a sound is AudioSystem.getAudlolnputStrealll.
This method accepts a File, InputStream, or URL object; in addition, there are two
other ways to create an audio stream (Audi oForllat and Encoding), neither of which
is useful for our needs.
s~mp

1e - Audi oSn tell. getAud i 0I"putSt re~lI ( new Fil e( numbug. wav" )) ;

Note that the return value of this method is an AudiolnputStrealll. Also, since
getAudiolnputStrNfII does not offer an overloaded version that just accepts a
String for a filename, you must pass a Fi 1e object to it instead. This is easy enough
using a ne... File object, passing the filename to the File's constructor method. If
you want to grab a file from a URi., your code might look something like this;
URl url new un ( "http; I /W>N. j ha rbour . comItes t. wa v' ) ;
s~ ..pIe - Aud ioSys tem. getAudi 0 lnputStrealll( ur 1 ) ;
Either way, you thell have access to a sound file that will be loaded when needed.
However, you can't just use an AudiolnputStream to playa sound; as the class
name implies, this is just a source ofsample data without the ability to play itself.
To playa sample, you use another class called CI i P (ja~ax.sound. sampl cd .CI jp).
This class is the return value of an AUdl oSystem method calk-d getCI ip:
Cl i P cl i P - AudioSyste... getCl lp();
lOild ing Resources
The code "",-"",nte<! he> will load a sound file correct!y when your Javil program is running either
00 y<IU( 10<iII PC Of in iI web bro\vse(, floweve<, we nCO'd to u..... a slightly different method to load
~ file oot of a Java archive This wbjecr is oovered in Chapter 16. ' Ga lactic Wil'; web Deoploy.
ment ' which cOYm web ~oymeot
But I want to prepare you IOf distributing your Java programs 00 the web IIOW; 50 thilt your
p>gJilms will afrl'adr be ready tOf <ieploymeot. To that end, you mUsT repla<e the ne... File ( )
and new URL ( ) meltlods to load a resource (sudl as iII1 imiIQC Of sound file) wiltl the following
(Ode instead; th is .getCla ss( ) .getResourceO. The getResource( ) method is tound
in 1Ile CUll'e!1t class instionce. this.getClass(). Yoo will rrnd it most u.....ful if you use

1S9

160

Chapter 9 SOund Effects and Music


th t s .getC Iusl J getRnourcel J MIJ'IIlIlf .,.,., need 10 builcll UllL Kfle is I method
~ ~ goo!:

roe

...,.;., INI

prtnte URL getURLIString fllen ) t


URLurl-ftull;
try (
urI - tht s.getChss( l.getRuource( fl1 eftl.);
I
catch (E~ceptton e) l )
return urI;

1'Ilfn. ~ .,.,.,gellO ChIptfr 16, !tIl'flnllII'IIl'6.,.,.,'ft_will belNdr lao MIl ~


in I ~ avl ~ (JARJI ~ '1'N' eoq:blOCflS ol1he.lloa IinguIge
'MIIWlg
g;wnes MIl 0Ih0r fI'I9M\l, .,.,., wi! ~ COIIII! ' " WIlh II'IIfly usri.J _ _ sum ;os
getURL ( ). You l'IIIJ WIIl1 10 store tIwm in IIfIlSII* pICbge ol 'fOUl 0WIl clesignIlIon.. The rooI
pICbge ~ be CIhd jharbour. IOd \hen I wooJd Idcl ~ 10 lI"Os, sum IS
jhlrbour.graphtcs. Jharbour.utll. MIl SoD OIl Since getURLO is !til' ooIy custom
retIlabIe method repeaIe(Iy used .. 11M! book. h is more COll\Itflienl 10 jllSl include h .. ~ das~

""*

Since we don't need. to pass a par:am\"ler to getCI t p, you might be wondering how
this objt knows whal to play. Thne:'s aetuaUy one: mor\" step involvtd becaU5C
al this point, aU you have: is a sound dip objt wilh the: cllJHIbility to Iood and
play an audio file: or S1r1"am. This method actually rdurns a sound dip objt
from the: default s~lc:'m milCT.

Loading

th~

Sound Clip

At this point, you have an AudtolnputStre and a Cll p, so you just need to open
the audio file and play it. These steps;\rC bo th performed by Ihe CI 1p class. First,
let's open the sound file;
cllp.open( ullple);

Pfilying the SOund Oip


Next, there arc: two "'Irs to playa dip, using the Clip class. You can usc: the
start() method or th\" loopC) method to playa sample. The stirtC) method
simply plays the sound dip.
narrator .stnt<);

Playing Dig ital Sample

Fil~

On the other hand, the loop method provides an option that lets you spedfy how
many times the dip will repeat, either with a specific number of repeats or
continuously. Here is how you might playa dip Drre rime using the loop method;
exploston. l oop<Ol;
Rememher, the pammeter specifies the number of times it will replay, as it's a
given that the dip will always play at least once IIsing the loop method. Here's
how you can playa dip continuously:
thrus lers . loo p(C I ; p. LOOP_CONT [HUOUSL Y) ;
You might use this option if you have a music track that you would like to play
repeatedly for the soundtrack of the gmne. Keep in mind, though, that sample
files (AIFF, AU, and W AV) are quite large, so you wouldn't want the userto wait
five minutes or longer (especially on dial-up) while the sound file is downloaded
by your apple!. This happens when you call the open() method, so if you try to
open a huge sound file it will force the user to sit there and wait for an indeterminate length of time while the dip downloads. This is why I recommend
using a MIDI sequence rather than a digital soundtrack for your game's background music.
Tip
MIDt i51he acronym for MusKallnSlruflK'nf Digirallnterfau. MIDI i5 a synt~i,ed music forma~
not a Sdnljlled fOlTl1a~ meaning that MtDI musk was not recorded using an aoalogto-digital
oonverte< (whkh i5 built into your computer's SOIJrldcard). Professional musical instrumems use
the MIDI format to record ootes rather than samples.
You may feci free to use the ClIp class' starl() method to playa sound dip, but I
recommend using 10op(O) instead. This type of call will give you the same result,
and it will be easy to modify the method call if you ~'Ver want to repeat a sound
dip once or Sl.'Vcral times. For instance, you might use this technique to save
some bandwidth. Instead of downloading a two-second explosion sound crfect,
go for a one-half-second clip, and then repeat it four times. Always keep your
mind open to different ways to accomplish a task, and look for ways to optimize
your game.
Tip
As you willle.lm in Cl1ap\e( 16. the Ja~a Rumime Environment (IRE) provides an attraetM! applet

download ween with a progress bar when you use a


of its medla files.

Ja~a

arm". (JAR) to store the apple! and all

161

162

Chapter 9 Sound Effects and Musk

Stopping the Sound Oip


Most of the time you will simp ly forget about a sound clip after it has started
playing. After all, how often do you nl'l'd to stop a sound dTl'Ct from playing
when then:'s a sound mixer taking care of all the details for you? Seldom, if ever.
HOwe\'er, if you do netd to stop a dip during pbyb;tck. you an use the stop( l
method. I SU5pect: the only time you will need this method is whm you aR'
looping a sample.
kaboo. stop():
Handling EfTOt'S

One interesting upect of the sound cb= is that they reqUiR that n-ror5 be
a ught. The rompi ler will refu50e to build a program using some of the sound
classes without appropriate try. catch error.handling blocks. Since this is a new
concept, ['lJ quickly explain it.
Java errors are handled with a spial error-handling feature called a try..(;Itch
block. This featun: w;I$ simply borro.... ed from the C++ bnguage. on which Java
w;I$ based. Here is the basic syntax of a try..cnch block.:
try (
I

lido sOll('thing bad


catch (Except ton el C

I
\hen you add error handling to )"Our program. you an: "wrapping" an error
halldkr around your cod e by literall y wrapping a try. ,catch block around a
section of code that you need to track for errors. The Java sound classes require
try. ,cltch blocks with specific ~ of error checks. The generic [_cepllon class
is us.ed to catch most errors that ace not aught by a more specific type: of error
handler. You can ha''(' llIany catch blocks in you r n-ror handln-, from the more
specific down to Ihe llIore generic in nature.
Ti p
In some casH, I try .catch _
hardtr i ~!II tade -:eptian _
INt a
part;cuI rrleIhod Ilwoows (on pwposeI. In d\ose C3I5oI!:I" yow PJ09'3IIl _
~ !hi

lIJIllIlIIlfi* em.- hardtr (sudl;jl$ IOExcept ton).


Another available version of th e error handler is called try ...catch ..final1y. This
type of error-handling block allows you to put code inside Ihe finilly sect ion in
o rdn- to perform any cleanup or dosing of files. The code in a IInllly block will

P1-ring Digital sample FilM

be run rtganflnsof wheth~r an CTTOr occurred. II gelS ex~uted if there "r(~rrors


nnd if lh~re are

110

errors.

For instance, if you are load ing a file, you will firSl check for an IOElcept 1on
bdou providing a gnxric Exceptton handlcl". The A~dioS,ste-, A~dtolnp~t
Stru.. and Clip cbsses requiu the following CTTOr haodkn:

IOE~ception

Lt neUnava 11 ab 1eExcept ton

UnS~PJlOrtedA~dl

oft 1cExcept ton

Let me show you how to implement;In CTTOr handln for

audio code you're

about to write for lhe PLaySouod program. The following cooe is found in the
Applet. tni t( I evenl:
publ Ie ~old tnH() 1
tr, I
Ifsource code lines cl ipped
I CI tch (Ill I forwedURlExcept Ion ell
I cltch (IOEaceptlon e) I
I cltch (L jneUnOallibleE~ceptlonel I
I ca tch (UnsupportedAud IoFl1 cExcept Ion c) I
I
111 be the first pfSOll to admit that this is some ugly COOl.'. Error handling is
notoriously ugly ~ust it adds all kinds of unplrasant-looking mal1;Ig~ment
melhoosand f'Venls around your beautifullywriucn sou rce COOl.'. However, CTTOr
handling is necessary and prevenlS your program from crashing and burning. I
like to think of aIry ...cltch block as a rev limiter thaI prevents n cnr engine from
blowing itsdf up when a foolish driver hilS lhe accel~rator too hard.

Wrapping Sound Clips


Since error ha nd ling is a necessary evil, it supports the nrglllllent lhat you mny
walll 10 pilI some oft-used code inlO reusable methods of your own. A couple of
methods 10 load and playa sound ftle would be useful (and thaI ~rror-handl ing
code could be bottled up out of sight). It would be logiaJ to Cf\<3j>5Ulue the
AudlolnputStru. and Clip objccU inlo a new cbss of your own deign with your

163

164

Chapter 9

Sound Effects and Music

. - 1'1'..,-

__

-_.

I !I-'" Ik _ _ _
I

PCN~22ll5OD"'l6l11,_1~

__

I - . . , - 22ll5O
__

.I'Ol~

_ _ '6-bl

"'-_:

"'9u '.9.)
The !'IIySc:uld prt9_

cIfmonsIr~ how

to lDl ind

P"iII' <I soo:n:I tip.

own melh0d5 10 load and pL.\" .. sound file or URI..I...tcr in Ihis dupter)'OU '.,,11
find source code for a class nlln! SoIl~dCllp thu does just that.

Playing Sounds
lbe Java sound classes are not quill.' a "turnkey" programming solution, because
)'011 muSI perform several sleps to load and playa sou nd file. I think it wo uld be
convenient to wrile a c1as~ lhat has a collection of sound clips you can load and
pby at any lime from thaI single class. bUI I hesit ate to "wrap" any la\'" code
inside another class when il is such a heavily ob;ect-oricnk'd language in the first
place. let's just wrile an example program to Set' how to put all th is code to work.
Tht resulting program, called 1>l.a)"Sound, is shown in Figure 9.3. The rt"IC"oanl
code 10 Ihis chapter is highlighled in bold
IIIJI'O't java.awt. :

'.'t jJYa.applH. :
.'tjua.lo. :

Playing Digital Sample Files

i mport j a v ~. net .';


I. port juu.sound.sa.pled. *;
public c l ~ss PlaySound extends Apple t 1
St r l ng f 11ename - "gong .wav " ;
Audlolnp~tStrell sllpl e;
pr1vate UU ge tUR L(Strl ng fi1en~me) {
URLur l -null :
try I
uri this getC1ass(} getResource(f ll ename) :
I
c ~ teh (Exceptio n e) I I
return uri :

I
/finltlal1le th eapplet
pub lic void in1t() 1
t ry I
sa.p Ie _ Audi OSyst N .geUudl olnputSt ru.( getURL( fi 1enae) ) ;
lIerute ~ sound buffer
Cl1p el ip - AudioSyst N .getCI ip();
fl1o~d t he audio flle
cl ip.open(ulIPle);
flphy the sound clip
clip.start();
I catc h (M~ I forRledURLXcept i on el
I catc h (toXceptlo n e) 1
t catc h (l i neUnava il ableXception e) I
t ca tch lU nsupp or tedAudi of 11 eExcept I on e) t
t catch (Exce ption e) ( t

lithe pa i nt event ha ndl es the screen re f resh


pubUc void p ain t (Gr ~ phlcs g) {
i nt y - 1;
g .drlwString('Sampl e file; "+ f ilenalH! . 10. 1S' Y++);
g. drawStri ng( '+ S~lIp Ie. getF orm~t( ) . toS tr i ng ( ). 10 . 15' Y++};
g .drawStrlng( ' S~ lI pl in g rate ; +
(in t )s~", p\ e . getForm~t() .getS~ mpl eRate(). 10 . IS ' y++};
dr~wString{ '
S~lIple cha nnels , ' +
sallpl e.getForllat().getChannels(). 10. lS ' y++) ;
dr~ ..St rjng('
Encode d forllat, " +
sam ple .getForllat ( 1.getEncodi ng( ) . toStr i ng( ) . 10 . 1Sy++ ) ;

16S

166

Chapter 9 Sound Effem and Musk


9.drawStrlll9( " Sallple slzt: " ~
slllple.9etForaat().getSalipleSlzelnBIU() + 'blt'. 10. 15')'++);
9.drawStrIll9(" Fra.. Silt: " +
sallplt.guForaat().getfra..Slzell. 10. )5')'++);

Playing MIDI Sequence Files


Although using MIDI is not as popular as il USl'd to IJ.c for background soundlracks in gamcs, you ha,c an OppOTlunily 10 savc a 101 of b.andwidlh by using
MIDI files for oockground music in a web-based gamc delivered as a Java applet.
On the web. bandwidth is crudal, since a gamc Ihat lakes 10(l long 10 load may
cause a potential playeT 10 abandon the game and go elsewhere. For this reason. I
would liU to rommend the use or MIDI for in-game music when ddivering a
game through the web. java supports Ihrl.'(' types of MIDI formats;

MIDI Type I
MIDI Type 2
Rich Music Formal (RMF)

loading a MIDI File


Loading a MIDI file in Java is just slightly more involved t han loading a digital
sample file because a MIDI file is played Ihrough a Kquencer rather than ~ing
played directly by the audio mixer. The Sequence class is used 10 load a M IDI file:
sequence S0fl9 - IIldISystl.'ll.gets.equencelnev FlIt! '.,slc .ld ' l):
A11hough this code doG pnparr a MIDI file to be played through the sequmeer,
.....e haven'l ;\dually created an instance orlhe Kqumcer yet, so let's do thai now:
sequeneer seq - 1I1dISystl'll.getSequencer! 1:
NOll.' that the Sequencer class can ~ accessed through MldlSySleti dirrctly. but it
requires less typing in of code to creale a local variable 10 handle the sclup oflhc
MIDI se<Jucncer. Nexl, let's tell Ihe Sequencer class lhal we have a MIDI file
available (via the Sequence class):
seq. setSequence( song J :

Playing MIDI Sequen<e FiltS


This lint of codt establishes a link betWffn tht sequtncer and this panicular
MIDI sequence file. Now all thaI R'mains 10 do is actually open the file and
play it:
uq.Ol>tnO:
uq.$UrtO;

At lhis poinl, 1M MIDI sequence.should Slart playing when the apple! 'Hindow

comes up.

Playing Music
The following program listing demonstrales how to load and playa MIDI file in a
Java apple! window. The PlayMusic prognlm is shown in Figure 9.4. As you can
sec, there are some minor details about the MIDI file thaI are displayed in the
applet window, which is basically just an easy way to determine that the MIDI file
has bttn loaded correctly. The key portions of code are highlighled in bold.

__
__

Mif~'

_M

'w

,,--=1

_,0

F"'5Iu", 9..
1llt ~ pnqim d<ornonslrMK how III kwd ond pi.,- a MIll

167

168

Chapter 9 Sound Effects and


iI'lIort
l"POrt
l.rt
l.rt
illPOrt

Mu~k

jl......t.;
jln. 'PIll et. ;
jln. Io. ;

jl".net.;
jnu.sound ldi. *;

public class Playlluslc utends Applell


5tr1 ng ftl enUN!
l tt Ie.us Ic .1d" ;
Sequence song;
W

prhlle URl getUlL(Slrlng ftlenl.) I


UIlL uri- null;
try I
urI - thls.getCllssl) .getResourcelf11eol.);
)

cltch lhceptloo e) r I
returo url;

{{loHI II he the Ipplet


public wold lolt() l
try I
soog - MldiSystgetSequeoce(getUIlL(11 Ifill.));
Seqllfllcer sequencer Midi Systfll. getSeq\lf1lcer() ;
sequencer. utSfquence(songl;
sequencer. open( I;
seqllencer. stl rt/ ) ;
1 Cltch (Inwal IdllldIO~t,E~ceptloo el I
1 eatch (1I1dtunlv~ilableE~ceptiooell
I cUch (IOhceptlon el I I

{{repllnt tile IPplet .. Irtdov


""bile void palnt/Grlphics,) I
lot ~-IO. y-I;
if (soog !- oull) I
q.dr'vStrln,( 1I1dl File;'" fileo, 1. 15' y++l;
g.dr,lfStrlog(Ruolutloo; .. song.gelRuolutlon(). 1. 15' y++l;
g.dr, ..Strlng(Tick length: * song.getTteklength(). 1.15' y l:
g.dr,wStrlng( Tr,cls; + soog.getTracks().leogth. 1. IS' y );
9.drawStrlng( 'Patches; +soog. getP,tchl 1St( l. length. 1. 15 y++l:
)elsel

Reusable (laue

g.drnStrlll9( "!rror

lo.dj~g

stquetlCe file""

flle~.

10. IS);

Reusable Classes
Now that you understand how to lood and play sound clips and sequence files,
leI'S pUl aU of this useful (but SC3tlered) code inlo two r~sable classes that can be
easily dropped into a project and used. Insle;Jd of d~ing with all of Ihe Java
sound c~ and packages, you will be able to simply create a new object from
11K- SO~ndCllp and Mtdtseq~e~ce classes, and thm load up and play ri~ a
sample or sequence with a couple lines of code.

r should

disclaim the userulllC$S of the:\(' c!uses for you, so you will know
",hat to expect. Java's Sound API has a sound mixer lhal works very well, bUl
we can't tal' il1to the mixer directly using the Cltp class that I've shown you
in lhis chapler. The sound fi les thaI you load using lhe Clip class do support
mixing, but a single clip will inlerrupt iudf if played repealedly. So, in the
case of Galactic War. whm your ship fires a we;Jpon , the we;Jpon sound is
restarted every time you play the sound. Howt'VCl". if you Iuve anolher dip for
explosions (or any other sound), then it will be mixed with any other sound
dips currently playing.
In Olher words, a single (1 ip object cannot mu wi,II it~l[. only with otMr- $(lurid$.
This process works quite well if you usc short sound effeds, but can sound odd if
your sound clips are one second or more in lenglh. (They sound fine at up 10
about half a second, which is typical for arcade"g.lme sound effects.) If yOIl want
to repeatedly mix a single clip, there are two significant options (and one other
unlikely option):
Load the sound file into multiple (I tp objects (such as an array), and then
play each one in order. Whml'Ver you need to play this specific sound,
JUS( ilerale through the array and locate a clip that has finished playing. and
then start playing it apin.
Load the sound fi le into a single (lIp objl, then copy lhe sample bytes inlo
mulliple C1 t II objecls in an array, and lhen follow the general lechnique
Mscribed in the first option for playb;lck. This saves l ime from loading the
clip multiple times.

169

110

Chapler 9 Sound Effeas and Music


Wril.. a thmtd.-d $(lund pbyba<:k class lhat cmt.-s a lH'W thr.-ad for eo,'.-ry
sound lhal is played. TIt.. thrad willlermilAt.. whm Ih.. sound has
compkt.-d playing. This r.-quir.-s $(1m.- pr.-lIy complex cock, and thue is a
101 of ov..m.-ad involv.-d in cr.-aling ;lnd destroying a Ihr.-ad for .......ry
singl.. sound Ih;lt is pl;l)'td. On........y 10 g..t around this ov..m..ad is to cr.-ale
a ,lrr..lld pool at th.. start of the program and th ..n reuse Ihreads in Ihe
pool. Again, Ihis is some "ery advanc~-d code, but it is how pro(C$,lional Java
games handle sound playback. If you write a great Java game suitabl.. for
publishing (such as Galactic War, which you will start in th... n...xt chapter
and dev..lop throughout th.. book), [ would m:omm..nd on.. ofth.. first
IWO options as good choices fOf a simple gam.-. You don't W3.nt to deal wilh
the ovcrbcad (or weighty coding r.-quircmmlS) of a threaded solution,
and an array of lh.. or so dupliones of a sound clip can be played 10 good
effect-with mixing.

The SoundClip Crass


The SoundCIlp class em:apsulates the Aud1 oSystelll, Audlol nputStruIII, and CI ip
classes, making il much easier to load and play an audio file in your appl els. On
the CD-ROM there is a project called Soundd.ass that demonstrates this class.
This class simply includes all of Ihe code _'v.. covered in th.- last f.-w pages,
combined inloa singl......ntity. Nolelh... key portions of Codelhat I'vediso.tssro in
this SCCIion, which are highlighled in bold.
Tip
A ~ projKt dflnoninmg this elMs Is wailable on the ([).R{)M in the Isol:ucesId\aplelO'Jl
SourIdClMslokle<.

i.po r t javu .sound. u.pled. *:


illportjan.lo.:
lllpOrt jan.net.:
publlc clan SoundCllp
't~e source for audio dna
prlnte Aud10lnputStrea. supie;
1/ sound eli p properly 15 reid only here

prl ute Cl 1p clip :


public Clip getCltpO I return clip: I

Reusable ClaSSl!'l
1I100p1ng property for cont1nuou$pllyllict
prhitt boolun 1000plng hi se:
public .old setlooplnglbooltlln _looplngl I looping - _Iooplng;
public boOleu getlOOlllngCl I return looping: I
IIrel"!ll1 prOl"!rt, U5e<! to plly $(\IInd ..ltlple tl.u
prl.ltt Int repe.! -0:
public .old setRepelltllnt jePflltl I ~Pfit - jePfu;
public Int getRepelltO I return rtPfit: I
Ilfl ItIlI.,. properl,

prhllte String fllen.,.- 00;


pub11 c .01 d setF 11 en.,.( Strl ng _fl1 enillt) I f 11 enllllt - _f I Ientllt;
publlc String getfilenll.,.O I return filenllrae; I
Ilproper t y to verlfYllhen Hlnple 15 rudy
public boo) elln Islolded() (
return (boolunHUllple I-null):

Ilconstructor
public SoundCllpO I
try I
IIcrult i 50und buffer
cllp - 1.IlcI10S,ltell.getCl Ipl I.
I c.tch lliMtJnn.II.lIle[ueptlon eJ I

IItMs onrlolde<l con5tructor ukes. souncl file IS


public $oundCl IplStrlng .lIdlofilel I
tM sl): I/c.l1 dehult constructor first
1old{ ludiofl Ie): Ilnoll 101ld the IUdl 0 file

II

Illlrllltter

prlute URL get UR L{Strlng filen!.) (


URL url- null:
try I
uri - thl s.getClus( l.geUuource( f l lenll.l:
I
citch (Exception ell
rthrn uri;

171

172

Chapter 9 Sound EffeCtl and MUlk


I/Ioad sound file
pUbi ic boo1 un 10ad(String audiofi Ie) I
try I
Ilprepue the Input stre for an .lIdio file

utFllena.(alldlofil.) ;
I/set the ...dlo strea. source
u~le 1.lId10Systl!ll.getAudloln""tStr.... (~tUIl{fllena.;
I/Ioad the aadl0 file
cllp.opH(sallJl l el:
return !rue;

I catch (IOExceptlCHI el I
return false;
I catch (UnsupporttdAudloFllebceptlon el I
return false;
I catch (LlneUnnallablehcept Ion el I
return false;

publ1c ~old play() (


flex1t 1f the snple hasn't been loaded
If (I hLoaded( II r eturn ;
IIrelet the sound clip
cl ip, setfrallePosl t ion(O);
IIplay u.,le wah opt lonal looping
If (looping)

cllp,Ioop(Cl1 p,LOOP_COITI MUOUSLYl:


else

ell p, loop( repnt) ;

public wold stopO I


cllp.stopO;

The MidiSequence Class


l1lc "idl~uence cI.ass is another CUSlom cI.ass lhat Il'IIUs it nsier 10 work with
tM Java sound code. This cI.ass malpsulates 1M IlldlSyste., Sequencer, and

Reusable Classes
~uenct

classes, making it much tuie:r to load and playa MIDI file withjusl lwo

IiI'lC$ or code inslead of many. Take nole of the: kc:y portions or code:. which ha~
bc:c:n highlightN in bold_

--

Tip

ACIIl'Ilpifte pnljKI ~ flsdlss is . . . . . OIllhe (I)-l()M illhe ~

IlIJIOrt JilYl. Ia. " ;


IlIJIOrt jiIYl.net.";
I~art JilYu.saund,.1dl,-;
public class M1dlSequence I
IIprlrury .I ~ I sequencer object
pr1v.te Seq uencer sequencer;
flprowl~e Sequence 15 reld-anly property
prlvatt Seiluence song;
public SequenctgetSong() I return song;

IIfllen property h reldanly


prlute Strl", fllen
;
publlc Strl", getFil
O I rehrn fil ...... ;
1I100ping property for lOOpl1l9 contlnUl;l\lsly
prhlte boolu" looping - fllse;
public boolun getlaoplng() I rehrn looping; I
public void setlooplnglboolun _looping) I looping

_100pln9;

IIrepe.t property for looping. fl~ed nu.ber of tl.s


prlute Int repeat - 0;
public void setReputl Int _repe.t} I repeat - _repeat;
publIc 1nt getRepeat(ll return repeat; I
IIreturns whether tne sequence h readjr for actIon
public boole.n hLo.ded() I
return lbooleanllsequencer ,I sOpen();

IIprl ..ry constructor


publlcMldlSeqllencel)I
try t
IIfire up the SeillIef\cer

173

174

Chapter 9 Sound EHero and Ml>sic


sequencer MldlSystea.lIetSeql>enctr( I :
I cltch (MldlUnnal1ablehceptlon el ( I

I
((over 1olded constructor accept s al dl fl1 enallf
public MldISequence(Str1n'l aldl file) I
thls(): f/cill debult constrlOctor first
10ld(aldlfl1e): f/load thealdl file

prlnte URllletURUStrlnll tll""a.) I


URlurl-nul1:
try I
uri thi s .IIHCI iSS( ) .lIeUesourcel fll ft'allf):
I
cltch ([ueption e) I
return url:

f/l Old a aid 1 fl1 e 1nto

I seqllence
public boolean 10adlStrlnll .ldlfl1e) 1
try (
Ilioad the.l dl file Into the seq~encer
filenaltf .idl file;
sonll IIi dl Systfll.lIttSequenct( lIetURl( fil enaltf I ) ;
stq ~ encer . setSeq~enc e (song) ;
seq~encer open ( ) ;
r etur n t rue ;
I catch (lnvalldllidlDahhceptlon ell
r eturn hhe:
I catch (1I1diUn"ulhbleE:<ceptlon ell
retu rn hhe;
I clltch llDE:<ception el I
return hhe:

f/play the aidl seq~ence


public wold playO I
If (!seq"!ncer . IsOpetll) I ~turn;
If (Ioopi"lll I
sequencer. setloopCou.t (Stqllfnctr LOOP_COITllOOIISLY) :
sequencer.surt() :
lehe(

Review Questions
sequencer. utLoopCount( repnt) ;
sequencer. start( ) ;

fl st op the 81dl sequefl(e


pub li c wol d s tt:lp () I
seqllf1neer .SUlp( ):

What You Have learned


This chapter explained how 10 incorporate sound dips and MIDI sequences into
your Java app lets. Game audio is a wry important subject because a game is just
nu fun without sound. You learnw;
How to load and playa d igilal sound file
How to load and playa MIDI sequence file
How 10 encapsulate reusable code inside a class

Review Questions
The following questions win help you to determine now wdI you have learned
the subjects discussed in this chapter. The answ=; are providw in Appendu A,
"Chapter Qui:t Answers. "
I. What is the Il3me of Java's digital sound system dass~

2. What is the name of Java's MIDI music system dass?


3. Which Java dass handles the loading of a sample

file~

4. Which Java dass handles the loading of a MIDI file?


5. What type ofexception error will Java generate when it cannot load a sound

file'!
6. Which method of the MIDI system returns the sequencer object?
7. ''!hat is lhe main Jan class Menrchy for the audio system

class~

175

176

Chapt~

SOund Effects

~nd

Music:

8. What is the main Java class hierarchy for the MIDI system class?
9. What three digital sound file formats docs JaV<l support~
10. What rare exception error wiD occur when no MIDI sequencer is aYi.ilabl~

On Your Own
Use lhe following exercisc:s to Icst your g:rup of the mat~al oovcrC'd in this
chapter. Are you ready to put sound and music to the test in a real game yea
These exercises will challenge your ullde~tanding of th is chapter.

Exercise 1
Write your own sound-effects generating program to try out a large list of sound
fi les. You can acquire sound files of various types by searching the web. Have the
program playa specific sound file by pressing kC)'5 on the keyboard.

Exercise 2
Wrile a similar progrom for playing back muhiple MID I music sequence files by
pressing various kC)'$ on the keyboard. For an even greater challenge. try combining this program with the one in Exercise I so that you can try out playing
music and sound effects at the same time!

CHAPTER

10

TIMING AND THE GAME Loop

You havc learned how 10 uSC' thc Graph1cs2D class to program graphics using
ve<lor shapes and bitmap images, and you have even seen a nearly complele
game wrillcn from scnuch. YOli ha\c lcarnffi how 10 load and play SOllnd files
and MID I mllsic files, and how to program lhe keyboud and mouSC'. By all
aCCOllnls, you have the 10015 to create many different games already. Bllt there are
some tricks of the lrade-srets of the craft-thou will help you to make your
games stand out in the crowd and impress. This chapler discusses the game loop
and ilS vilal importance 10 a smoothrunning game. You willleam aboulthrea.ds
and timing. :uld you willtUe the Asreroifis..-styk game crealed in Chapter S inlO
completely IKW terrilory, as it is modified extensively in Ihe follo""ng pages.
Here are the specific topics you willleam about:
Overriding defauh appll'! methods
Using timing melhods
Starting and Slopping a lhrcad
Using a thread for lhe game loop
Iluilding the Galactic War game

178

Chapter 10 Timing and t he Game Loop

The Potency of a Game loop


The key to creating a game loop to facilitate the need$ of a high-speed game is
la\"3's multithreading capability. Threads;IU sIKh an integra.! pan of Java thai il
mUt$ a special thread available to your program jusl for this purpo$t. This
speciallhread is called Ru"n,ble, an inlerface class. However, il'smlirdy possible
10 wrile a Java game "',thout th reads by just using a simple game loop. I'll show
you how 10 do this first, and then we1l take a look at threads as an even bener
form of game loop.

An inttrff am ~ "" <>b5tr"" dM5 wn!I propenies and meIhods lhM 3Il! definfd but I'lOl . . .
mJIfd. A P'O!1'" thM """ an inIfff~ dM5 ;.; ~ 10 aIflSOfIlf it. and must i~ lIlI of !he
pobIic rnttf>o(t in !he inlerfKe. lypciI namples indu:le [fyL t s tener aod Runn,b 1e.

A Simple Loop
~

Runn.ble interlace gives your program its own awareness. I realize lhis
concept sounds a lot like anifida.l inleUigmu, but the term <IW<lrmdS is a good
description oflhe Runnable interf.lce. Before Runuble, you r lava programs have
been somewhat naive, capable of only processing during the screen refresh. Let's
take a look at an example. Figure 10.1 shows the SimpleLoop program. As you

f;9U~

10.1

The~propn.

The Potency of ao Gaome Loop


caon~,

this program doesn', do aonylhing in ruJ time; it waits until you press a
key or dick the IDOliSe' bero~ d"'wing a rlangk.

.
f

.............

lllpOrt JUt
lllpOrt j
lllpOrtj
l.po r tj

aollt.:
aollt.nent.:
app lu .:
utl1 .:

pu blI CChH SlaplHoop extends Applu lapleMents Key U stener, MouseU stener I
R.ndo- rand

new RalldOll():

publiC YOld in!tO I


.l1dh,ll stener/ thl s I :
aoddllCl~sellst_rttlllsl

publie fold p.llnUGraophlcs gl I


Gr.phlcs2D g2d IGraphles20l g:

Ilcreate a randOIll reetangl@


lntll - rand.n extlnt( 100 ) :
lnt h rand.nutlnt(100):
lnt x - rand.nutlnt(getSizeO lI idth . II ):
intyrand.nutlnt{getSlze().helght h):
Rectangle reet new Reetangle(x,y,II,h):
JIg_rate a randce color
1nt rN! raolld. next [nt (2!16) :
lnt green rand.nutlntI2~l:
lnt blue ralld.nutlnt(2!>6):
g2d . s@tColor(ne-ColorlrN,grnn,blue)l:
Jldrallthe rectangle
g2d.flll(reet):

Jlhandle keyboard nents


public foid keyRelused(hyEfent kl I
publle fold keyTyped(KeyEwent kl I I

179

180

Chapter 10 Timing and the Game Loop


public void keyPreneilCKeyEvent kl I
rep.lntO;

IIhandle ..use uents


public void ..useEnter!'dlllousehefll _, t I
public void ..usehlteilUlouseEnnt _, I I
public void -ouseRelus!'dlllousehefll _, ( }
publl c void -ouse( 11 ck!'d{lIouseE,ent _, I I
public 'old -ousePrnseil{lIouseEvent _} I
rep.lnt();

Tip
The Ra ndom dass ;\ loeatt<! in the j ava alit, ut j I 005 aloog with many other lIIihly OOses
that provide common~ oeeclfd tools to yoor program, To use Rando_ in your program, you must
~ this dim with ~ followi"!lt .port ltateme<lt:
l"POrt jua ,'111. utll.:

This program h.as no loop whalSOeVn", so it cannOI process anylhing in rtal


tim~nol sp.au ships, asteroids, jumping Italian plumbers. yellow dot -tatCT$, or
fe<mJe spehmk~ packing dual Beretta The only thing ll1is program can do is
dr.iw a single reclangle.
Ttte-n are som!!' ways you can make Ihe progr.im ;II little more inler.ietive,
The only problem with the mouse and kl!'}'bo.1rd listm!!'r interfa.cn is th'lIl
you hay!!' 10 implemenl flll of them or 110m' of them. That reminds me of
Yoda's famous saying, "Do, or do not. There is no 'try.'" An interfac!!' class
is an all-or-nothing proposition Ihat lends to junk up your SQurc:e rode, l1()t to
mention that it takes a lot of work to typt' in all of those interface event
methods every time! But there's no real workaround for the unused event
methods.

Not .
I've t-n thinUng about. w~ to use;ll 01 thew interiiKe OOses (such ill Runnable and l!le
input listeners} by lUCking tIlrm MIl into a class ~ of tIlII' main~. This 5I4'POfI class
-..:I pn:I'Iide ""f main progfanl wilh rY lMIIlS when IIWlgs ~ sudl as bJ fft15eS,
fI'IOtM ll'ICIWlI'I!I'I II'Id CIlher _
~ this is dle biIdl 01 an idN lIwl wiI beame some
SO"! 01 game ensJilIel l.e'I's wait II'Id see wIIat Part .. "The Galactic war 1'nljKt,. has in S'n.

The Potency of a Game Loop

Overriding Some Default Applet Behaviors


lbtre in $Crious problem with this program b2use it was supposed to just adda
new rectangle every time the user presses a key or 1llOUC' bUIIOn, not rtdraw tM
entire appld window-with a single rUngle left OVtt. lbtre is definitely some-thing odd going on b2UC' this program shoWJ J.D,,!, worUd as cxpro.cd.
Well, it tul'JU out that Java has bn ~ng with the screen without pennissionor ~thcr, by difaw( The Applet class, which is the basis for the SimpleLoop program (rec:aIl that it ati'7UU Applet), provides many default cvmt methods that do
artain things for)'Ou. You dont even nm:Ito implement pllnt{) if you don't wanl
to, and theApplet baseclass will provide il for your program. Granted, nothing will
be drJwn on the window as a rcsult, but the compiler won't give you an error. This
differs from an interfuce class (such as Keyll stener) that IIUl1IdllleS that you must
implement all of its abstruct methods. So it's prelly obvious by this difference in
functionality Ihat Applet is not an interface class, but a fully functioning class.
What happens. then, whrn you implement an Applet clasli method such as 1nlt(}
or p.intl l? These methods aT(' essentially empty inside the Applet class.. ah, they
exist and are not abstract, but they don't doanyrhing. The Applet claS$ defines Ihese
md.hods in such a way thai you can O'Wf'T"i,kthl'lTl in your ..ppld.. For instance, in
the SimpleLoop progr.un, SimpkLoop is aaually the RanK' of the class, and it
inherits from Applet. 1lJnrl"0f"C', Simpldoop has the opportunity to override any
ohhc methods in Applet that il wa.ni.S to. including hilt( I and pllnt( I.
Howt'Ytt, tMre's anothtt method that we haven't wed)'l!'1 aUed upodate(). Aha!
No. I wasn't holding out on you-you've aaually wed Ihis method before. in the
game project back in Chapltt 3. 1bt updlte() method actually IIs do something as coded in the Applet cbJs-i t caIls replintO 10 refresh the appld.
window. (Light-bulb momen!.)
Since the default upd.teO method has been refreshing the sen"en for our programs, we have had ab$Qlutely no c01l1rol over this process. That explains why
only one rectangle was being drawn at a time in the Simplel.oop program updlle(} was refreshing the sereen on its own. In a sense, your Java program is a
slUl'e to the master Applet class until you override the functionality by rewriting
its methods. It's time 10 break tht bonds of objoxl-Oritnled slavery. Let 's add the
update() method to the program:
puMlc void updlte(Grap~jcs 9) I
pa 1nt(t);

18\

182

Chapter 10 Tim ing and the Game Loop

Figure 10.2
The SimpleI.oop progrilm now dlaws rna." ~Ifs.

As you can 5C'C, the: upchte ( 1 mdhod is bearing a single: line: of cOOc:, a call to the:

pal nt ( 1 mc:thod. This giVt:$ you compk1e: control over the: scrttn rdroll bc:causc:
the default updH ell would root just rqxtint the: scrttn, it woukl abo dmr 1M
scrn. Now you can run the: SimpleLoop program and Stt a bunch of rectangles
as originally expected, as shown in Figure 10.2.
I think this example makes it pretty ckar that any serious game ...~1l nte:'d to
override the update() method as wdlas paint( 1. But that doesn't rally address
the subject at hand- what about the loop? I've been calling this program
SimpleLoop when no loop is even being used. Let's gettD that now.

Feeling loopy
There are quite a few Applet methods available that we haven't implemented yet,
in addi tion to the standard methods you've snon.so far. I'll go over the remaini ng
kq Appl et methods at the end of this Stion to make you aware of them, For
now, I'd like to introduce you to the: next Appl e t mdhod: stutO , The sUrt( I
mdhod is invoked in your Java appltt by the web browser right after calling

The Potency of a Game Loop

in1t( l. So, you can use in1t() to get the game ready to go, and then use Hart()
to get thing!i moving.
Now you finally haY< an opportunily to add a real loop to this progr.tm. BUI just
for kicks, what do you think would happm if you added a III1i le() loop to the
In1tD method? I tried it, so you should try it too. Doing this will lock up
the applet, which will not evcn be disp layed. Why! Ik<:ausc inl t( l is ealled before
the applet has evcn brought up thc web browser or applet viewer, so putting a
loop he-re preve-nl$ the- apple! from drawing. Howeve-r, this is not the- case with
the stutl) method, which is calkd afte-r the applet has hem initialiud and is
ready to go. lbe engine has hem started and is just waiting for you to press theaccde-rator at this point.
You can build on this game loop, too. The-caUto re-pa int the-window isonly the last
in a gaml". Firs!:, you move your game objects around on 1M SOttIl, pttform
collision testing. and so forth. You can ptrform all of these steps in the- start()
event and then call re-paintl) at the very end. Here- is a suggestion. This is 1I0t
actual code that you should add to the- Simpld.oop program-it's just an eltample.
.st~

void stJrtO I
lithe 91. loop
vIIlh(trlle) I
II.,;).., the 9111e objects
upd~ teObJ ects( l ;

p~bl1c

llperfo", collision testl/l9


testeo!! hlons( l;
!lredraw the window
repa1"t();

Recovering long-lost Apptet Methods


Learning about the start() and ~pdHe() events was intriguing. Let's take a look
at some morc helpfulrntthods in the- Applet class lhat will Ix useful.
The- ShowStltllS( 1 method is used to print some te-xt in the bonom status har of
the- bro~r window or apple! viewer program:
showStaluslString 1S9l

183

184

Chapter 10 Timing and the Game loop

The IsActi Yet 1 method delermines whether the applet is a'tive, which is marked
just before the stut() event method is ailed:
boolun isAtthel)
Tbe st.rt() method is called after InttO arK! il; often usN to resume the apple!
after the usn Iw; b ~ away from the applet page and then returned.
YOld sUrtO
Tbe stopl I method is called by the web brows.er when the web
signifying that the applet is about to be destroy!.

pa~

changes,

Yold stop()
The destroy( 1 method is <:ailed by the browser when the applet is about to be
destroyed (removed from memory).
void destroy()

Stepping Up to Threads
We use the Runn.ble interface class to add threading support to a game. This
inttrface has a single tvent method called rill'll) lhat represents the lhread's
functionality. We can cmllle this rune 1 method in a game, and thaI will act like
the game loop. Tbe thread just calls rill'll) once, $0 you must add a willie loop to
this method. Tbe"hlle loop will be running inside a thread t.ha.l il; 5CpaTllle from
the main program. By implemenling Runn.ble, a game bKomes multithreaded.
In that sense, it will su pport multiple processors or processor,ores! For instance,
if you have an Intel Core2 Duo or another multi-core processor, you should be
able to $Ct' the applet running in a thread thai is 5CpaTllte from theweb browser or
appltt viewer program.

Starting and Stopping the Thread


To gel a thread running in an applet, you have to create an insta nce oflhe Thread
class, and then stan it run ning using the applet's st.rt{) event. First, let's crcale
the thread object:
Thre.d thrud:
Next, create the thread in lhe surt{) event method:
PIlbllc Yold sUrtO t

Stepping Up to Threllds
thrUd
ThrudCtll1s);
thrud.SllrtC);
&

Then you an write the "ode for the thread's loop in the run() n't'nt method (pan
ofRunn~bh).We'll take a look at what goes inside runC) in a moment. First, let's
take a look at the stop() event. This method is provided by the Applet dass and
can be overridden. It has no functionality bydcfaull. This is a convenient place to
killthc thread, so let's do that:
public woid stop() (
thrud-n~ll:

The ThreadedLoop Program


Let's take a look at thCl'ntireThre:adedLoop project, and Ihm I'll nplain the loop
for the thread inside the runC) method. The key ponions of code have been
highlighted in bold.

ThrudedLoop progr

....

l-eMlrt j.n.'wt.:
lllpOrt j.n.I.,..,. ;
l-eMlrt j.YI"wltt.:
IlI(IOrt j.n.utl1.;

pUblic class ThrudedLoop ntends Applet 1111l1rllrnh Run".bl, I


I/r."da. n~llber gener.tor
Randolll ra"d new Rand01llC ):
&

Ilthe ..i n thread


Thrud thrud;
IIcount tllr n.-ber of rectangles dra....
longcount-O;
flapplet Init ennt
public wold Inlte) 1
Ifnot needed this ti-e

18S

186

ChapiN 10 Timing and the Game Loop


!llpplet surt tvfllt
pub11, void suno (
thread" new Thrna{th1s);
thread .stut{) ;

Ilthrnd run eyent


publl' void run{) I
1000g stut ~ Systeoo"urrentTI ...II1!s(),
lIa,qulre the ,urren! thrnd
Thrnd 'urrent '" Thrnd."'rrentThrnd( I;
{Ihe r e's the new gaM loop
wtl1le ('urrent-- thrndl {
try (
Thrnd ,sl eep(O);
}

utcll1lnhrruptedExuptlon d (
e.prlntStadTra,,() ;

IIdrlw s_th1ng
repl1ntO;
Ilflgure aut hOll fnt 1t's rUll/l1ng
1f (start + 1000 <Systell.,urrentTl-eMlllls( II (
stut - Syste_.,urrentT1-eHlll II( I;
shOllStatus("R"tanglts potr le-eolld, + count),
,GlInt - 0;

Jllpplet stop event


publ" Yold stopO I
thrnd .. null;

JlapplUIlpc!lte eyent
publl, void upc!lte(GraphlCI gl I
pllnt/g);

Stepping Up to Threads
II'W1et p.lnt elent
pI.Ibllc lold p.lnt(Gr.p~IU g) I
Gr.p~IU20 g2d (Gr.phtu2DI g:
IIcreHe' r.ndOil rectangle
1M II qnd .nextlntOODI :
lnt h r.nd.nutlnt{lODI:
tnt ~ r.nd.nutlnt{gHSlu() .lIldth . ,:
tnt J . f'nd.nntlnUgHStu( l.~etght . h):
Rectangle rea new Rectangle( . J .1I.h):
IIgeMute I r.nda. color
tnt red rlnd.nutlnt(256):
tnt IIreen r.nd. nutlnt(2561:
int blue rand . nutlnt(256):
g2d. setCo l or( nell Cclcr(red.green .bl ue)):
IIdr.1I the rectangle
92d. ftlll rect):
lI.tid .noUer to
count... :

t~e

counter

Now let's eX3mine this ru MI event that is called by the Run nab l e interface.
There's a lot going on in this method. First, a variable calle<! start gelS the
current time in milliSC'Conds (Systell.currentilIleMlll t $( I). This \'alue is w.ed to
pauseOIKC per second 10 print out lhe total number of rect.. ngles th..1h..ve bttn
dnwn (5 Figure 10.]).
Next, a local variable is set to the currenllhread, and then a IIhlle loop iscreate<!.
Thrud current - thread.currentThread():
This local thread makes sure thm our loop only processes thread events intended
for the game loop because you can use multiple threads in a program.
IIh lle (current thrudl

lbe core of the threld loop includes a call to Tllrud.sIHp(OJ, whkh is a place-holder for slowing the game down 10 a consistent frame n le. Righi now it's running
as fasl as pos.sibk because the sleep( ) method is bting passed a O. This single method
call requires an ClTor handler because: it throws an [nterruptedE.cepticn if the

187

188

Chllpler 10 Timing and the Game Loop

fi9ure 10.3

The 1'hrNd..t.oop pnIpll <ilpYr; d'ot ........ ~ '*-' "" MalI'O!.


th~ad is ever interrupted by anolherthn:ad. This iun advanced subject lhal doesn't
concern us, [fthe thread isinterruptcd, it's not a bigdeal-wc'lJjusl ignorcanysuch
error lhat might crop up.

tr, 1
TMud.sleepIOl;
I

cltcM Interrupted(Kcept I(ln el 1


e.pr\ntSuckTr~ce(

l:

After this call 10 sleep (which will slow 1M g.t1Tle 10 a consistenl frame rate), Ihm
we can call game-rebttd methods to updatt ob;tcts on the Krn, perform
collision taling, perform g.tme logic 10 interact wilh enemies, and $0 on. In the
block of code thai follows, you can ~ $Ome liming code inside an If stalemenl.
This code prints OUI the number of rectangles thai have been drawn by the
pll nt() event during the pasl 1,000 milliseconds (which equals 1 second).
IIdrill

s_thtng

rep~tnt(l:

What You
I/fl9~r~ o~t

I t (su rt .. 1000

Ha~

Learned

tnt Its runnl"9

<S,SUII.currentfllltl\! llls( l)

I
sUrt e S,5ttll. current TlIlelli111 S( ) :
sh""Status( R~ctan91 u per second: . .. count):
counteO:

The single call to repaint() aClually makes this program do soml'/hilrg; all of the
rest of th~ code helJl5 this ml'1hod all to do its job cfl"a:tilely. Here inside the
run() event, whkh houses the: new thrnded game !oop.l've used one of the: new
.&.pplet ITIl'1hods. 1lIe shCStatus() ITIl'1hod printsoul a string 10 tbe status har at
the: bottom of the Web broW$l"T or appld viewer.

Examining Multithreading
Aside from the sample g;une in Chapter J, this program might h~ve been your
first exposure to multithreaded programming. Java makes the process very easy
wmp;lred to other languag('S. I've used a Ihread library cilled pthread to add
thread support in my C++ programs in unux and Windows. and it's not very
taS)' to USoC at aU wmp;lral to Java's built-in support for threads.. We will wntinue to USoC Ihreads in every subsequent cbapter, 50 you will bave had a lot of
exposure to this subjt by Ihe lime you complde Ihe book.
Nole
~ Third Edirion (Thomson (oorse PHI. 2006l 00YefS the pthread
and rnatl'f other ouss1Ntform game ~ \Opics. and is bawd .-.::I the AIltgro
IiiInry and the ( ...... ~

Game ProgtMllflling All 1/1

~brary

What You Have l eam ed


This was a heavyweight chapteT wt wvnal some very difficult subjl'cts. But the
idea is to get up the most difficult part of a hill so you can reach the peak, and then
headon down theotheTsidc. That iswhat thischapterreprcsems- thc last few steps
up to the peak. You have now learned the most difficult and challenging aspects of
writinga Java game at this point, and you are ready 10 start headingdown lhe hill at a
more leisurely pal? in the upcoming chapletS.. This chapter aplained:
How 10 create a Ihreaded game loop
How to override default appld mdhods
How to manipulate a bitmap with transformations

189

190

Cnapter 10 Timing ,nd the Game Loop

Review Questions
The foUowing qUe$lions will help you to determine how wdJ. you have learned
the subjt.s discussed in this chapter. 1be answers are providM in Appendix A,
"Chapter Quiz Answers.."
I. What is the name of lhe inlerface c1as5 lhat provides thread support!
2. What is the name of the thread execution method thai you can
code inside the separate thread~
3. Whal is the name of the class that handles vec10rba5ed

U$e

to run

graphics~

4. What Thread mdho<! causes the thread to pause execution for aspecified time?
5. What Syste. method returns the current time in milliseconds?
6. What is the name of the met hod that returns lhe di rectory containing the
applet (or HTML container) file?
7. What is the name of the method that returns the entire URi string induding
Ihe applet (or HTML container) file?
8. What c1ass do you

U$e

to store a bitmap ima~

9. Which 6Ullhlcs2D method is used 10 draw a bitmap?


10. Which c1ass helps to improve gameplay by pr<Mding random numben?

On Your Own
The following exercises will test you r comprehension of the topics covered in this
chapter by making some important ch.anges to the projects.

Exercise 1
The l breadedLoop program runs at br....akneck speed with no dday. Modify the
th read delay value to see whether you OIn slow down lhe numbl.'"r of rectangles
being drawn to just 1,000 rectangles per second.

Exercise 2
The ThreadedLoop program jusl draws V!or graphics. Modify the program so
that it draws animated sprites instead.

PART III

THE GALACTIC WAR PROJECT

This final part of t~ book is devoted 10 th~ dtvelop~nt of a compll'1~ gam~


called Galactic War, built entirely in Java as a web appld. By the lime you have
finished reading the book, you will have lC'3rned how to create this game from
scratch and deploy it to your website. Here are the chapters in Part Ill:
Chapler II: Galactic War: From Vectors to Bitmaps
Chapler 12: G~laclic War: Sprites and Collision Boxes
Chapter 13: Galactic War: Squashed by Spacc Rocks
Chapter 14: Galact ic War: Entity Management
Chapter 15: Galactic War: Finish ing the Game
Chapter 16: Galactic War: Web iA-ploymmt

CHAPTER

11

GALACTIC WAR: FROM


VECTORS TO B,TMAPS

The G~I~ctic War proiect will demonstrJte just one type of game that can be
created in Java. This game is compln, but that complexity is hidden inside a
game engine that, once written, does not n~'\'d to be opened again. You will write
an al'pkt that will inherit from the game engine, and then the vast majority of
the core code for the game will be handled behind the scenes. We'll build the
game step by step, beginning with the simplistic Asteroids-style game from
Chapter 3, gradually improving the game until it is finished and ready to be put
up on your website. The first step to building Galactic War is to begin converting
the original project from an entirely vector-based game into a bitmap-based
game. We'll start with a partial conversion in this chapter, retaining some of the
vector shapes but replacing the player's ship with a bitmap.
Here are the key topics:
Improving the game
Generalizing the vector classes

Improving the Game


Chapter 3 gave you an example of a semi-complete A>leroids-style game to
show you what would be covered in the upcoming chapters. You have now
learned enough about game programming in Java to greatly enhance the game.
That original game featured a class called BaseVectorShape, which contained

'"

194

',,",pter 11

GiI~k

W"r: From 'leeton to Bitmaps

all of the properties needed to manipulate game obje'C1s on the scm:n (the
asteroids, bullets, and ship). In this chapter, we'll make a few changes to add
sprite support, and in the next chapter we'll move the game entirely over to
bitmaps.
By upgrading t~ spaceship to an illl<lgt' and doing away with t~ Vtor ship
(which was liule mon: than a filled triangle), the pme is rally starting to look
mon: playable. Then: is no substitute for bitmapped graphics. But ooe of t~
gools I set out to achieve for this game is adding the ability to use an image
instead of a shape, while still retaining the existing tra nsformation features (most
importantly, real-time rotation).
You an open up the: project from Chapter 5 and continue using it or you an
just copy the .jav.ll files from t~ old project to t~ new project. Here an: the
source (Ode files you will ....-ant to bring over to the new game from prC'Yious
chapters;
&seVectorShape.java

Asteroid.jav.ll

Bullet.jav.ll

Ship.jaV1l
SoundClip.java

MidiScquence.jaV1l

Note that I did not include the main source code file, Asteroids.java. There an: a
few changes needed to add image support to the game, so [ will just give you
the complete sou rce code listing for the main code file, which is IlOW called
GalacticWar.java.

Generalizing the Vector Classes


In the Asteroids project in Chapter 3, there wen:S("Vera[ dassoes to handle the ship,
asteroids, and bullets in the game. Now we're going to gencrolize these thr~
classes and make them mon: general purpose, since a lot of code is shared among
these classes. The Ship class will be replaced entirely with an l..gcEntlty (covered back in Otapter 6). Let me show you what wt"n: going to do with the
.\steroid and Bullet c~

Improving the GlIme


Cre~te a n~ soun:eeooe file(3l11 VectorEmity.java. 'l1lis class is verysimpll", ~s
the following rode suggests. Note that this class inherits from hse~.Entl t)'!

Holt

ector clHS for hindllng gi. ent I ttes

..........................................................
l"IlOrt

JIY~.i.. t.;

public clin YectorEnt I t)' utends BlseGi.Entlty I


lIurhbles
prhlte Shipe shipe;
Illccusor llethods
public Shipe getShipe() I return shipe; I
lI..tator .thads
public void setShipe{5hipe shipe) I t/l1s.shipe Shipe; I
Ildefault constructor
ectorEntHyO I
set~pe(null ):

The New Asteroid Class

The Asteroid class will be modilil now to use YectorEntity as a base class. This
fred up a lot of code that was previously dupliGitoo in Asteroid and the olher
classes. The Aneroid class inherits from YettorEntlty, which in turn inherits
from BaseGallleEntlt)'. You eun open up the Asteroid.java file that you copied
over from th{'" projfft in Chapter 3, or you can just add this as a HeW dass to the
Galactic War project.

I......
Asteroid chss derlYes trOll hseYectorShipe

......................................................1

publiC ehss Asteroid utftM!s hctorEiltlty (

195

196

Chapter II Ga lactic War: from

Vt~

to Bitmaps

JI<lefllle tM nterold poly9Oll shape

prlute lnt[] ut,,-120.-I3. 0.lO.22. 2(1. 12. 2. -I(I.-2Z.-161:


private lnt{] asty ( 20. 23.17 .20.16. -2(1. -2Z. -Ia. -11. -2(1. -51:
II rotation spee~
protected double rotYel;
public double 9!tRotatlonYeloclty() 1 return roUel: I
public wold utRotatlonhloc1trlcloubh, yll rotVel - y: I
llbounding rectangle
public Rectangle getBouoos{} 1
Recungl e r :
r -newRectingle({lnt)geU{) - 20 . (IntlgetY() - 20 . aO. aO):
return r;

lldefault COflHructor
AHeroldO I
setShapelnew PolnonlulJ:.. aHy. un_length):
setA11we{ true) :
set Rotat1 cnVe l ocl trIO . 0):

T"h. New Bullet Oass


Much of the code in the previous Bullet daSii has now been moved to VectorEnt 1tr as well , so we can JUSl rewrite th i5class and give ilthe specific information
relevant to a bullet object (most notably, that the getBounds() method returns a
rectangle thaI is one pixel wide and one pixel high)_

bllet class derhes

B.aseYeetorShape

..........................................................
f~

lJlllOrl jan .awt. :


publiC class Bullet utends YectcrEntl tr I
llboundlng rectangle
publlc ~ctangle get60uMs l I 1
Rectangh r;
r - new Rectang' el lint loeUO. Ii ntl geIY (). 1. I):
return r:

Improying the Game

BulletOI
IIcrute the bullet

l~.Pt

letStl~PtlfN!'ll~cu",le(O.0.1.1));

letAlhelhhel:

The Main Source Code File: GalacticWar.;ava


The actual gameplay hasn't changed much in this new revision. Figure Il.l
shows the game with a new bitmap image being used for the player's spaceship.
However, we have upgraded the core classes sign ificantly in this updme.
which will be very useful in the nex t chapter, where ImageEntity will sec a lot
mOTe usc.
1.,gtEnt tty, which you learned about in Chapler 6. provides a getBounds()
mnhod to perform collision lesting. You can stilltogg.le the bounding rectangles

figu.. 11. 1
n.. plIyfr's spI'Ship ii ..- I bitmap II'N9l' ..n.. Ih.1n

palypL

197

198

Chapter 11

Galactic War: From Vectors to Bitmaps

Figure 11.2

The bo<lrllliog ,ectangk.'s are lM<.! for coIlisioo testing.

on and off by pressing the B key. Figure 11.2 shows the rtangle around the
player's ship. The collision code is a little too strict for a truly enjoyable game
be<:ause the bounding rectangles are slightly too big. We'll corrt th is by finetuning the collision code in the next two chapters.
The majority of the code remains ullchang~-d from the Asteroids.java file b.lck in
Chapter 3, so you may just open that file and modify it as indicated. But I'm
going to list the emire program again because it's been a long time since we went
over that code. I have highlighted in bold att of the lines of code that have
changed. If the source code for a particular method has nol changed at all, I
simply commented out Ihe code and inserted Ihe statement linD changes needed,
so keep an eye out for this comment and then reuse th:l! code from the Chapter 3
projl. II is a beallliful testament 10 object-oriented programming that so few
changes arc needed to this source code file!
/

.
.....................................................

GAlACTIC WAR.

Ch~pter

II

I.por t java . appl H.':

Improving the Game

IlIIlCrt
I.rt
IlIPCrt
llIIPOrt
llIIPOrt
/

jUI.IlIt. ;
jUI.IlIt.Uent. ;
jUI.IlIt.g_. ;
jUI.IIILluge.
jUl. ut 11 . ' ;

.
..................................................... ,
Prlurr ellss for tl\e gl

~ull\lc

elln Gll,etWln utend~ """p1H 1l1pl_nu IhInnlble. leylhtener


lithe uln thrud Ilec.-e~ tl\e gl.loop
Threld gl.loop;
lIu$! thh 1$ I double buffer
Bufferedluge blCkbuffer;
Ift~ uln dnlllM object for t~ blCk buffer
GrlpMC$lO g211:
IItoggle for drilling bounding bou~
boolun ~hOllBound~ rll~e:
/feru t e the u t erold Irrar
In t ASTEROIDS - 20:
A~ terol d[] IS t - nell As terol d[ASTERO IDS] ;
Ilere~U tne bul let array
In t 6UllETS -10:
6ullet[) bullet - neIl6ullet[6U llETS) :
Int eurrentBullet - 0:

lithe p1iJ'lr' s sMp


'lOIIge[nt1ty ship new ' Nge[ntltr(tMs);
Ifcrute Ue Identl tr trlnsfoMl
AffineTrlnsfol'll identity new Afflnelnn~foMl( I;
/fcrute I rlndoe nlllltH!r gellerltor
bncloe rind _ Rlndoe( J;
/flold 50un<l effecU
SouIldClip 51looot;
SOu!ldCllp uplode:

I,
.
..................................................... ,
Ipplet \nit eyent

199

200

Chapt er 11

Galactic War: from Vectors to Bitmaps

pub l lcvo1dlnit()I
/I c reate the back buffer for smoo th graphi cs
backbuffer new Bufferedllllage( 640. 480, Buffered Image. TYPE..I NT.JlGB) :
g2d - ba ckbuff er . c rea teGNpht cs () :
({set up t he shl p
sh1 p. setX( 320) ;
sh1 p. setH 240) :
sh1 p. 1oad( "spacesh1 pi png " ) ;
sh1 p. setGraph1 cs (g2d) ;
{Iset up the bul lets
for (lnt n 0; n<BULLETS: n+-+)
bull et[n] - new BUll et();

flset up the !steroids


for (Int n 0: n<ASTEROI DS: n++) {
ast( nJ new As tero; d( ) ;
as t{ nJ. setRotat j onVe l oci ty ( rand . ne~tlnt( 3)+1 ) :
as t! nJ. setH (double) rand. ne~t Int (600 )+20) :
as t! nJ. setH (double) rand. ne~t 1nt( 440 )+20) :
as t! nJ. setMoveAngl e ( rand. next Int I360) ) ;
double ang - ast[nl.getMoveAng l e() . gO:
asUnl . setYelX(ca 1cAngl eMoveX(ang)):
as Un] . seUe IYIca 1CAng1eMove( a ng ) ) :

(l1o!d sound files


shoot - new SoundCI1 p( shoot ,lIav);
explode - nell SoundC l Ip( explode lIav):
Iistart the user Input l istener
add Key Lis tener( thi s ) ;

I .. .. .. .. .. ..

app1et updlte event to redraw the screen

........ .................. ............ . ..... ... .... u l


pub l iC vo1d updatelGraphlcs g) {
liND CHAN GES HERE

Improvin g t he Game

.....................................................
drawShip called by applet update e_ent

public _oid drawShip() 1


fftrUsfor. and draw the sh1p
sh1p. t r ans for.( ) :
sh1 p.dra w() :
//draw bounding rectangle around ship
if (showBoundsll
g2d. setT rans forll( i dent j ty) ;
g2d. setCo1or( Color. BLUEl :
g2d. draw( shi p. getBounds ( l ) ;

There are no changes beyond (his point. Please double-check the source code
listing in yOuT new Galactic War project to ensure that all of the methods following this point are included (from the project in Chapter 3). If you prefer,
you may open the completed project on the CD-ROM in the folder \sources\
chapterll\GalacticWar.
/

.
.....................................................
drawBull ets ca 11 ed by

~pp 1et

update event

publlc void drawBullets(} I


,/NO CHANGES HERE
/

.
.....................................................
drawAs tero i ds ca 11 ed by aPO) et update event

publ ie _oid drawAsteroids{)


//NO CHANGES HERE
/

..
.....................................................,
applet window repaint e_ent 'draw the

pub 1ie void palnt(Graphics gl I


//NO CHANGES HERE

bac~

buffer

201

202

o..pttr 11

GaloKti( War: From Vectors to Bitmap"

.
.....................................................
thrud sttrt

tf~nt

. $tart tht fa. loop running

public woid start( I I


liMO CHAMGES HERE
/

thrud run ewent (ga. 1009)

.. .. .. ....f

public wold r~"() (


liMO CHANGES HERE

thrud $t0)9

tf~nt

.....................................................

public w01d slopO I


/ fNO CH~NGES HERE
/

.
.....................................................
..we al'd ani ute the ObJKts In the ga

prl.Ht wold gUltUpdHt() 1


//NO CHAHGES HERE

.....................................................
UpdHe the $hlp posit Ion bHK on .eloclty

public wold updateShip() 1


//NO CH.IJIGES HERE

..

.....................................................
Update tilt bullets based on welocltr

pubHc wOld updUtB~11tU() (


// NO CHANGES HERE

Improving the Game


/

.
..................................................... ,
UlXIUe the asteroids bastel on velocity

public void upd,teAsterolds() I


llltO CIIAllGES HER!

.....................................................

Test asteroids for collisions with ship or bullets

pubHc lold chec:kColllsfonsO I


IlItO tlLUGlS HERE

I .. .. .. ..

.....................................................,
hyllstener elt1lU

public void keyRelusedlKeyElent k) I


public void te)'T)'pedIKeyElent t} 1 I
pUblic voId keyPrtssed(KeyElent k) I
CHANGES HERE

I NO

I
cilculate X-o.elltnt "lue based on d1rectlon Int1e

..1

pub 11 c d(l~b 1e cll cAntl eHo.eX I d(l~b Ie Intl e)


IINO CHANGES HER!

I..
.....................................................
Cllcullte y ..,uent IIlue bned on dIrection Intle

public double c,lcA",,,ltllOleHdouble In,le) 1


IINO CIWI6ES HERE

203

204

Chapter

11

Galactic War: From Vectors to Bitmaps

What You Have learned


This was a heavyweight chapler thaI covered some very difficult subjects. Bill the
idea is to get up the most difficuh part of a hill so you can reach the peak, and
then head on down the other side. That is what this chapter represents-the last
few steps up to the peak. You have now learned the most difficult and challenging
aspeds of writing a Java game at this point, and you arc ready to start hcading
down the hill at a more leisurely pace in the upcoming chapters. This chapler
explained
How to add bitmaps to Galactic War
How to support multiple key presses

Review Questions
The following quest ions will help you to determine how welJ you have learned
the subjects discussed in this chapter. The answers arc provided in Appendix A,
"Chapter Quiz Answers."
1. What is the name of the class that handles bitmaps?
2. Which class in Galactic War detects when bullets hit the asteroids?
3. What is the maximum number ofsprites that can be supPQrted by the game?
4. Which method in the Graphics2D class aClua lly draws the image of a sprite?
5. What is the name of the Appl et method that redraws the window?
6. How llJany key presses can the game detect at a single time?
7. Wha t method do you use to track the mouse's movement?
8. Whattypc of graphics entity docs the game IISC for the astero ids?
9. Regarding ship rotation, by how many angles can the ship be rotated?
10. What method prov ides the game with support for collision detection?

On Your Own
The following exercise will test your comprehension of the topics cover~-d in this
chapler by mak ing some important changes to the projects.

On Your Own
The Galactic War game is much more playable now than it was back in Chapter 3,
thanks in part to the new keyboard handler. But we are completely ignoring a
perfectly valid altemative to the keyboard-your trusty mouse. Devise a way to
add mouse support to the game. You could rotate the ship when the mouse is
moved left or right, apply thrust when the monse wheel is used, and fire when the
button is pressed.

205

CHAPTER

12

GALACTIC WAR: SPRITES


AND COLLISION BOXES

The goal o f this chapteT is to develop a way to handle the game objects moving
around on the scr~n and to enhance the Galactic War game with some significant new gameplay features using sprites rather than just images and vectors.
Here are the $pific 10piQ you wililcam aboul:
Upgrading Galaclic War to a sprite-~ game
Adding new artwork to the game
Adding new functionali ty to the g.1meplay

Creating the Project


The Galactic War game has so mudl potential that I'm cagn to implement. but
the game has been sormwhal hobbled up 10 this point du!!' to i15 being Iimitedfirst byVCC1ol'$, then by $imple bitmap$. Now that we haVl!' this useful ~ Spr t te
das.s available with some serious functionality built into it, the game will really
start to resemble what 1am envision ing for it.
The first thing 1 want to do is enlarge the app let window to 800 )( 600. I rea lize
that 640)( 480 provides beller support for users oflow-tnd Pes, but the truth of
the matter is that most usen run their Pes at 1024 )( 763. while a similarly larg!!'
peruntage U5<' 1280 x 960 or 1280 x 1024. Only a tiny minority of PC ~ run
the system at the Iowa nsolutions. A resolution of8OO x 600 will giv!!' the game
more breathing room due to the large si:u of the aslC1'Oids.

208

Chapter 12 Galactic War: Sprites and Col lision Boxes

Figure 12.1
The new ol'l'Siorl ot Galactic War.

As you can sec in Figure 12.1, the game has been completely converted to bilmapped graphics, finally doing away with the vestiges of its vector-graphics
ancestry.

The Galactic War Bitmaps


There are a lot of high-quality bitmaps in the game now, giving it a sharp, catchy
appearance. The first and most significant change to Ihe game is that it now uses a
background bitmap instead of a blank, black background. The bluespace.png
background image was created by Reiner rrokein and is shown in Figure 12.2.
This is one example of many backgrounds, srrites, and tiles you can find in the
Reiner's THesel collection at www.reinerstilesel.de.
There are five different types of asteroids of the large variety in this iteration of
Galactic War. These gigantic rocks wi]] be blasted into smaller pieces in later
iterations of the game: at this roint, shooting one of them simply causes il to
disappear. I wantc<! to use very large starting asteroids to make the game more
interesting by breaking them up inlO many smaller rocks. The asteroids shown in

Creating the Project

f igure 12.2
The badgroood image used in Galactic War.

fig ure 12.3


The five uniqIJe asteroids featured ;n the game.
Figure 12.3 were rendered by a talented 3D artist by the name of Edgar Ibarra,
who actually created these asteroid models for a project I was working on several
years ago. I have converted the asteroid bitmaps to the PNG format using Paint
Shop Pro, as well as applied a transparency mask in the process.
The spaceship has also been upgraded significantly from the version presented in
the previous chapter. I based the spaceship on a design by Reiner Prokein and
significantly modified it to give it a more distinct look with a pseudO-3D
appearance. (Note the four guns, top and bottom.) Figure 12.4 shows the ship
sprite. This is a great-looking ship, if I do say 00 myself. I'd like to add a small fire

209

210

Chapter 12 Galactic War:

,-

and Collision Boxes

_------ ..

. "' .....l;I""
ll". b

Sprit~

~;::=

1'"

.... _

_j["ijr__

!iI._.

---....,

_.
.

~ "-f""'=":J

.~
.,

~iI

. _._........... _-

--"'._-~

fig ure 12.4


The player's spaceship used in Galactic War.

...... ......
--~

animation coming out of the engine nozzles when you press the Up arrow key to
apply thrust (fulUre enhancement!).

The New and Improved Source Code


There's a lot ofsource code here for the new version of the game. Th is is necessary
be<ause the game is now taking shape with a lot of functionality, and it has room
for new features in upcom ing chapters. Futu re chapters will actually involve
additions and changes to the monumenta l work done in t"is chapter, which is
now a new foundational vers ion of the game. The first thing I'd like to point out
is that Galactic War no longer uses the Vector$hape class, so you can remove it
from the project. [have made no changes to BaseGamefntity or l..ageEntity, so
those can rema in in the project. (I lIIageEn tity is used by the $pr1te class

Creating the Proje<t


interrntlly.) You (3n

5tt

the current slate of the project by looking at the list of

files now required by lilt' project:

8a5cG<lmeEnlily.jaVll
GabetkWor.java

IJllogeEntity.j.wa

Sprite.java

Point2D.java

Tip

n-

Two obwIoullJ ~ omossionlo


fie 9i'Rr sot.- ' - bHlllOWld tftects iRd IIMic. We wi
idd lOWld III the gaml' in ~ 15. GaLIctic war. FitIMIng the GMnf.'

The first (ode li5ting here indudes the main d:us definition for the game, along
with the global variables. r have highlighted key (hanges to the 1}1111e in bold tex!.
{

GALACTIC IIU, Chlpter 12

.....................................................
jUI.lpplet. :
jIU.I"t. :
jIWl.I"t.nent
JUI.I"t ,1"'ge
JIU . ~t 11. ':
I~ort JIVI. lang. 5Y51C11 :

l-.ort
l"POrt
l"POrt
IlIllOrt
IlIllOrt

Prl ... ry (lin for the 91

......

..

pIlblle <lass liIlIelldlar extCllds ,l.pptet llIIllnents RllMlble. teyl1stener I


IIglobll (OIlS tints
stlt Ie Int SCIEUlIlllTll - 8lIll:
stille Int SUHIHEIGIlT - 600:
stille Int CEITERI - SCREEllIIlllTlt / 2:
stltle Int CEIflERY- SCREUHEI6HT /2:
stltle Int ASTEROIDS - Ill:
stltle Int 8UltETS - 10:
stltl c Int 8UltECSPEEO -.:
stitle dOUble ACCELERATION - O.OS:

211

212

Ch~ptt r

12 Ga lactic

W~r :

Sprites and COllision

80~ts

I/$prite st.te .. 1ues


SUtlc lnt SPlllTUORllAL 0;
lutlc lnt SPlllTLCOLLIDEO-I:
lithe IIllln thrUd MC_S tl\t g loop
ThrUd gloop:
IIdouble buffer objects
Buffertdlllllgt b.c\buffer:
Gr.ph1cs2D g2d:
IIVirl0US toggl es
boo I t.n shOllBounds - true:
boole.n collh10nTestlng true:
IIdeflne the game objects
I. agtfntlty background:
Sprite ship:
Sprl te[J ast * nt~ Sprltt[ASHROIDS]:
Sprl tt[J bulltt new Sprl tt[BULUTSJ:
Int current8ul1et - 0:
Ilcreate a randaa n~r gener.tor
II..1ndaa ral'ld _ II..1ndaa():
IIde fine the SOIII'Id Hfects Objects
So"I'I<IClip shoot:
So"n<!Cllp uplode:
IIsllIple ~.y to h.l'Idle ..1t lple keyprfssfs
boolean keyDolm. ktyUp. ktyLeft. ktyll.lght, keyFlre:
Ilfrav. rate (ounter

lnt frav.Count - D. frlv.Rltf - 0:


, ong s t.rtTille - Sys te currentT l11tlll111 s ( ):
Thc sho~Bounds and colli s 1onTest I ng va riables arc used to draw some helpful
information on ,he scrcen, which is invaluable while develop ing a complex
S'lme. Figu re IZ5 shows the game with bounding bo~es and collision testing
turned on. When a collision occurs, the bounding boxes of the two objects arc
drawn in red instead of blue. You can turn ofT collision testing altogether, if
needed for testing.

Cruting tho! Project

Figure 12.5
80lIlding boxe and mIisions are IoggIfd with Ihr Band C keys.

This brings up an imponanl point aboUIIM currenl Slate of 1M game. ~ au


a lot of new featu res in the game, and il prt'lty much look$the WoIy it will whl'll il
is finished-acept for a scrolling background and a few olhorr tidbits. The game
really docsn'ttake any action atlhis point based On a collision. Instead, collisions
arc dt'l<.'Cted, and that status information is made available 10 the game through
the sprite's state property (which is gmmc enough for use howt'Ver you see lit).
Welll't'Visit tilt' prQCt':SS of responding 10 rollisions in Ihe nal du.ptlT.
Next up is the In It () t'V<."Il1 mt'Ihod of tM applt'l:.

.
.....................................................
/

applet lnlt event

publiC ~old lnlt() I


Jlc reate t~t back buffer for s_t~ ,upIl1C$
backbufft.""", s..fftredlugt(SC REUlIlIDTM. SCRHlOIEiGHT.
Bufft.edlugt. TYPL,.r_RGlII:
,Zd backbllffer .crUltGrilp~ tu():

213

214

Ch apl~

12 Galactic War: Sprites and Col lision

8o~es

Ifload the bukg round iMge


bldg round - IleII IMgeEnt a,ltM J) :
bldground.l~dl-blunplce.png-I:

Ifse t up the ship


ship new Sprite(UIs, g2d1:
shlp,lold("splcnhlp.png-1:
shl p, setPos i t 1onlnev Pol nt20l CENTERX, CENTERY):
ship, seUllve(true):
Ifset up the bulleh
for (l nt n - 0: n<6U llET S: 11+ +) I
bull el(n} - net' Spri t t l thl s, g2d1:
b\llltUn]. 1ol d(p hs.ullot. pIlg-l:

II set up t he I steroids
fo r (lnt n - 0: n<ASTEROIOS: 11++1 I
ut[n] - new Sprlte(tM s . g2d):
ut[n]. seUllveltrue1:
IIlold t ne asteroi d l..ge
int I- rlnd.lteJU.t(S )+l:
ut[n]. lod l -asterold" + I + . pd'- ) :
Ils et to I rlndOOl position on th e scree n
l"t J - rand .neJ tln t (SC REE HW IOTH) :
int, - r and.nut1nt<SUEEH~EIGHTl:
ut[n].setPositionlnew Polnt211(K, y):
IIs et .0Ution angles to I rlndOOl ' I lue
ast [n) . set rIc eAng1e( ri nd. next Int< 360) ) :
u t [n). setllOveAn, le( rand .next [nt (360) ) :
1St [n}. setRottt i onRi tel rUd .nutOoub Ie( II :
IIset velocity bued on -a.eaent di .ection
double I ng - H t[ n} .-a. eAng leD gO :
doubl, veh ClltAnglellcl.el(lng):
dOubl, vel, - uIW,ltMov'Ylangl:
ut[n), set Yel oci tr(nell Point20(ve lx, vely)) :

IIstirt th, user input lhtener


Idd t eyll stener( t hi s) :

Creating the Projea


The next sedion of code is the main game update ponion of me game, including
the applct updateO ('Yenl, the palntO event, and three methods to draw the
game objects: draw$hlp(), drawBull eU(), and draw~~terold~().

, applet

~p'Sate

ewent to redraw the screen

.....................................................

/
.01d updlte(Grlphlu gIl
IIcalculate frlM rHe
frIMCount++:
1f (SystM.currentT1MHll1 1$0 >stlrt1,.. + 1000) {
stutT1M - Systee.currentT11ol!111111s0:
fUMbte frllltCouot:
frl.counto:

p~bllc

Iidraw the backgro~nd


gld. drawluge( background. geUuge( ) 0,0. SCREENWIIlTH-I.
SCREENHEI&HTl. tbls):
IIdrlw the gl.. gnplllcs
drlwAsterolds( J:
drawShlp{):
drawBullets() :
IIpr1nt stltus lnfoNlltlon on the screen
gld .lietColorCColor. WHITE 1:
gld.drlwStrlng("fpS: W+ frlMble. S. 10):
long X - Mlth.round(shlp.pos1tlon(J .l()):
leng)' - Mltb.round(shlp.pos1tlon(). V()):
g2d.drnStr1ngC"Shlp: w+x+".w+ y 5. 25):
g2d.drlwStrlng( "No,e Ingle: " + Nlth. round(
sh1p ....wgleOl+i0. S. 40):
gld.drlwStr1ng(Whee ang)e: "+ Math.roundC
shlp.faeeAngleOI, 5. 55):
If (showBoundsll
gld. setColer(Celor .GltEN):
g2d.drlwStrlng('BOUNOING BOXES'. SCREENWIOTH150. 10):

215

.216

Ch.a ptH 1.2

Gal~

War.

5pril~

and Colli$ion !lofts

if (tolll$lOl1Testlngll

gld. setCo1or(Co Ior . 6REERI :


gZd . d r~WSt rl ng( 'COll [51 O ~ H5 TI MG' 5CRE MII IOTH ' 150. 25) :

IIr~p~lnt

tM

~pplU

.lndow

p~lnt(gl :

dr~wSh 1p C~ 11 ~ d

by

~ pp 1et upd~ te ~vent

" ' 1
publlc void dr~wShlp(J (
II let the tr ~nsfOrll for the lnlge
shl p. t rlns fOrll( ) :
ship.dr~w(J:

If (showBounds) I
1f (5hi p. stltt( ) SPRITl..COLlIOEo )
s hi p. drlw8ounds ( Co lor. REO ) :
else
shi p. drllf80unds (Co I or . 8LUE I :

.
.....................................................
drlwhllets ulled bYlpplet updlte ewent

public void dr~ w Bullets() I


for (lnt n 0: n (8ULLET5: II-H) t
If (bullet[n).~lln()11
Ildr~w thebull~t

bull et(n]. tnnsforn( ) :


bill I etC n) .dnw( ) :
if (shadounds) (
1f (bull et{n]. shte() .- 5PRITl..CoLLloEO)
bII1Iet(n) . drawlounds (Col or . 1E0 I:
else
bu I let[n). drlwBounds( color. BLUE) :

Creating the Project

..................................................... ,

dr~wAsterojds c~lled

by

~ppltt

updUe event

publ ic .oid dnwAsteroids() I


for (Int n 0; n < '!'sTEROIDS; I"H) I
If (ut[n].llheO) (
IIdr,w t~e uterold
ut[n}. tr!nsfor.();
Ht(nl.dr! w() ;
if (sbowBovnds) I
lf (ut[n].stlU() SPIITE-COLLIDEO)
ut(n]. drlwBovnch(Co lor .IUD) ;
else
ute"] . drlwBounds( Co I or . SLUE) ;

.
.....................................................,

~pp)tl

window

rep~lnt evenl-dr~ w

tbe b!ck buffer

publiC .old p'lnt(Gr~p~lcsg) I


g.drlwl..ge(blckbuffer. D. D. tbls):

or

The next section code updates the gamc via thc g~lIIel oop thread, which calls
g~IleUpdate( l. Th is method, in turn, calls methods to process user input; update
the ship. bullets, and asteroids; and perform oollis.ion testing.

.....................................................,
tbrud sun event - stlrt tbt g,llt loop rUMlng
public void sllrt() l
g'lltloop - _ Tbrud( t hls);
gult 100ll. stirt( );

I
.....................................................,
publtc .01d run() I

211

218

Ch.Jptl!'l" 12

Ga~ic

W lK: Spntes and Col lision Boxes

lI acqu1r e t he cu r rent thru d


ThrUd t Thread . cur rentThreld{);
//kp going IS IO/Ig IS t~ thead 1$ 111 v,
" hlle (t gl.loop) !
tr1!
Threld. s Ieep( 20} ;
I
CltCh( Inle rruptedhcep t Ion ,) [
e.prlntStlcU r.ce( ),
I
f/updue the gl. loop
gl.Updlte() :
repllnt O :

.....................................................

t hru d sto p event

public voId st op() I


gl.loop - null;

........f

private void gllieUp dlUO


checklnput() :
IIpdite$hlp();
\Ipdlte81l1Ieu() ;
updateAsterol ds( ) ;
if (colli slonTest! ng) checkColl1slons();

..

Updue the shIp pos! tlon bued 00 velocl t1

.....................................................
public void updl teShlp() I
aM p.llpdltePos Itlon ( I :
doubl e lIevlt sM p. pos ltl OII( I .X( I ;
doub11 newr. ship. posl tl OII( I. Y( ) :
" " riP Hound leftlrlght
If (shlp.poslt l on(). XO < ' 10)

Creating the ProjNt


_
- SnEOIlIIOTK + 10;
else 11 (Ihlp.posltlon( I.I( I
nft)[" -10;
lI.rlp uound

>SCIEElllllOTH + 10)

top/bott~

< -10)
nellY" SCRHMHEIGilT + 10:
else 11 (shlp.pOlltl0n(). YO >SCREEMHEIGilT + 10}
ntlf)''' -10:

if (lhlp.pos1tlon().YO

s1l1p.let'011t\ontHW '0Int2D(HWlt.
ship. utStlte( S.IITEJIOIIIo\L):

1lfW)'J):

I
.....................................................,
Update the bullets blled on veloclly

public Yold updHeBullets() I


IIWlYe tile bill I ets
for (lnt n -0: n (&uUETS: rt++) I
If (bul1et[n!.llhe()}I
lIupdate bullet' I x PQs It Ion
bull et En]. up.:IItePos1t 1on ( ) :
IIbullet dhlppurs at leftlright edge
11 (bullet[n].polltlonl).It) < 0 II
bullet [n]. posl tl on I ) .It I >SCREElIVl DTH)
bullet[n).uUl1wetfilse) :

lIupdate bullet's y position


bull et[n] . up4ttePol 1tl one ) :
IIbullet diupptars It topfbottol edge
If (bullet [n].pos ltlon ( ).YO <0 II
bullet(I].posltlon{ ).l{) >SClEEIlKEl6HTl
bul let(n].seUlln{fllse) :

bu I \ et[n] .s'tSti let SPRITUORHAL l :

219

220

Chapter 12 Galactic Wilr: Sprites and Collision Bo~es

,
.
.....................................................,
UpdUe the nterolds t>.sed on .elocHy

public .01d

~pdntAsterolds() I

1I~.e

,ncr route the nterolds


for (lilt n - 0: n < ASTEflOIOS: tt+ +) I
if hst(nl.JlheO) I
lI~pdne the n t erald's position Jncr rau l 10n
ISt[n]. opdettPos It 1on e);
ISt(n). apdlteRout ton ( J :

1nt II ut(n] .1MgellldtMJ- l;


tnt h - ut(n] .IMgtHelghUJ - l;
do~ble ntwX ut{n] .poslt10n( I.I( I:
do~ble n~ - ut{n] ./lOsl t10n(). Y():
I/.,ep t he ~st e r old ~ ro~n d the sc reen edges
if (ISt[ n] .poslt l onf).If)

< '11 )

newx SCRHHWIOTM + w:
else if (lSt{n].pos1tionf).X{) >SCRHHIIIOTK+II)
newx ow;
1f (ut[ n].poslt10n O .YO (-h)
newy- SCREEWKEI&HT + h:
else 1f (ut(n] . poslt1onO. YO > SCREENKEIGKT + II)
newy -h:
ut{n].setPosltlon(new PolntlO{ntwX.n~):
ut(n] setSute( SPIIT[JIORlIAL);

Test ut'ralds for co III s 1OfIS with shl p or

b~ llets

.....................................................,

pobll c .olel checkeo III s Ions Cl I


IIcheck fo r coll 1$100 bet ~e n nteral ds end bulle ts
for (Int _ 0: -<ASTEflOIOS: ... +) ,
1f (1St[.j . Jlhe(JJ I

(rnling the ProjKt


III ter,te

t"ro~g" the bill Ieli


for (lnt n ~ G: n (WHETS: ,.... ) I
If (bullet[n] lheO)I
IIcolI Ision?
if (ut[_].collldesWltMbull,t[n]l) I
bu II't[n]. setSUtfl SPRI TLCOLlI DEG):
ut[_]. utStateISPRITLCDLlIDEDl:
explode . play() :

IIclled for collisIon ilsterolds ilM sMp


for (int _ - 0: -<ASTEROIDS: .. +) I
if lilSt[_].illheO) I
if

(i~1p.collldeIW\th(i1St[_j)l(

te_]. setStlte( SPRITLCDLlIDED) :


Ih1 p, ietStitel SPRITLCDLL I OED} :
e~plllde .pl iI1( l:

iI i

The next SKlion of rode processes keyboard input. The game has prog=sed to the
poinl where lhe limp li~ic keyboard input from earli~ chapters was insufficient, so
I've added support (0 the game for multiple key presstS now. This works through
the use of 5e\lef3l global variables: key Left, keyRight, and so on. 'These boolun
variables are set to true during the keyPressedl) event and set 10 false during the
keyReleilsed( l event method This provides support for multiple keys at the same
time in a given frame of the game loop. 1lIcre is a practical limit 10 the number of
keys you will be able to pres at a time, but this code makes the game fluid-looking.
and tbe input is smoother than lbe jerky input in the IasI: chapter.
/
process keys tllilt line been pressed

.....................................................

publiC .old checklnputO I


if {keyLeftl {

221

222

ChiJpter 12

GiJliKtic WiJr: Sprites iJOO Collision

Ill eft arrow rotltes

s~lp

8o~es

left 5 dlgrHS
5):

shlp.utFaclMgle(s~I,.faceAngle
O

If

(s~I,.faclAngh()

<0) s~lp.setflclMgh(360- 5) :

I
e1le11 (h,R1ght) (
IIright urow ralites ship right 5 dl9rlls
shlp.utFaceMgle(shlp. flceAngle() + 5):
11 (shlp.flceAngle() 360) shlp.setFlCeAngle{5):

I
11 (ke,Up) (
lI~p arrow applles
applyThrutfJ :

thr~st

to shlp

I....
.....................................................
ke, 1 I stiner nlllu

publlc .old ke,TJpe.dfh,E.enl k) I J


publ lc .old kI,Preued(le,r.ent kl I
switch (k.getllJtGdeOl {
case te,E_ent. Vt..LEFT;
ke,left. true:
br eak :
clse leyEvent.~ICR1GHT:
ke,RIght true:
break:
case KeyE.ent.Yt..UP:
kI)'IIp trill:
break:
case Ke,hlllt. Yt..CO.TROl:
kI1Flre-tMII:
break:
case Ke,hent.Yt..B:
IItoggle b""rHllnQ reclinoles
showBoundslshowBoundl:
brelk:
case KeyEvent . ~t..C:
flloggle col11slon tutlng
colli s 10nTesting - Icol1 I 11onTestlng :
break:

Creating the Project


publtc votd hyReIUStdlleyEvenl~) I
switch It.getleytodeO) (
cue KeyEvent. YK.-UFT:
teJ'left - hlu:
brut:
cue [eyE vent .YK.-RI6IIT:
teJ'Rlght - f'he:
break:
cue Keyhent.YK.-U~:
keyUp-hlu:
break:
case KnEvent. ~K.-COHTROl :
hyFlre-f,he:
flreBullet():
breat:

public vold applyThrustl) I


Ilup arrCl'll adds thrust to shtp (1/10 no ....l
shlp.setlillnAngll(shtp.hceAnglel)- !MIl:

SJll!~)

IIcalculate lhe I and 1 nloclty based On angle


double velx - sht p. velocttyll. Xll:
velx +- calcAngleHovd(shlp.lKlveAngle()) ACCELERATION:
double vely - ship. veloct ty(). Yll:
vely +- calcAngleHonHshlp ..anAngleO) ACCELERATION:
shtp.seUeloclty(new Polnt20(velx. veI1:
I
publ1c votd flrtlulletl) I
IJflreabullet
e1lrrentBull et++:
lf (cur renUul let >BULUTS - I ) currentlul1et - 0;
bullet[ currenUullet]. setAll ve( true );

IIset bull et s start lng pot nl


lnt . - bullet[currenUullet].lugelltdth();
lnt h - bull et[currentBullet].lugeHetght():
~oub lex - sht p. centerO Xll - ./2:
doubley-shtp.centerO.YO - h/2:
bul l et[currentBull etl. setpos t t lon( new Pot nt20(x ,y) ) ;
llpotnt bullel tn u. dt rtton shtp t s faclng

223

224

Chapter 12

Galactic War: Sprite<; and Collision Boxes

bull et[ cu rrent8ull et] .set FaceAng1e( shl p. hceAng1e( ) };


bull et[ cu rrent8ull et] .setMoveAng1e (shl p. faceAng 1e() - 90);
l/fire bullet at angle of the ship
double engle - bu11 et[ current8u11 et].-.lveAng1e ( ) :
doubl e nx ca1cAngleMo~ex(angle) 8ULLET_SPEED:
doubl e ny calcAng1eMoYeY(angle) BULLET_SPHO:
bull et[ cur rent8ull et]. setyelocI ty( new Pol nt2D( lVll. svy)):
IIphy shoot sound
Shool-play():
The last section of code concludes the main code listing for this new version
of Galactic War, implementing the now-familiar calcAngleHoveX() aJld
ca IcAngleHoveY(} methods.
1 ..

**..............

Angular motion for Xand Y is ca lculate d


.................**
0*
*0
public double calcAngleHovex(double angle) (
doubl e IlOvex Math .cos(angle Hath. PI I 180);
return llO~ex :

I
public double ca1cAngleHo~eY(double angle) (
double llO~ey - Hath. sin( angl e' Hath. PI 1 180):
return llO~ey:

What You Have learned


This signifi<:cont chapler produced a monumental new version ofCalactic War
that is a foundation for the <:chapteTS to (Orne. The final vestiges of the game"s
vl.'<:tor-based roots have been diocarded, and the game is now fully implemented
with bitmaps. In this chapter, you learned:
How game entities <:can be(Ome unmanageable without a handler (su"h as
the Sprl te class)
How the use of a sprite dass drnmatiall1y deans up the sour"e code for a
game

On Your OWn

Review Questions
The following queslions will help you to determine how wdl you h~ve kamM
the subjecls discussed in litis chapter. The answns are provided in Ap~ndiJ: A,
"Chapter Quit Answen."
I. Which support cbss helps maragt' the position and velocity of sprites~
2. During which keyboard ~t should you disable a ky.pras variable when
ddling multiple ky presses wilh global variables~
3. What is the IlOlInt oflhesprite: collision detection routine: used in Galactic War?
4. Which method in the App1et class provides a way to loo.d images from a JAR fila
5. Which Java package do yOll need to imllQrt to use the Gr.phics2D class?
6. What numeric dat a type docs the Polnt2D cbss (created in this chapter) usc
for internal storage of the X and Y values?
7. How does the usc of a class such as POintlD improve a game's source code.
\'CT5US using simple variabla?
8. Which pro~ny in the Sprite class determines the angle at whKh the sprile
will mova
9. Which property in the Sprite class de'lermincs the angle at which a sprite is
pointed?
10. How many milliseconds must the game usc as a deby in order to achieve a
frame rate of 60 frames per second?

On Your Own
TheGalaetic War game is in a transition at this llQint, after having bemllpgraded
significmtly from vector-based graphics. At present, it does not perform any
action due to coll isions other than to rCllQrt that a collision has occurred. We
want to separate the roJJjJjoll ICJlillg code from the {olliJioll rCJpo"J(" code. Add a
mdhod that is called from g.-eUpdau( l that displays the position (x,y) of any
object thaI has collided with another objecl, for debugging purposes. You can do
this by looking al.ll sprite's state properly.

225

CHAPTER

13

GALACTI C WAR: SQUASHED


BY SPACE ROCKS

We'll make a minor enhancemen t 10 Galactic War in this chapter by adding


suplXlrt for an animated explosion. To facilitate tbis, we'll have to write wme
new code to bandle the timing and state properties for eacb sprite. The goal in
this chapter is to add a handler for responding to ship-'lsteroid collisions. When a
collision occurs, the game should animate an explosion over the ship, and then
put the ship into a temporary invulnerability mode w the player can get the heck
out of the way before another collision occurs. Here are the key topics:
Examining possible interactions among the game entities
Adding collision detection between the player's ship and the asteroids

Being Civilized about Collisions


Responding to collisions in a civilized manner is the first step toward adding
realist ic gameplay to any game. The next step is to add a sense of timing to the
responsc code. We'll add explosions to show when the player gets hit by an
asteroid! The new animated explosion in the game is a 16-frame sequence, which
is shown in Figure 13.1.
When the explosion code is addc<1 to the game, the ship will literally blow up
when it collides with an asteroid (see Figure 13.2) .

228

Chapter 13 G<l lactk War: Squ<lshed by Space Rocks

Figure 13.1
The ll;frame an imaled explosion (courtesy of Re<rler ProkeinJ.

Figure 13.2

TI>e new Mimaled explosion

now part of the game.

Being Civilized about

Colli~ion~

l.cl's lake a look al Ihe changes required 10 update lhe game 10 supporl explosions. There's more involved here than just loading up the animalion and
drawing il because we have to accoun l for timing and Sprile Slaw values. The firSI
change is in the globol se<:tion at the lOp ofl he cbss-notc lhc changes in bold.

Tip
!hi' s Uti c keyword defines a variable thai does nOi change.

pJbl Ie closs Gol act Ic'o'ar extends Applet l",ple..ent~ Runnoble, KeyLI stener I
Ilglobol constants
stotic Int SCREEH'o'IDTH - 800:
stotie Int SCRHHHEIGHT - 600:
stotle lnt CENHRX - SCREENWIDTH I 2:
stotte Int CENTERY - SCREENHEIGHT I 2:
stotle Int ASTEROIDS 10:
stot Ie int BULLETS - 10:
stotle int BULLECSPEEO - 4:
stot Ie double ACCtLEJU..TlD~ 0.05:
Ilsprite state nlues
statle lnt STATE.-HDOOL - 0:
stltle lnt STATE.-CDLLlOEO - l:
stl tl e I nt STATE.-EXPLDOIHG - 2:
/ltne ",ol n t~reod beco"'es
Thrud gOJIICloop:

t~e

gaM loop

II daub1e bu Her obj eets

Bu f fered I..oge boekbu ffer :


Gropnl (s20 g2d:
I/variou> toggles
booleon s~ollBounds - true:
boolean eollhlonTestlng - true:
long Colli ~lonT\-er" 0:
Iidefine l~e golllC objects
laag eEnt1ty boekground:
Sprite ship:
Sprtte[] aSl - nell Spri!e[ASlfROIOS]:
Sprl t ell bull et - nell Sprl te[BULLETS]:
lnt currentBul1et - 0:

229

230

Chapter 13 Galactic War: Squashed by Spa<e Rocks


Anl ... tedSprlte uploslon:
IIcrute ~ r~nd... n~r ~tntritor
RInd ... rind - nl'V Rlo.d...( I:
Jso~nd

eftech

sa~nd(llp s~oot;

saO/ndCI Ip fXplOCle:
Is111ple "Iy to ~irdle ..,ltlple ~eyprnses
bOOlun keyllo1o key",. <eyu~ft. keyRlgM. keyflre:
I l f u . rite CO<lnters.n4 ot~er thing ur1lblel
1"1 fr .co~"t - O. fr Rate - 0:
10"~sUrtTl.-Sy$t..... eurrentT ... 111s1

Makc thc following changes ncar the top of the lnltl f'\ent method to s"itch
the background ob;e.::t from an l.. ~e to an 1... ~e(nt1ty.
pubHc wold ,nit I
IlcreHe Ve back buffer for slIOoth graph1 cs
baekbuff er - ntll Bu ffered [",age( SCR[[~ W! OTH.
Bufferedl",age . HPCINCRGB):
g2d - backbuffer .creHeGraphl cs[) :

SCR[[ ~~[

IGHI

Ilioad tht background laage


backllround .. new l.ageEnt t Iy( thl I) :
background. load (~b I utspace. png~) :

1\ext, scroll down inside the Inl t() l"'o'cnt mctbod a bit more until you ha"e
found the call to iddl(eyllstenerl thl s) and insert thc follo"ing code in bold.
This code lQ;lds a 16frame explosion animation.
1110.d the upTosloo
uploslOll" net' MI ..tedSprlte(thh. 92d):
uploslon.lo.d(~e.lflToslon9'x9hl'.JMlg~.i. I. H. HI:
upl ollon. utFrI.rN!117(2):
upl 011011. uU Tl.tl It I se) :
IsUrt Iheustr input listener
Iddlcyl Istener ( th \

Being Civililed "bout Collisions


;-<ext, go to the updat e() event and look for the line of code Ihat draws the
background and make Ih.. change noted in bold. Then, a few lines further down,
add the new line of code shown in bold.
Ifdr~ w

the

boc ~ ground

g 2d . dr~wI,uge (ba ckgro und

getluge().O .O. SC REE NWI OH-I. SCR[E NH EIGHT- I .

this) ;
!ldrawthe gUM! gr ophlCS
drawAsterolds() ;
dr awShi p() ;
draw6ul l ets() ;
drawhploslons() ;
Now, while you're still in the update() method, scroll down a few lines to the part
of the method that draws stalUS information on the screen and add the following
code to display the ship's current stale.
tf (shtp . SUH( )- . STATLNORlV.L)
g2d .drawStrlng("Sute : NOll.MAl", 5, 70);
else t f (shlp .sute( )"STATLCOLLIDEO)
g2d .drawStrtng("Sute : COLLlOEO", 5. 70);
else If (shlp .stlteO"STATLEXPLODING)
g2d .dro wSt r lng("Sute: EXPLODING", 5, 70) ;
Now, scroll down past the three dra w methods and add lhe following O1<,thod
after drawAs l erold s(}. This is just IIII' first version of the explosion code, and it
only draws a single animated explosion. later, the game will need to supporl
several explosions al a time .
pJbHc void dr awEx plosiollSO 1
If expIos Ions don't need sepHa t e updne llC thod
i f (expl os ion ,a l l ve( }) I
exp 1os Ion ,updateAn I-at Ion{ ) ;
If (expl osion . cu r rentfr~ me() -- nplos Ion. tot al Frames( )1) 1
expI os Ion. se t Cu rren t Fr ame( 0) ;
explos 10n .set AI I _c( fal sc) ;
I
else {
explos lon.draw() :

231

232

Chapter 13 Galactic War: Squashed by Space Rocks


Next, scroll down a bit more to the gameUpdate() method. Modify it with the
additional lines of code shown in bold.
void g~meUpdateO (
checklnput():
upd~teShlp() :
upd~teeullets() :
upd~teAs tero1ds ( ) :
if (collhlonTestlng) (
checkCo111 s1on$( ) :
h~nd1 eSh1 pCo111 $1 on$ ( ) :
hand1eBu11etColl15ions() :
h~nd1 eAster01 dCo111 s1 ons ( ) :

prlv~te

Scroll down just past the checkCol1 i si ons() method and add the following new
methods to lhe game. The two collision handler rOlllines for asteroids and hullets
are not implemented yet, as the goal is first to get a response for ship-asteroid
collisions along with an animated explosion. The handl eSh1 pCol11 s1 ons()
melhod uses the ship sprite's Slate property extensively to monitor the current
state of a collision, and il adds a thr~-sccond delay after a collision has occurred
so Ihe player can get out of the way before collisions slart 10 occur aga in.
public v01d handl eSh1 pCol115ions() 1
1f (ship. state() ~ ~ STATCCOLLIOEO) (
1'0111 slonTllM!r - Systelll.current11aeMll l l.1():
ship . setYeloci ty( new Polnt20(O ,0)):
shl p. setS tate (S1ATCEXPLOO IKG) ;
startExpl oslon(5hi p):
I
else if (sMp.st~te() -- STATE_EXPLODING) {
if (coll1s10nTiaer'" 3000 <Systell.currentTi . eMl1lls{)) {
sM p. setSu te (STATLNORMAL) :

public void startExploslon{Sprlte sprite)


if{!exp10s10n.~lhe())(
double x - sprite .posit lon(). X() .
spr1 te. get80unds ( ) wi dth I 2:
double y - sprite. posit 10nO .lO .

Review Questions
sprl te . getBounds ( ) . heigh I f 2,
explosion .setPosl t lon(new Polnt2D(x. y));
exp1os 1on .setCu rrentf rame( 0) ,
explosion .setAII ve(true l,

public void handleBulletCoI11s1ons() (


for (lnt n 0: n<BULLETS, nH) I
1f (bull et[n] .state() STATCCOLUDED) (
Iinothing to ~o yet

public void handleAsteroidColll s1 ons()


for (lnt n -0 : n<ASTEROIDS; nH) (
I f (ast[n]. sute() STATCCOLLIDEO) 1
Iinothing to ~o yet

What You Have Learned


This chapter tackled the difficult subject of sprite collision detC<:tion. Adding
support for collision testing is not an easy undertaking, but this chapter provided
you with the knowledge and explained the collision code in the core classes that
makes it possible. You can now dmw two or more sprites on the screen and very
ellsily determine when they have intersected or collided. and then respond to
such events.

Review Questions
The following questions will help you 10 determine how well you have learned
the subjects discussed in this chapter. The answers arc provided in Appendix A,
"Chapter Qui? Answers."
1. What is the name of the mcthod thm makes collision detcction possible?

2. How many collisions can the game detect within a single update of the game
loop?

233

234

Chapter 13 Galactic War: Squashed by Space Rockl


3. What would happen if the ship were to fire a projectile that "warps" around
the screen and then hits the ship? Would a collision take place? Why or why
not?
4. What should happen to the player's ship after it has been destroyed by a
collision with an asteroid? Describe a better way to "respawn" the ship than
what is currently being done.
5. What type of transform could you apply to the explosion sprite to change its
size?
6. How docs the ship's velocity affect the result of a collision when the ship is
destroyed? Should the ship continue to exert momentum even while
blowing up?
7. How can the collision routine be improved upon, so that collisions are more
precise?
8. What is the name of the constant applied to the ship when a collision has
taken place?
9. What is the name of the method that updates a sprite's animation sequence?
10. What is the name of the method that handles the game loop for Galactic
War?

On Your Own
Since we are constantly improving the game with each new chapter, there is little
you can do now that will nOI be addressed in the next chapter. However, some
improvements can be made now that arc not added in future chapters. Here is
one such example: Add another explosion to Galactic War so that the asteroids
blow up like the player's ship when they collide with the ship.

CHAPTER 14

GALACTIC WAR: ENTITY


MANAGEMENT

You have le;mlCd lhe basics of web game programming in previolls chapters, ;lIld
you have bem building the Galactic War game here in ParI Ill. The source wde
for an applet-based game is becom ing a bi l ledious at this poim. You've seen that
there is a lot of code lhat does not change very much from one game 10 lhe next,
now thaI you know how 10 write a typical game in Java. Arm'l you gelling tired
of steing the kqand mouse handlers io I:'Icrycode listing? [sure am! I don't want
to enfolU' too much structure for your own game projects. but I think it will ~
hdpfulto add some organization 10 Inc code.

lkre arc a lot of e--ents and methods that mUSl: ~ caIJed and monilored nogubrly,
and sina this code doesn't change very often (if C\o~). it woukI clean up the sourcc
~ considerably if we could move reusable code inlo a separate class. This
chapter shows you how to create a base dass for an applet-based framework-or
r:lther. a gailit' mgine. You win ~ able to writea game very easily by inheriting from
the new-G' 1Ie class. and your game won'l nred to implemenlthe interfaces (such as
Runnlble) any longer. Instead, you will be able to focus 00 high-level game design
and gameplay. Hcre are the specific topics you will learn about:

Evenl-driven game programming


Creating a Java applet-based game engine
Interna lizing the sprite handler
Adapting Galactic War 10 the new game engine

236

Chapter 14 GaliKtic War: Entity Management

Adjusting to EventDriven Programming


The biggesl obstacle 10 the adoption of a game framework, or a game mgine, is
that you must give up direct (active:) control of the game and accept an indirect
(passive) programming methodology. Adapting Galactic War from a direct In a
~iY(' game: was very challmging. and I woukllikc to share my apcrimce with
you so that you will 001 haY(' 10 go through !he pains of developing your own
game mgim- the hard way. I could have compk1cd the game two or three times
over in the time I'Y(' spmt building the ~. das$ in this chapter, bul the md
result is a powerful mgim- for creating additional games for ddi~'Cry on the web.

Exploring the Class Library


l.et's take a look althe dasslibnlry as il exists at this point, after the changes made
in the previous chapter. Figure 14.1 shows a diagmlll of the four main classC'S
we've developed so far. The diagmm doesn't show the YectorEnt I ty class since il
is no longer being used.
First, BneGulI!Ent I ty contains the basic movement and orientalion variables that
are passed on 10 the l. . geEntlty class. This class adds the capability to move,
rotate. and draw bitmap images, and it is the core class. From this point, a new

Image Entity

-
--

---

..... _- ~~~~-
-::":"_!~=;,=-=--

FisJure 14.1

The ku key .liYa ~ b -*ing 'Mlh

Sprite

--,--

AnimatedSprite

giIllt ~

AdjlMing to

~venH)fMen Progr/lmmlng

class nlled Sprite was dtveloped. Now, Sprite does not inheril from llUgeEntity, but r~ther, it uses th is core class to store its internal image used for
drawing sprites. That is why the link from I.age[nt I ty 10 Sprite is a doned line.
Nat we h:n~ Anh.,tedSprl teo which is the core of the game engine due to iu
support for animation.
When you look at this diagram and resoIwo the connec:tiol1$ in the l"CVC'l'Se dirtion.
you find that AnllUtedSprl te inherits (rom Spd t e several key properties: an image,
width, and height. AnlllltedSprl te also makes usc of an IIlIQeEntity to handle the
brge, tiled images containing frames of animation. The Sprite class li~
looking backward, ronsumes an image and its oridth and height propmies. You
can use this diagram if you ever stan 10 feel over-..iIrimed while perusing the
Gillactic War souoce oodc: ill these: final chapters, because the game is becoming
lighter. When an AnllutedSprlte loads its souoce billn~p, that is passed up to
the "supeT" or "parent" class (Sprite), which handles the image. This is inherilance
at WOl'k. bcause the AnINtedSprite cbss doesn't actually need to handk iu image
...11
[ use the word "tight" to dl'$CribC' the situation in which the source code is not
becoming blQtlted, as is often the case when ~ game becomes more advanced .
Instead, Galactic War will evolve from a d irect. actively coded model 10 an
indirect, passively coded model using the game engine dtveloped in this chapter.
The source code listing for the game isabout the Arne length as il was befOrt, but
the game has ~n comp/tlely rt:Writtnr. The Ga. ctass is quite complex, but the
"front end" or "user" source code file, GalacticWar.java, is much, nIl/ch simpl",.
That is the benefit of a game engine-it handles all of the messy details for you,
UIowing you to focus on building gameplay. From the engine's point of view,
"You art too skilled to be hotherftI wilh $UCh minutiae as sprill' IN.nagement.
le1 me take care of that for you, while you focus on making this game u fun u
possible!"

Building the New Ga.e Class


The purpose of the game engine is to mcapsubte the p/mform code on which the
game is developed. In this case, we're talking about a lava applet that runs in a
web browser. So, the goal of this chapter is to build a game engine Ihat simplifies.
buildi ng web games. with Java. The first step is to creale a new class. I opened the
Galactic Warprojt from the last chapter and began modifying il. Firsl, I crealed
a DCWclas.sa.Ued ~1Il'. The 6 ctass alends (or inheriu from) the Appl@tclass,

231

238

Chapter 14 Galact.i( War: Entity Managel1lf'nt

as our programs have fOt the last 13 chapters. This new dass will nd to
implmlent the Uybnard and mouse listener intedac:n. 11 wiD also neal to handle
the game loop thread on its own, so the derived game will not need to be
bothered with mcll details (or rather, logistia, sinee we'n: trying to INmige the
Iogistia of an app~).
Enapsulating a StiJndatd Java Applet
Hen: arc the lNin events that you'n: aCCU$tome(! to stang in a Java app~-bastd
program up to this point. I'm including the mouse events because they are going
to b< part of the engine, even if we haven't used them very much in Gala(!ic: War.
You arc wekome to add mouse support if you wish; I'm iust not sure how you
would control the ship with a mouse.
pubHc
pubHc
public
llubHe
pubHe
p~bllc

/lIIblle
publlc
public
IWbllc
public
jlIIbllc
jlIIbllC
publ Ie
public
publ Ie

void
void
void
void
void
void
wold
wold
fold
fold
wold
wold
wold
wold
wold
wold

l nlt()
V)
palnt lGraphl" Vl
sUrt( )
run O
stOlID
h,.T,.p.e4lke,.Ewtnt k)
hJ'PrtnedClt,.Eftnt kl
kQ~ltued(lt,.Ef~t kl
updlte(Gr~ph lcs

.,usePrtnedlllo~seh~ttl
"osf~l.utdlllo\lseEw~t
"us.wedlllo~stE

..nt

e)

e)

.,oseDralNedlllousth~t tl
.,ustEnttttdlllOusthent tl
.,ustExlttd(l!oustEwtnt tl
lIOuscClltktd (Househtnt tl

Even if you ignore the mouse listrner events, there are a lot of raw evenls in th is
listing Ihat every applet-I);lsw game must implement at a minimum. As for
Ihings such as k...'Y events, we wanlto completely repktc:e the stock events with a
keyboard handler that supports multiple key pres~ and releases. I experimented
with quite a few different ways to do this, and I came up with a solution that is
veTS3tile but not totally internal to the Ga.e class. The key events arc passed on to
the game, which can then usc a few global boolean variablC$to keep track ofkC)'S
needed by the game, The mouse events :;lte parsed, and sever.tl mouse prOllC'rties
are made available to provide your game with mouse button and movement
infonrnttion.

Adjusting to Ewnt-Driven Programming

Custom Game Events


In place of tht- standard applet n'mts., I ""'ant this claM to 50md ~ts to the gamlE'
that arlE' directly rela tw to gamcplay issues, such as spri te collision and .s<:r.-m
rrtrcsh, The most crucial methods in the GnK~ class usc the ~nl.atedSprl te class,
Tl!C' &.w class has thlE' following featurcs:
Performs automOltic double buffering
Maintains a consistent franle ratlE'
Handlcs thlE' input listmcrs
1\lanages the gJIme loop thread
Maintains an intema.J linked list for sprites
!'erforms an alllomat ic frJmlE' r.lte calcu lation
Automatically mo"cs and dra....s all :Kti,e spriu's
PlE'rforms collision Itsting on all acti"e spritcs
Passes imponant ('\'cnts on to ,hIE' ganlC
That's an impressive list of gOOlls for any spri te-based vme engi ne. This engine
allows you to build far morc complex gamcs than would be possible with
tM simpk arr.l)'S ....IE''>'IE' bcotn using in the prt'Vious chaptlE'rs. Tl!C' kiE"Y to the ~prite
handkr is thlE' Jnl,utll,LintedList class. By using a linkw list contOlining
~nj.atedSprJte obje<:ts, you can dynamically add and remove sprites wi thout
adHTsely affecting ~rformance. I've managed to get 300 sprites on the .s<:reen at
OI1<:e, ""ith full collision tc:sting. and thlE' game stiD runs at ,IK: desired frame ratlE',
Keep in mind, the target platform here is a ....d> browser! An}1ime you add some
overh('ad to a syst('m, )'OU will inher('ntly imrOllu c(' som(' indficicncy,
Thn'e's a tradroff bmoo'ftn simpk spd and your dail'(' to ju''IE' an mgaging.
complex game with 01 lot of graphics on the screen, I like having the ability to add
an explosion to the Vm(' al any point wilhom having to worry aboul that
explosion after it has finished animating iliclf. AnlaaledSprlte has a 101 of
rropntia and methods that maklE' this possible {soch ;os the llfetl. and
1I (eage vari ables tha t the GUle class uses to terminate a spritlE' when Its lifespa n
is completed ). I want to tell thlE' game engine. "II"')', the player's ship has
aplodcd riglll I1CTiE' (providlE' X,Y position). PIcasiE' display an nplosion here:'

239

240

Chapter 14 Galactic War: Entity Management


The game engine should nol only animate the explosion at that location, it
should also h.:Indle timing aOO then remove the explosion from the linked list
Illlrom'lIiC./llly.

n.
1hr iI"'=ed IisI hi hinlIes ip'IIl!S in lilt gnr I'IlginI! iI aglabll di5S ~ calIod Sl'rttes( I.
'bl CMI ~ . . d'I'IC! ~ II,.... ~ when. n - hm &._.

Here arc the ncwC'\~nts introduced in ttH, sprite cngiIY. These C'\'enlsare declared
as aDmact hccaus-e the inheriting class must implement them. Thq do not
contain any source code in the Ga.e class, ahhough Ga.e does callihem. This i~
what gives the sprite engine the ability to pass events on to the game while still
handling all thc rcal work behind the scenes.
VOId ga.eStarhp( )
voId gaeTl.edUpdatl()
vol d gaeRefresbScreen()
voId gaeSbutdown()
voId gaekeyOown(tnt \eyCode)
vOId ga-eke}'1Jp(lnt keyCode)
voId ,a-ellouseOownO
voId ,a.ellousel/ll
voId ,a.ellousellove
vOId sprl tellpdate lAttlNledSprlte sprJte)
voId sprltrilnw(Att1 ..te<lSprlte ~pri Ie)
vol d sprl teO:tlllt I .....1.. tellSprl te sprl te J
void Sl'rlteColl1stOll( .....1..tedSprHe sprJ. ..... 1.. lelISprlte spr2)
Know what's really great ahaut these events? lbcydescrihc t=dlywhat you need
to know in your game. What does spriteOyl ng mean? This is called just before a
sprite is removl-d from the linked li~t! What about Sl'rl telll)date and sprlteOra..'!
These two work hand in hand to give yalrr rode an opportunity to monilor a
sprite as it is updated and drawn (both of them are handled for you). For
instance, you use Sl'rlteOr~.. to draw the bounding box around a sprite if you
turn on the bounding box display option (8 key). The sprites are already drawn
by the time spriteOra.. is called. This just gives you notiC<" that the spritt has bttn
drawn, and you can do whatevtr you want with it. Likewise:, wben sprttellpdate
is called, the sprite will have already movecI (hued on vclocity). II'S up to you to
keep the spritt from going out of ttH, .scrCll:n bounds or ptrfonning any othtr
hchavior you want.

Adjusting to Eve"'Driven Programming


The Game Class Source Code

Here is lhe complete source code listing for the Ga lile class, which I have been
describing 10 you thus far. 1\luch of thi s code should be fa mili ar to r Oil ba use
we've used it extensively in previous chapters. I will highligh t ill bold the crl/ci,,1
code th at is not directJr related to the apple!. While perusing the source code for
thl' Ga lle class, pay close atte",ion to all bold lines of code, and then take note of
the rest of the code. This should help yOIl 10 grasp exactl y what this class docs.

I
.
........1
Applet

G~tIe

l .. portJ~ Vl

FrallCllork class

~p pl el .;

alit .';
j ava. awl . event
java .allt . 11IIage
java . lang .System;
jaVl . lltll .;

'!II,ortj~va

Iliporl
IfIlport
Ilport
I.port

abst rac t cl ass Ga~e extends App1et j lIIp1ellCnts Rllnna b1e .


HouseLlstener . HOllsellotlonLlstener I
lithe lIaln gall>E! loop thread
private Thread gallle]oop ;
IflnternalllH of spr1tes
prl ~ate LI nkedL1st _sprl tes:
publiC L1nkedL1st spr1tesO ( return _spr1tu, J
IIscreen a"d double buffer r elated urhbles
private Bufferedluge b~c kbuffer ;
prl~ite Graph1cs20 g2d ;
prl~ate Int screenllid t h. screenHelgnt ;
Ilkeep

of llOuse position and buttons


Pol nt20 mouseP os - newP01 "t20(0 .0) :
pr Iva t e boo1un lI011 seBut tDns [l - nell boo 1ea n( 4J;
tr~ck

p rl~ at e

IIframe rate coun ters and other t111l1ng variables


privat e lnt _f rallleeo"nt .. 0;
privat e Int~fra!IICRate" 0;
prlnte lnt des1 red Rate:

~ey l1

st ener .

241

242

Chapter 14 Galactk War, Entity Management


prtnte 10n9 stirtTl-e. Syst('ll.currentTl-e"lll t s():
l/local ap pl et object
public Applet appletl) ! return this: )

Ilgame pause state


prt vate boo1ea n _g~lIIeP~used - fa I se :
pub11c boo l ean gamePa used() 1 return _gutePaused :
pub l ic votd pau$eGa!lE'() I _g!lIIePaused - true: )
publtc vold resullleGalle() (_galliePaused- false; I
Iidec I are the game event lI('thods tha t sub cIa ss . us t illlp Ielllent
abstract vol d gaIleStirtup():
abstract vol d ga.eTt IledUpdate( ) :
abstract vol d gallleRefreshScreen ( ) :
abstract votd gallleShutdownO:
abstract votd gallleKeyOown(lnt keyCode);
abstract vot d galllel(eyUp( 1nt keyCode):
abstract vot d gille"ouseOown():
abstract votd g&e"ouseUpO:
abstract votd gallle"ouse"ove():
abstract votd sprlteUpdate(Anl u tedSprite sprlte);
abstract vol d sprt teOraw( Ant lIIatedSprl te sprt te) ;
abstract vol d sprt teOyt ng (Anl l11atedSprl te sprt te) ;
abstract vol d sprt teeo111 s t on (Ani lllatedSprHe spr 1. AnllllatedSprHe spr2):
I"

constructor
............. ......... ...............................f

publiC Gallle(lnt fralleRate. intwtdth. int hetght)!


deslredRate fra-eRate :
screenWi dth wl dth ;
screenHelght - het9ht;

Ilreturn g2dobJect so sub-class can draw things


public 6raphtcs20 graphlcsO { return g2d:
II current frallle rate
publtc int fra-eRiteO {returnjraeRate:}
I/lIOuse button\ and IIIO velllent
pub I1c boo lean lIQuseBut ton ( t nt btn) ! retu rn aouseBut ton s( btn]: I

Adjuning to Event-Driven Progr<lmming

public

Polnt2DIC~sePosltlon(11 returnlCusePos;

I..
.
1
~

pplet lnlt event -eU'od

publlc yold InU!) I


/lcrute t"e b.ct buffer nod drnl/t9 surface
butbuffer - new Bufferedlllil9f(screenllldth, StteenHelght.
Buffered lllilge . TlPCI~CRGBl ;
g2d backb~ffer .cruteCrap hlcs() ;
Ilcrute the Internll sprl te I I 51
_sprltu - _ llntedLht<AnlutedSprltt)();
IIshrt the Input listeners
I dd~eyLI stener (thl s J ;
.ddMouseU stener ( this) ;
add HouseHot1onL I stener ( thl s ) ;
IIthis II!t~od llIIpl_ntPd by sub class
, Shrtup{) ;

I....
1
pplet update event -e th od

publIc yold ~pdlte(6npIltCI9) I


lIulculite frail! rite
_fre.count++;
If (Sntetl.currentTl_lIt 'lll() > ItlrtTtlM! + 1000) (
sta r tTIIM! _ Sntell.currentTh,ell l llls();
_fruehte Jrl.Count;
_fra.CountD;
/lonce nery SKOftd all deld sprites are deleted
purgeSprltes();
I
IIth1s -ethod I. plelltnted by sub cla n
gleRefruh$creen() ;

Ildra.. the lnternll list of sprites


I f (lgl.',usedO) (
dr.wSprltu{);

243

244

Chapter 14 Galactic: War Ent,ty Maoagemeot

/lredr,,' the scrun


pJlntlg):

.
.....................................................,
applet windOW

pl~nt

evenl

~lhoo

p~bllc

void pJIAl(GrilpMcs 9) I
g.dr ... lllilge(biI'~b~ffer.O. O. thiS):

thrud stilrt evenl

stilrt the galle loop

r~nnlng

vold lti1rtl I
9i111e1oop ~ new lhreild Ith I):
gille1oop. ~ tnUI:

p~bllc

thrud
p~bliC

r~n

void

event (g.1Oe loop)

r~n()'

lIi1cq~lre

the current thread

Thread t

Thrud.c~rrentThrud() :

Ilprocess the lIiIln galle loop thrUd


while It gilileloopli

v,

/ISH iI cons1stent frlile rille


Thread. sl eep(lOOO , des I redRa te) :
I
catch( I nterr~pteil[~cept t on e)
e.pr'ntHac<Trtce( :

lIupdille the Internal list of


If (lgIIlePlusedO) {
updateSprltes( );
testeo11 hlons();

~prttes

Adjusting to

e~nt-Oriven Programming

".l1ow ailn g to upclilte If needed


g T1IledUpd.te( ):

IJrefresh the streen


rep.lnt();

thrud

Sl~

ennt

~'*
I

Jl'IIbllC YOld stope) I


111'.111 the g~. loop
9~.IQOp - null;

IIthls IItthod 1Mplelltnted by sub clns


gShutdown( ):

.
.....................................................
,
r

key HSlener uents

publlc yold keyTyped(KeyEvent kl I


publ1c void keyPressed(KeyEvent 1'.) I
g~.KeyOoWll( k. geUeyCode( ) ) :

I
publlt wold keyllelened(Keyhent 1'.)
gl.KeJtl!l( t. getke,y(ode( I) ;

.
.....................................................

thedButtons stores the stote of the MOuse buttons

prl vlte wol d checkBut tons (HouseEvent e)


swlttMe.getSutton()) (
use Mousehent .8IJTTllIU:
MOuseB~ttons[l] - true:
MOuseButtons[l]- til se:
MOuse!uttOl\s[l]- til se:
bruk:
cue lIouseEvent. WTTOlll:
M<)useBut tons [1] - fll se :
M<)useButtons[2]- true:

245

246

Chapter 14

Galactic War: Entity Management

WluseSuttons[J] - filse;
brUk:
elSe llouseEvenLBUTTON3:
WluseButtons[l] ~ filse:
WluseButtOlls[2] ~ f.lse:
WluseButtons[J) ~ true;
bruk:

,
.
.....................................................,
",use 1t stener events

publtc wold "'usePress~CllouseEvent ell


checUuttons(e) ;
",usePos. seU (e .getl( II ;
"usePos.setlle .getYC II:
g.lltllouseOown( I;
J
pub 1j c wot d WluseRe 1ea sed(lIouseEwent e) I
cheCkBut tons{ e) :
IOlIsePos . seU(e .geU() I :
-,ousePos . setY{e .getY ()) :
gUH!HouseUp() :
J

pub 1j c vot d aouseHoved CHouseE went e)


chec kBut t ons{ e) :
WI.sePos. setlle .geU () I :
lOusePos . setHe.geU());
gllltlIouseHove() ;
J
pub 1t c vO td lOuseOrlgged(HouseEveflt e I I
check'ut tOIlS (e I;
lOusePos .seU(e.geU( )) ;
WlusePos.seU(e.getY( II;
g.-'Iollseo-. ( ) ;
g'-*'lIseMove( I ;
J
publ1c votd ..,useEnteredClloouseEwent e) I
WlusePos.seU(e.geU( II;
","sePos. seU (e .getY( I) :
g.-'IouseMove( ) ;

Adjusting to Event-Driven Programming


p~bl

Ie wold 8O~U[~ I ted(lIO~sehf1lt e) I


8OUUPO$.utl(e.geU( Il;
"~$ePos . ~ e t Y (e . getH ll;
ga.M o~$eM o.e() ;

I
Ilthl $ uent Is not netded
p~blle

wold ..~seCl lekedOloliseEnnt el ( I

I .. .. ..
X al'ld Yweloelt)' eale~htlon f~netlOfls

..................................................1

pr ote ct ed d o ~bl e calcA ng leMov eX{ dou bl e angl e ) I


r eturn (doubl e }( Math .cos(a ngle Mat h,P I /leO)):

I
pro t ected dO\lble uleAngleJlOweYldolible anglel I
retu .... 160llblel (Math.$lnlan,le ItHh.PI / l80ll;

updat e U'e sp rite lI H f rOIl U,e g ~lle l oop thread


" 1

protected wold IIpda teSprl test I I


for (Int n-O; n (_sprites. sinO; n++) I
AnllliitedS9rlte spr - IAnI ..tedSprl tel _sprltes,<]eth.);
If (spr.alheOll
spr .lIpda tePos I t 10n{ ) ;
s pr .upda te Rou t 1on( ) ;
spr .updateAn l ru t lon() ;
sprl teUpdate{ sllr) ;
sllr .lIpdateLlfet 1.1 I;
If{!spr.alln()) (
5prl tell)'llll1 ( spr I ;

perfo,. eollislOO1 ustlng of III Ictln spri t es


~

protected wol d testCo Ills Ions( I I


!Ilter ate throu,h the sprite list , Ust eaeh sprite agalnst
/lever)' oth er sprite In t he lIst

241

248

Chapter 14 Galactic War: Entity Management


for (Int flrst-ll: first < _sprites. sileO: flrstH) C
Ilget the fl rst sprl te to test for collision
Anllll te<lSpr I te spr I (ArIlllltedSprlte) _spr Ites.<.et( f1 rst I:
1f (sprl.alheO) C

lliook thro~gh .11 sprites .galn for collisions


for (lnt secol'd 0; second < _sprl tes .slu(); second++) C
/Illite

s~re

this 15n't the

if (fIrst I second) (

u.

sprite

IIget the second sprite to test for collision


~nll11tedSpr1te spr2 (An ll11ledSprl te)
_spr Ites . ~et( second) ;
H{spr2 . 11ve()) I
If (spr2.collldeslllth(sprl)11
sprlteCol11s10n(sprl, spr2);
break:

I
else
sprl setColl1dedlfahe);

I
drall all actloe sprltes h the sprlte 11st
spritE's IQfier (n the Iht He drun on top

....1

protected void drlwSprltes() (


IIdra .. sprites ln reverse order (reverse prlorlty)
for lint 11-0; n<_sprltes.slze{): nH) I
AnJIII tedSpr 1te spr - (Anll11tedSprl te I _sprl tes get (n) :
If (spr .lheC)1 I
spr .updatefr( I;
spr . trans fo,..( ) ;
spr.dralll);
spr1 ttOr.II( spr) ; lI/lOti f1 pllJ'er

nhancing Galactic War

once eve ry second during the (r a_c updHc. U.is _thod


Is ,'lIed to reflOve ,II dud ip r ites (rOIl the 11 "ked 1i st

.....................................................,
prl VI te vold pur<~eSflrl tes () I
for (Int lI'"O; n <_sprltes.slze(); n )
Anl .... tedSprl te spr (An' .... tedSpri tel _sprltes.getln):
if (!spr.,lhe()) I
_spr1 tes. re.Gveln);

Enhancing Galactic War


You will be surprised to learn th:1 t the source codc for Galactic War has remained abou t the same in length , even though the game is dramatically more
complex with significant n~ gameplay features. It is truly only a f~ steps away
from completion, and mygoal in the nen ChapUT will be to add more gameplay
features (such as powcrups). Just play Ii'll' galm' for a few sond$, and you'll
immediately s a J>Ced for powerups! This gilme is hard! If you can manage to
keep from dcstroying some ofti'll' larger asteroids while working on thc smaller
oncs, you migi'll have a chance, but once you start letting bullets ny and
asteroids begin to break into smaller pieces, you will have to be quick on the
rnaneuI'cring to keep from becoming space dust (which I have just becomeS' Figure 14.2).

Explo ring the New Galactic War Source Code


Rather than go over the source code solely from a functional point of view, I'm
going to take you 011 a tour of th e code ,dong with screenshots of key aspects of
the game that arc affected by speci fi c sections ofti'll' source code. For a game this
complex.630 orso lines o f code is surprising {not counting the support da5.SCS). 1
have highlighted all key lines of code in bokItcxl so they "'ill stand out. If }"Ou pily
close atlmtion to ~ bold lines of code, I~ code listings should make more smsc
to you.

249

250

Chapter 14

Galactic War: Entity Management

LC't's gel st.. rted with the opening atdilS for tht' gamt, where ..JI the initi:l1
variablts ..nd objtcts Ut dmned. 1M most signiflant thing .. boul this code is the
short cbss definition! Tht 6I.lIct1cll,. dass just ottnds 6i-e-..nd th<lt's it!
Ikyond th<lt, the initwiz.uion o(the gamt'S graphics is.U done bcn:. The images
defined and loaded at the beginning are used ....mnl"Ver a new sprite needs to be
added to the internal sprite list.

..

.....................................................
GALACTIC liAR.

Ch~pter

IlIp<.Irtj,,,,,"t. ,
IlIp<.Irtj.~,.utll.:

IlIp<.Irt ju'.lIng.Systell:
l..-ort jU, . vt.uent.:
publIc clln Gahctlcllir extellds G,. I
If then ...n be stattc btc~use they Ire passe<! to . constructor
~Utlc Int FRAHERAl[ 60:
stHlc Int SCRHNIIJOTH 600:

Enh.alncing Gal<lcti<: War


shtl( tnt SCREE_WEIGHT

600;

1I_lse global constants


flnal tnt ASTEROrOS -10 :
fln.l tn t BULlET_SPHO-.;
fl~.l double ACCElERATlOI - 0.05:
fln.l double SHIPlOTATlOI - 5.0;
IIsprlte state v.lues
flnal tntSTATE-HORHAL~O:
ftnal tnt STATE-COLlIDD ~ I:
ftn.l tn t STATE-UPLlIllUIG ~ 2:
I1sp rlte types
flnlllnt SPRITE-SIIIP -I,
flnal lnt SPRITUSTHO ID_BIG - 10;
flnll lnt SPRITUSTElO IOJlOIUIl- 11;
fln.llnt SPRITUSTUOI0_WU -12;
flnll lnt SPRITUSTElIOIO_TlIY -13;
flnll lnt SPRITUUtLET -100;
floll lnt SHITE-EXPLOS ION - 200;
"various toggl es
boolean ShOOlBoullds - false:
boolean coll tslonTest tilt e true:
IIdeHne the I..yes used tn the g.-e
hlageEntlty bl( kgrouod,
IlllgeEotlty bulletllllge;
1_lgeEntlty(] blgA$letOld$ new IllIoentlty[5]:
IllIgeEntlty[] ..u.sUtOlds - new IllIgeEntlty[2];
IllIoeEntltJ'[] $_lAsUtold$ new IllIgeEntltJ'[l]:
IllIoeEntl ty[] tll1Asterold$ new 11lIgeEntltJ'[4] :
IllIgeEntlty(] explO$10n$ - new IIlI,eEntlty[2J:
IllIgeEnt t ty(] $MplllIge - new 1_lgeEottty(2] :
I{(teate a randall n.-tler gener.tor
~ndOOI r.nd - new ~ndOOl( I;
{fused to Ill\! ship t"'Porlrtly tnvulnerlble
lo ng co 11 Is IonT1l1er - 0 :
{fSrf key tnput tracktng variabl es
boolean keyLeft. keyRlght. keytljl. keyftre. keyB. keY(:

251

252

Chapter 14 Galactic War: Entity Management

........................

..,

publiC (>,l.cticll.r() I
IIcal1 b.se G ,1 us' con~tructor
iUper(fRAllHATE, SCREEHIIIDTH, SCREEHHEIGHT);

...
yold ,1.Startup()
111 old the b.ckground lalge
blckground new laigeEntlty(thl~);
blc kground. 1Old( ' b1uesp.ee. pn9 ' ) ;
/lerute the ship sprite- f irst in the sprite list
~hiplalge[D] new laigeEntlty(this);
shi plalge(D]. 1old ("Sill cuM p. png-);
sMlllalge(l] .. new laigeEntlty (th1s);
sh1 Illalge( I]. 1old( "sh1 p_thrust. png") ;

M1 ..teclSprlte ship" new M1.teclSpr1te(t~ls. grlpllles(;


sh1 p. setSprl teT;rpe (SPklTl-SIlI' ) ;
sh1 p. set1.ge(sh1 pl.ge[O]. get I..,e() );
shl p. setfra.1I1 dth ( sh1 p. l.gelll dthE});
ship.setfrHelgh! ( shlp.1 ..geHelgh!( ;
shlp.setPosltlon(new Polnt20(SCREEHIIID1HI2. SCkEEHHU6HT/2);
sh 1p. setA11 vee true) ;
sh1 p. setState( STATl-MORItAll ;
sprltes( ). Idd (sh1 p) ;

111 old the bUllet sprite '.ge


!luI letl.ge" _ l.geEntlty(this);
bulletlalge. 1old("p las_shot. png");
Illoid the uploslon ~prlte '.ge
nploslons[O] .. new l.geEntlty(thh};
e~p I os I ons(O]. \old( "e~p' os, on .png"} ;
e~p 1os1ons(l] .. new 1.lgeEntity (this) ;
up1os1ons(l]. 1oad( " e~p 1os 1on2 .png-) ;

Enhancing Gala(ti( War


IO.d th~ blq tst~rojd I.. ~U 5 tot.ll
for 11nt n-O; n<5: Itt+) (
bl9Mt~rolds{n) - new l.. g~Entlt)'(thls):
String fn _ Wuterold w+ lll+l) + ".png":
b1 gAs terolds [n). laid (fn) :
)

Illo.d th~ lledlu. asteroid luges 12 total)


for 11nt n -0: n<Z; nu) (
IledAsterolds(n] - new l... g~Entlt)'(thls);
Strlng fn _ wlIe1IllIII" + (0+1) + w.pngw:
IIe1IAsterolds [n). laid (fn):
)

1I10id the slNll ast~ro'd llNtes 13 total)


for 11nt n - 0; n<3; 0++) (
,.U,sterol ds [n] .. nell [lNg~(nt I t)'(thls ):
String fn" ,.all" + (n+l) + . png W;
s.lAsterol d,(n].l oadl fn) ;
)

l10ld t~e tlnYlsterold I.. g~! (4 totall


for lint n "0: n<'; 11++) (
tll1.ut~roldsh)" new IIN,~E.ntlt)'(tllls):
Strln, fn" -lll\1w", 111+1) ... " .png-:
lIl1Astuolds{n]. IOldlfn):
lit reate the rando_ asteroid ,pr!tu
for (lnt n -0; n<ASTUOIOS: 11++) I
cr~at~Asterold() :

Game Loop Update and Screen Refre$h

Now let's tak~a look at some keyNents passed here from the Game c1ao;s. Remember,
these methods were defined as ,,jl$lr(l(/ in Galle so that they would be implcmentl'tl
here in th~ ckrh'ed sourc~ code fijI'. ~ ga1leTI-el!Uplllte() method is called from
..ithin the timed game loop thrad. ~ qI1leItefresh:Kreen() melhod is called
from the appln updat~l) ('Vmt. I'm 1101 currmdy using gShutdownl), but it is
a,~ if you IlffiIto dwl ~ befo~ the progr.am tOOs.
Now let's take a look at figure 14.3. which shOW'S the game fairly early on in
the run. The :wIme toggle'S a~ still available in the game, including collision

253

254

Chapter 14

Galactic War: Entity Management

figure 14.3
The largeastereMds brtoak "" intosmallef ones. either when 1heyhit your shipOl'when \'OJ fire a plasma bolt.

testing and bOl.lnding-box display. There are four stages to deslroying an


asteroid:
Large, detailed asteroids
Medium asteroid chunk>
Small asteroid pie<:es
Tiny asteroids
I)Qn't let the tiny asteroids fool you-they will still damage your ship, and they
can be destroyed by your guns. Speaking ofdamage, the game would be more fun
if the ship's health were displayed somewhere on the screen-a good featurl.'" to
add in the next chapter.

.
.....................................................,

ga~eTjlledUpdate

event passed by gale engine

.old9a.Tlwdllpditt~()1

chKUnput();

' ga.Refr~shScreenevent

.... ",

.., , ..

p;1ssed by ga. engIne

"

..

" /

'Old ga.Refr eshScreen() !


Graphlcs2D g2d - graphlcs() :
lIthe shIp Is alwa1s the fIrst sprite In the Hn ked lh t
I.IllaatedSprl te $h1 P (1dl1IWO tedSpr1 tel sprl tes( ) . get( 0) :
IIdraw the badgro~nd
g2d .drawllWOge( background. get lute( ).0.0. SU[EIIIHOTHI.
SCRHIIHIGHT I. this):
IIgrlnt status lnforllitlon on the scrn
g2d. setCo1or(Co1or . WHITEJ :
g2d.drawStrlng( FPS: ' + fra-eRitteO.~. 10);
long x - Hath. round( shlp.IlOS1 t 10n() .I():
1ong 1 - Hitth. round! shl p. pos1t 1onO . Y( )) :
g~d.d raIlStr lng( ' 5hlp: + x +'.' + 1 . ~. 25) :
g~d. d rallSt rl n; ( 'Hove a n; 1e, ' +
Hath.round (ship . JI(I.eAng le( ))+90. 5. 40):
gld.dra IlSt rl ng( ' Face angle: ' +
Hath.round(sh1p.hcel.llgleOI. 5. 55);
If (shlp.state( )--STATLIIORllAL)
92d.drawStr<ng( 5ute: IIDRllAl. 5. 101;
ehe If (shlp.sUteO--SUTLCOllIOEO)
92d.drawStrlll/J( St,lte: COllIDO. 5. 10);
~ls~ If (shlp.stau( )-5TATLEXPLOOlfl6)
g2d .drawStrlng( State: EXPLODIIIG . 5. 10);
IIdlsplay th~ n~lIber of spr1t~s current111n use
g2d.drawStr1n9(Spr1tes: "+sprltn ().s1u(l . 5. 120):
H (shol/Bounds) I
92d . setCo10 r( Co 1or . GREEN) :
92d .drawStr 1n9 ( '80UND [NG 80lES' SCR([ IIW !DTH 150. 10) ;
I
If (coll1s1onTestln9) I

256

Chapter 14 Galactic War: Entity Management


92d. seteo Ior (Color. GREEN) :
92d.dr~wStrln9( " (OlLlSIOH TESTING " . SCREEHWIDTH-ISO . 2S):

1-*..-*................
..1
gameShutdown event passed by

void

g~lIeSnutdown(} I
lion well, let the

g~rbage

ga~e

eng; ne

collector nave at It.

Updating and Drawing Sprites


Next, let 's 13ke a look at the spri teUpd~te() and spr; teDraw() evenls, which are
passed on by the Game class. The spriteOr; ng() event is also available here, but iI's
not currently being used in the game. You would use this event to keep a sprite
al ive if you wanted to keep it around by S<':lling al ive back 10 true .
The game engine proces.scs all of the sprit...s in the linked list all10matically (by
the gallic loop thread), Aner each sprite is updated, control is sent to sprite
Update() to give you a chance to fool around with the sprile if you n~...x1to. (It is
passed as a parameter.) You can make any changes you want to the sprite, and
those changes will be stored back in the linked list because you arc working with
an object reference-the actual objl.'Ct in the linked list, not just a copy.
This is truly where the sprite engine shows its power. You can fir{'" as many
weapons as you wam, and they will strike an unknown number of .~pri1(S, which
will each blow up in an animated explosion-and the linked list keeps lrack of all
theS<': detai ls for you . All we really have to do is put together a new scratch sprit{'",
gil'e it an image from one of our global images, and then pop it off to the sprite
lis!. This sprite will then automatically move around on the screen, rotate (if
rotation is s<:t}, and animate (if frames exist), This new sprite will also be
included in coll ision test ing. The engine allows you to determine whether a
collision "sticks" by giving you control at the key point where a collision has
occurred, passing both spriles 10 yo ur imp lementation of the spri teColl1 s1on()
event.
When I was first developing the engine, I thought il \vollld be too limiting to
move all ofth{'" sprit{'"s into the list. I kept th{' ship outside the list for a while, and
then realized that to truly make the internal sprite handl{'"r work the way it is

Enhandng Galactic War


supposed to, you r~ally need to give it all of the sprites it needs to update, draw,
and test for collision. When you get tired ofa sprite, just set its a 11ve property to
false, and then the game engine will remove it from the list a few seconds later!
Furthermore, as yOIl learned in the previous ,hapter, you can make use of the
features in AnlliatedSpr lt e for limiting the lifetime of a sprite. By setting the
lifespan of a sprite, yOIl can have it automatically krmillate after a c("rtain
number of passes through the game loop. For instance, the "flaming plasma"
projcrtiles fired from the ship are gi'-("n a lifetime of200 loops. The engine d("te<:ls
whell the :lge is beyond the lifespan threshold, and then kills the sprite automatically!
Figure 14,4 shows the game after all of the large asteroids h:lve been broken down
into smalJ~r ones. The game is truly crazy at this point, wilh close to a hundred
small asteroids floating around yuur ship on various random trajc<:tories! It's
hard enough just to spin aud shoot frantically, I("t alone move around. I even h'ld
\0 cheat \0 keep from getting blown up repeatedly! (I'll share the cheat with you
at our next stop in this trip through the source code.)

Figure 14.4

The \h,p

OYeIWhelmed by ilIteroidl ilI'ld in need of some we3POfl upglades!

257

2S8

Chapter 14 Galactic War: Entity Management

"

.....................................................,
, sprltelJpd.te en"t p.ssed by til. e"tlne

pub11 C YOld sprl teUpclilte(AnllOiltedSprl te sprite) I


s.l tcM sprl te _sprlteT,pe ( 11 (
ClSe SPRITE-SKIP:
IIIrp(sprlte):
breilk:
case SPRiTLBULLET :
IIIrp( spr 1te}:
brut:
cilSe SPRITLUPLOSIOM:
if

lsprlte_eur~trril.O

sprlte_ toUlFrsO- l 1 (

sprl te _uUll Yel fillu ) :


)

brut:
Cil se SPRI n StEROI O_BIG:
cilse SPRiTE STEROIOJlEOIUM'
cilse SPRln STE ROIO_SIlALL:
Cil se SP RIn STERO I 0_T I HY :
wilrp(sprlte):
bruk:

f "

sprl leOnv ewe"t PiiSSed by til. e"tlne


c.lled by t~e til. c1i1Ss .Her eilc~ sprite 1$ drilWll
to the 1(1~ iI chil"ce lOailnlp~lilte the sprite

.....................................................,
public yold sprlteOrilW(A"lai1tedSpr!te sp r ite)l
if (s~owBou"ds) I
1f (sprHe,collided{)
sp r i t e, drilwBou"d s (Co Ior , REO) :

else
spri t e, dr ilwBou~ d s{ Co Ior ,BLUE) :

Enhaneing Galactie War

sprtttDJtng tytnt puad b, 9u~ tngtnt


cilltd H~trl sprltt's Igt ruchts tu ltftspn
It .... Ich pot nt 1t lit 11 bt kt 1I<!d Gff. Ind thtn rt.avtd f rClll
tM 1hktd 1t $to 'GU eln clnctl tht pur9tng prlKtH htrt .

.....................................................

public .Gld sprlttO,IIIIJ(AnI..t<!dSpritt sprite} I


IIcurrtnU, 110 nHil tG rutn In, spritts
Handling Sprite Collisions

The m()$t important ""ent in the game is proNbly!he sprlt~GlllStGn() \'\'mt,


..ilkh is passed from the engine to your source eode automaticaUy. "The engine
gots through the list ofactive sprites (when' the II tyt property is true) and teslli
each one for collision with all the other spri tes in the list (ac""t for the same
one-we don't want to have objects blow themselves up for no reason!).

Tah a look at figure 14.5, which shows the ship firing several volleys of flaming
plasma bolts toward asteroids. There are several bolts traveling away from the

F;gur. I ..,S
I"rojKliIes fired from the Vip ~ get pmed!llrol.9h the ~ __ wIwn they a6

...........

259

260

Chapter 14 Galactic War: Entity Management


sh ip at various angles and ~ explosions thai are animating on the- S(r('('1l.
Figure 14.5 shows sprite collision testing in progress.. Thce-nginc,as implemenled
in the 6ne class and inhm led by 6i1hctlcllilr, goes through the- sprites and tests
thtrn.a11 for collision-but that's ail it does. Look at the spritt countt"r in this
figure-132 sprito! This number includes lht ship, bulku, asteroids, and
explosions, and the- nurnbcr ~ and falls dynamically with the- gamtpby. (lbc:
mgine- dears out unused sprites that havc " dial" ODCC' C'Vn')' second whm lht
frame rate is calculated.)
Thcu is no more logic going on behind the S(cn('$ than the- test for collision itself.
The engi ne sim ply passes both sprites that have interacted to thc sprl teCo111
s 1on() method. At thi s poi nt, it's entirely up to you to decide what to do with the
conflicting sprites. You can destroy them , have them bounce away from each
other, or anything else. So all you have to do is figure out what kind s of spri tes
have been passed to you through the spr1teColl1s10n() event. You can do this
with your own predefined constants (defined as a static int in Java). Forexample,
I have defined SPRITE-EXPLOSION for all explosions. Yes, explosion sprites are
tC'$too for collision like C'trything else-but we simply ignore them, let them
play out, and then disappear. lbc value of each constant is nOI imponant, as long
as each one is dilTerent. 5iIlCC' I defined each oflhe four types of asteroids wi th a
stparate constant, I had to write a hdpc1" mnhod called 1s"sterold() to handle
them all in tht same way whm they arc pas,soed through tht C'VCIlt handlers.
Now, howabout that coot I promistd! WlItn I gt't inlO a lough spot in the-galne.

I hold down the kft or right arrow kty to spin while hitting bolh fire buttons (the
Ctrl keys). This launches twice- as many volleys of plasma bolts, as Figure- 14.6
shows.
{

.
.....................................................

~prl

teCo111 s1on eve-nt

p~ssed

by gnt eng Ine

pub l iC voId sprl teColl151on(AnllMtedSprlte spr l. AnllatedSpr lte sprZ} I


IIjullp out quickly 1f collistons are off
if (lcol1 hlonTntlng) return:
Iffigure out whilt type of sprite hilS collided
swltcMsprl.sprlteTJpot(J) {
cilSe SPRITE,..8ULLEi:
IIdld bullet hit i1n nteroid?
1f (1 sAsterold(spr2. sprl teTypot( ) J) I
sprl .uUlln{hlu):

Enhancing Galactic War

F;gu.. 1.6
~ IN>d> lwice

YlIU

a5

many boilS lMlg botIl CIr1 keys!

spr2.utAl he{f,l se):


brukAste r old{spr2) :
)

bru t;
CHe SPRllLSHIP:
IIdld Harold crHh 1nto the ship?
If (l sJ.ste~l d ( lpr2.lpr1hTYPt()1l {
If (spr I . IUh( I -- STATLlIOftIlAll {
call1slonTl _r - SystIM.earrffttTllldtlll1l( I ;
sprl. seth I11(:1 t1lftew Polnt20 (0. 01 1;
doubl e x - Iprl.pollt1on O .Ill 10;
double y - Iprl.poslt1on O .YO 10;
stirtBlgEll.plos1on(new Polnt20 (x. y)l ;
sprl. set5u1l (STATLEIP LOOI ~Gl ;
sp r2.set.i.lln(hlse) :
brukAste rold(spr2) :
)

II-a t e ship tellPOrully Invulner' ble


else if (sprl.lhh( 1-- STATLEIPLOOING ) {
if (call1 l lonTI_r + 3000 <

261

262

Chapter 14 Galactic War: Entity Management


Systell.currentTlMfll111 II II {
Ip r l.utSu te (STA n,.. .IlO11Mll ;
}

I
brnk:

Keyboard and Mouse Events

The keyboard and mouS(' handlers are not always used, but you must still
implement them in your program. You might wonder, then. how is this allY
better than just using the li$tenen d irectly in the game's source code file~ That's a
good quation! Basically, we want to homogenize the ~cnts as much as possible.
Note that the keyboard ~enu pas.s nothing but the key scan code, and the mouse
evenu pas.s nothing at all- you must access the mouse infom\;ltion through the
musePos and .auseButtons variables (wi th their associated acCes.llOr methods).
Simply k,no",ing about a ke}t>o;.rd or mouse e-'enl isenough; we don't nd all of
the a1ra information that Java sends the program.

..

.....................................................
;uleKey[)()Wn eoent passel! by gall! \,n;ln\,

public vo1d gaeKeyOolln(1nt keyCod el (


sllitchCkeyCcdel l
case KeyEvent. YK.. LHT;
key Left true;
brnk:
use Keyhent. U ..RIGHT;
keyRlght trc\';
brnk;
use KeyE.ent. YK.,UP;
keyUp. trut:
brnk;
eHe KeyE.ent. YK..cownOL :
kl!)'Flrt true :
brNk;
CiS\' K\'yEv\'nt. VK....B:
fltog;l\, boundIng recUn;lu
showBound s tsh0w8ounds:

bruk;

Enhancing Galactic War


cue Ke,E vent .VILC,
JJtonle colli s10n t esting
eDlllsloftTntl~" leoll\sIOllTnt1ng;
breit;

.....................................................,
publi c veld g ~ meXeyUp(lnt keyCodel I
swltc Mk eyCodel I
cHe h yEvenLVlLlEFT :
keyleft fi15e;
bruk :
cue Ke,Ev ent. WK.-RIGIH:
h,RIgbt fi15e;
bruk:
cue hlE ven t. VK.-UP:
k.,Upfi15e:
bruk:
USI tlyEven t. VK.-CONTROl:
k" Flr e.filu:
f1re8ullet{):
br ei k:

Mluse evenh PiHe4 by g._ ell<Jlne


the g is not currentl, using .use 1n",t

.....................................................,
",bile void g.~ou$elloom() I I
",bIle void g.-eHou$eUpl I I I
public void g. _ l\I)u$ellO'l( I I 1

Asteroid Manipulation Methods


The asteroid$ are the mO$t complicated part orGalactic War. First orall, there arc
!Clur types or asteroids in the game, each with several differenl images available
("'hich are chosen randomly when a new asteroid is created ). J have written

263

264

Chapter 14 Galactic War: Entity Management

figure 14.1
Tht bculding bcae5 MIll JIIi5oon

CMl SIilI be kIggIfd an

CIt

off in the game.

sevenl methods for working with asteroid$, mainly calJed from !he collision
~u thoot occur. The brgtt asteroids an: ~royd and replad by SmallCT
asteroids, while iIIl apJOSKln is animated over !he old asteroid.
Figure 14.7 shows the game with bounding boxe$turned on so you can s the
dimensions of each sprite in the game. Note how even the explosions have a
bounding box-they an: included in collision testing as well, even though the
game ignores them. All of the or iginal asteroids have been destroyed and
replaced with increasingly smaller ones. If you wanl to grab wme powerups
(so mething thaI will be added in the next cha pter ), you will have to be careful
not to destroy all of the brger asteroids-the smaller ones are next to
impossible to get around, and your shi p will get hosed by them in short order if
you let the guns go carelessly. As a result of this gameplay factor, this gamt'
involves somt' strategy.
This section of cOlk marks t~ end of the gaITIt' t'ngine events. From this point
forward, all of tht' rrK'Ihods an: custom progn.mmed and provKle tht' real
g;tmqIby in Galactic War.

Enhancing Galactic War

.....................................................
bru t up In IS t erold In t o slNller pieces

pr'~lte ~014 b~. U sterol d ( An llN t e<lS prI H

sprit e ) I

sll ltch(sprHt.sprlttTlI.eOII
cue SPRITLASTEROI(UIG ,
'sp."", .-dhll u tero14s o~er t he old one
sp'vnAsteroldlliprlh l i
sp'vnAsUr01 d( spr1 tel i
sp'vnAster01 d( sprl te l ;
Idr," bl~ up10si0ll
surUl9UpIos I OII( sprl te. pos Itl one I',
brut:
cue SPRITE..ASTEROIO_" EO Il.lH ,
lisp' ,," $all i l .sterOl4s over t he ol d one
sp.vnAsterol d( sprl tel;
sp'lII\A.s urold( sprl te) :
spllIMsterold(sprlte) :
II dr," sIN11 explosion
s U rtSIH 11 Exp1osl one sprite. posl t Ion ( 1) :
bruk;
ci se S PRIT USTE ROI D_ SII~ ll ,
II s p.~n ti ny Ht er01ds over the ol d one
splllnAs terotd (sprl te) :
5pllln~s terol d(5prl te) :
5plllnAs terold (5prl te) :
IIdr lll slNll expl osion
5tartSIN11xplos lone sprl te. pos I tl on( II :
bru lr.;
tlse $PRlTE..A$TE RDID_Tl NY,
ISp.,,", r.nda. paver"p
SpllmPOIIl!rllp(sprl te I:
'dn ll sull uploslon
starts..11 Up1oil 0111iprl te. poil t I011 I I1:
brelt:

. .....................................................

.....................................................,
sp..... 1 SIlIlle r ISterold bHe<I on p'sse<l sp r ite

prlnte vol4 sp.....Aster01dIAni ..te<lSprlte sp r ite) I


IIcrute. new .stero14 sprite

265

266

Chapter 14

Galactic War; Entity Manitgement

An1 ..tedSprl te 1St - new An1aatedSprlte(thls. grlphlcs(:


1St. setA11 vel true) ;
f Iset pseudo' randa- pos 1t 1on around source spr Ite
1nt w- sprl te. getBounds( ) .width:
lnt h - sprlte.getBounds( I.helght:
double x - sprUe. posl t 1onO XO wit + rand. nextlnt( 20) -40;
double 1 - sprUe.posltl0l10. y() + h/2 + rand.nextlnt(20)-4D;
ISt.setPosltlon(new Polnt2o(x,ll):

Ifset rotltlon and direction InglU


1St. setFaceAngle( rind. nutlnU36011 :
1St. sel.lloveAng le( rind. n",tlnU360 II :
1St. seUotitl onlllte( rand. nutlloubl,( II;

IIset velocitl based on IOI. .nt direct Ion


doubl e ang - ut.lOleAngle( I - !lO:
60uble veh - calcAnglellovel(lng ):
double vell - calcAnglelloveYling l:
ut,seUeloc:1t, (new ~lnt20(ve" . v,I, )):
llse t s_ slIe'speclflc propertlts
sorl tch (sprl te, sprl teT,pe ( I I (
cue SPIITj.STJtOIO_"&:
ut, "tSprl teT1JH!( S'IIT....ASTIlllIOJlOll,ll l :
IIplck OIIe of the ra~ astertlld 1.. ~s
lnt I- ralld,lIeJltllUll;
1St. set l..ge(.edAsterolds [I). get I..ge( II :
ast. setFrallell1 dUl (.eQshrolds [I J. ldt~() I;
1St. setFralellelllht(.edAsterolds[I]. hel ghtll I :
break;
case SPlITj.STElOloJlEoIUM:
ut. setSprl teT,Pt( SPlITUSTIlllIOJMA.LLI :
lipid one of the rand.- Isterold 'lIagts
I - rand. nextlnt( 31 ;
1St, "tlllage( s.l Alteral ds[1] .getl..g,( II:
1St, "Hr._Ill dtM s.l Asterol ds[ l].wl dtM I I :
1St. seHr._Hel ght (s.Usterol ds(1]. hel ght( II :
br eak:

Enh~n(in9 G~lit(ti( W~r

us~

SIIMI Tl.ASHll:OIO_SIIAll :
I5t. setSprltfTJ~lSUI Tl.ASTEIlOI O_T i Iff I :
Ipic~ on~ of t ~. rarnlo.,ltuold i... g~s
t r. nd.nutlntU):
Ist.s~tl.. g~(tnJAst~rolds(11.getlug~())
:
1St. s~tFrl-elfldth( tnJAst~rol ds[ 1] .wt dtM ) ) :
ut. s~tFrlMHelght (tnyAstuol ds(l] h~t ght( ) ) :
b r ~ak:

I,:d t h~ new ISter( Id t<


sprl test ) ddlut):

t~~

sprt t~ lIst

.....................................................
crute a und~ poIlerup.t th~ supplied sprt te loutton
( t hls will be tllPle-e nted tn t ~ ~ n ~ xt chapter)

.....................................................,
~rt

<ate <0t d spawnPower up(....nhi' tedSp r i te sprt te) (

.....................................................,
Il'obl

id cr. l~~ ....steroidl


new 'sterotd sp r ite
. . nt ..tedSprt te 1St - n!'lf ....nt ..tedSprtte(thls. grlphlcs(:
1St. setAl he(true);
ut. setSpr t teTJpe( SPRITUSTERIlIIl..BIG) :

flcrelt~'

Iptck one of the r,nd-..sterotd i.ages


int I rlnd.nutlnUS):
ut. set '.,ge( bllobteNltdsI1) .9ttl.. ge( ) I :
ut .setFTI-ellt dtMbl90\5terolds[ t ). wi dth( I I :
ut. setFruellelghUbl lob terolds[ I). Mlght( ) ):
ISH to , und~ pcsl t 'on n the screen
tnt ~ _ rlnd.nuUnt(SCUEIIWIOTlHZ8l;
tnt J . rond.n.~tlnt(SCREENHEl6iHIZ8):
ut.setPosttlon(new PolntlO(x. J)l:
Iset rOUl' on ,nd dt reet ion ,ngles

267

268

Chapter 14

Galactic War: Entity Management

ut. sethceAngl e( rand. nextl nt(360) ):


ut. setHoveAngl e( rand. next1nt(360) ):
ut. setRotat t onR~te ( rud .nextOoub1e( ;
/1 set vel oc t ty b~ sed On ~velOf nt dI reet I0"
double ang ut,llOveAngle() - 90:
doubl e velx. c~1 eAngl eHoveX(ug):
double vely . c~1 cAngleHoveHug):
~st. setve IocHy (new Pol nt2D( ve lx, ve ly) ) ;

lIadd the new astero id to the sprite list


sprl tes{) . ~dd(ut):

.....................................................
returns true H

p~ssed

sprite type is an asteroId type

prhate boole~n tsAsterold(ln t spriteType) (


sw1teh(sprtteType) {
cue SPRITUSTEROID.,BIG:
cue SPRITUSTEROIOJlEDlUH:
cue SPRITUSTEROIO_SHALL:
case SPR1TUSTEROIO_T1n:
return true:
dehult;
return false:

Handling Multiple Key

Presses

I spent a lot of time trying to incorporate a multiple key-press syslem into the game
engine itself, but this proved to be too troublesome. N, a result, the main game
keeps track of key presses and releases using global boolean variables. Since only a
handful of keys are ever used in an arcade-style game like this, a more complex form
of key handler is not necessary. As il is implemented here, the engine calls a few
even!S and passes the key code when a key is pressed or released so thaI you don'l
have 10 bother decoding the KeyEvent, MouseEvent, or MooseMotlonEvent class.

.....................................................
process keys that

h~ve

been pressed

Enhancing Gala.cti< War


pIobllc Yllld

c~ectlnput()

it .Inys t~e first sprite hll~e lhttd 1 ,t


AIllNtedSprltt shlp l AIlllMttdSprlte) sp rltu l ).gft(O);
If (teyltft) {
I/left ..rClW rlllltes s~lp left 5 degrHS
shlp.setFiceAngle (shlp. hceAngle() - SIlIPROTATlO.);
if (shlp.h ceAllg1e O < 0)
s~lp.setflceA.llgle(360- SIlIPROfATlO.);
It~t t~ip

I
else If (keyRight) (
Il r ig~t arrow rlltates th fp right S degrees
shlp.setflceAngle(shi p. hceAngle( ) SHIPROTATlON);
11 (ship.flceAngle(l >36(})
shl p. setFlceMg1e( SHI PROfAT 10.) ;

11 Ue)'llp) (
lup Irrow Ipplles t~ ..... st til ship
dt p. set 'lIge( lhl pl..,e[l).,etlllge( II ;
IpplyThrustll;

I
else
Iiset s~ip iatge to noratl non-t~rU~I iatge
shl p. set IlIge( shl pllNge[O]. get I., ge( I) ;

Moving the Spaceship

The sp.1uship in Gabelie War is rol3lffi usi ng the lrit and righl Jrrow keys, and
thrusl is appliffi by pressing 1M up arrow by_ The IpplyThrust( Illt1.hod
handle$

I~

acedenlion of t~ ship while kping I~ ship within a reasonabk

,docilY threshold

...........

public ~llid applyl~ru~tl) (


lithe ~hip Is always the firs t ~prite in the 1in~ed li~t
AnilNtedSpri te shlp (AnllNtedSpritt)spri t es () .getIO);
Ilup ,rrow add~ thrust III s~ip (1/ 10 nil .... ) speedl
shlp.seUlllveAngll(shtp.hciWIgl,() gO);

269

210

Chapter 14 Galactic War: Entity Management


fl calcuhte thl I aoo Y vel ocltl ba sld on angle
double velK - shlp.velocltl( 1.1( I;
velK +- calcAnglellovel(ihlp...,eAngleO) * ACCELERATION;
If (nlK < 10) nb.- 10;
lin H (nix> 10) velK -10;
double nll - shlp.nlocltl( I. H I ;
vel, +- calcAnglellonHlhlp...,eAn9leO) * ACCELERATlOI;
If (nl, < 10) nl, - 10;
IhlH (nl, > 10) nl, -10;
sh1p.nUelocltl(new PolntlO(velx. vel,);

Firing Weapons

The mOSl signifkanl area of improvement for Ihe game is in the weaponry
dep.1Ttment, so some lime will be spent in Ihe ne~l ehapler adding powerups 10
the game. You will be able 10 grab powerup icons lhal arc dropped by exploding
asteroids, which will Ihen enhaoce the ship in various ways. The (lIfTenl version
of the game here has ll(){ changed from the previous chapter, except lhal il now
fuJlctlons with the game engine. The Orl key is ~ to fire weapons, bul you
can change this 10 ;lll(){!ler key if you want by eumining the key handlers.

flrl a bullet trOll the shlp's poslt1011 aoo orlenut1on


uuu

"

u ,

public ,old flrellulletO I


lithe ship 1s al n ,s the fIrst spr i te In the Il nkiKIl1 H
AniutiKISpr1te ship - (AnlutiKISprltelsprltu( ).gltlO);
IIc r eate t he new bullet spri te
AnlutiKISprlte bUlllt - new AnlutedSprlte(thIS,grapMes(:
bull It. set luge (bull It luge. getluge( ) :
bullet. setfrnellldtMbulletluge.wldtM I):
bull et. setfruHlHII ght( bullet 'uge .hel gM( l ) ;
bull It. sltSprl teT7pe( SPRITUULLETl :
bullet. setAllle(true);
buliit. sltLlfespan(200);
bullet. setfaceAngle( shl p. hemgle( I ):
bullet.setlloveAntle(shlp. hemgleO - 90);
llse t t he bullet's stlrtll1<j posltlOll
double x - shlp.cllIter O .10 . bal1el.lu9ltlldtMlIZ:

Enhancing Galactic War

double , . shlp.eenUr(). Y( I bullet. 1..geHe19ht( lIZ:


IMlllet.utPos1t1on(new Pol ntZDll.1I l:
IIsft the IMlll et ' s ye1oc1tT
double Ingle - ballet ..enAIl91e( I:
dOllble s.. _ ealc:AnglellClnX(uglel* IULLE'CSPEEO:
double SfY- ealc:AnglellClnY(uglel* IUlLET_SPEECl:
bullet.seUeloelty(new 'OlntZO(SfX. Sfy)):
lIadd IMlllet to the sprite list
spr1 tts( l .Idd (1Ml1 1et):

Giv~ M~

Something to Blow Upl

There 3re two main methods for sta rling explosions. 1could hnve come up with a
craftier way to do this, but I decidcd 10 just write two similar methods: one for
initiating large explosions and another for smaller explosions. They usc images
stored in the explosions array (of '_lgeEntlt1 objeclJl ). There are currently only
two explosion animalio ns. The large explosion is used when you hit a large
asltroid. The smaU explosion is drawn when you hil smallCT a5leroids.1 think the
result looks prdty good.
Because 1!lCTe are Q lot of mWI asteroids and bullets flying evay whidl .... y. you
don'l ....nl too oomplex of an explO$ion sucking up lhe game's resources ..nrn
Ibm: oould be a couple dottn such explosions animaling at a lime. QIeck out
Figure .4.8. The large explosion's frames are 96)( 96 pixels in size and 1!lCTe are

LARGE EXPUlSION

SMALl. EXPLOSION

ftgure 1....

The two eq:hion .-a\lO'IS ~

br -.

271

272

Chapter 1<1

Galactic War: Entity Management

16 frames; the small explosion has 8 frames, and each one is only40 x 40 pixels in
size. The big explosion is used for the ship and large asteroids. This should be
used sparingly because it is such a large image.

I
........................
,
hynch, big uploston It the PIssed loc,tlon
~

publiC "otd stutBiohplosion(Potntl1l potnUI


Ilcrute I n<!'ll uploston It the PISSed IOCitton
Anl.,tedSprlh upl n<!'ll An1.,tedSprtte(thll.gr,phtcs{));
up1 .setSprthTYIl"(SPRITLUPLOSIO.):
up1 .setA1lveltrue):
exp1.setAnt.l.,geluploslons[O] geU.-ge():
expl .setTohlfraaes(6):
exp I . setCo1u....s(.) :
expl .setFraaeWtdth(g6l:
upl .setFraaeHelght(96):
upl .sftFr,aeOehy(2):
upl .utPoslttOfllpolnt):
lI.dd the n<!'ll uploston to the sprtte It st
$prltes( ).dd (UfIl):

I
Ilunch, s.,11 uploslon.t the PIssed locltlon

.....................................................

pybl1c "otd stlrtwllhpiostonlPoint20 point) I


IIcrelte I ne" exploston.t the pissed IOCltton
Ant.atedSprlte expl nn Ant,"tedSpriUltbls .grlpbics();
upl.setSprlteType(SPRITLEXPLOS10Ml;
expl.setAI he(true);
upl .utAn1.'.age(uploslons[1].geU.lge( ll:
exp1. setTotll FraHs I8) ;
exp1. setColu..sl.);
exp1. setFrallCWl dtM.O) ;
exp1. setFrl_HelghU.O);
expl. setFrl_Del.y(2l;
expl.setPosltlon(polnt);
lI,dd the "<!'II uploslOfl to the sprtte list
sprl tes () .add( expl) ;

What You

Ha ~e

Learned

Additional Game Logk


~

lut method in the gaIl'll' iscalkd warp{ l,and it has l~ duty of making Spriles
wnp around the edges of the screen (right, left, top, and bottom). This is kind of
a slrange occurrence if you think about ii, but a lot of gal1le$ llSof' this technique.
The iOO is that this makes an otherwise Slrnlll playing fidd appe;;tr larger because
objects can just travel through etha-J/K'U behind t~ monitor and magically
rnppe;;tron t~ Ol~rside, othelWUc' Ul1$Cl.thed. It helps to cont;Iin Ihe pmeplay
when a scrolling pme world is not a gool for the gilme.
f ..

.....................................................
cause sprite to IlIrp around the edges of the serren

public >old wlrp(AnlwatedSpr lte spr) l


IIcreHe so-e sho r tc~t variab les
Int" sp r. fra-eWldth()'1;
Int h. SpT. fra-rHelght{ )'1:
/I"Tap the sprlte around the scrHn edges
If (spT.posltlonO.XO CO',, )

spr pos Itl onl ). seU(SUUIllII OTM);


ehe If (spr .posltl onD .XO ) SCREEIllIIOTll)
SpT . pos Itl on{). setXI 0-,,);
if hpr .posltl0110. YO COU
SpT .pos I tlon{ J utYl SCIEEIHE 16HT):
ehe if hPT .posltlonO. YO > SCIUIHE IGHT)
SpT . posl t lon{ J. utYIO'h):

What You Have learned


This W;lS a code-heavy chapter that involved significant changes to the coding
modt"l we'~e been following in each chaptt"r up to thi s point. Now that Galactic
War is t"~ent -d rivt"n and makt"$ use of a sprite engine, the game has many more
upgrade possibilities than it might have had before (where each new sprite had to
bc implemented from scratch). Here are the key topics you learned:
How to creatt" an e'\'t"nt-drivt"n gilmt" engiM
How to

llSof' t~

new Ga. class

How 10 handle all sprites unifonnly, regardkss of type


How 10 adapt Galactic War to an t"Ymt-drivm game

273

274

Chapter 14

Galactic War: Ent ity Management

Review Questions
The following questions will help you to determine how well you have learned
the subjects dis<:ussro in this chapter. The answers are provided in Appendi~ A,
"Chapter Quiz Answers."
I . What is the name of the new game engine class developed in this chapter?
2. How many sprites can the new engine handle on the screen simultan~"Ously?
3. Which of the four key classes in the game engine handles image loading?
4. How many different asteroid sizes does the game use?
5. True or False: Collisions are handled inside the game engine.
6. What type of object is ani .. I..age, a private variable in ~nl l1atedSprite?
7. Which class is responsible for rendering a single frame of an animation in
Ani Ilia tedSpr 1te?
8. What is the maximum velocity value for the player's spaceship?
9. Which class does the game/sprite engine pass in some of its events?
10. Wha t is the name of the support method in An1mHedSprite that returns a
properly formed URL for a file to be loaded?

On Your Own
The following exercise will help you to see how well you have integrated the
new materia! in this chapter with your existing knowledge of Java game
programming.
There are curreotly two methods used to sta rt an o:plosion-a custom method
for large explosions and another one for small explosions. lbese methods do the
same thing, but they use a few different properties. How could yOIl revise one of
them to handle both cases for a large or sma ll explosion with a si ngle method?

CHAPTER

15

GALACTIC WAR : FINISHING


THE GAME

The lhings you have learned in this book all culminate in lhis lasl chapter
involving Galactic War. The game will be enhanced, polished, and ready for a
production environmenl althe end of this chapter. A111hat will remain to do is 10
package up the entire game, rl"SOUrCCS and all, into a JAR file for dislribulion on
lhe web (which is covered in the neXl and final ch'lpler of the book). Here are lhe
key topics of inlerest in this chapler:
Adding powerups to the game
Implemenling a global game state wilh a slarl and end screen
Polishing the game and preparing il for production

let's Talk about Pow erups


Let's face it; Galaclic War is a difficuh game. !l's nearly impossible to dear lhe
asteroids with the weapon we've bcrn using up to this point-a single peashooter,
for the most part. I "",nt 10 totally enhance the gamc in lhis chapter and make it
ready for primetime- for distribution on the web. To me<.1 thaI lofty goal, lhere are
quite a few things to cover in this chapter. I'll itemiu what I'd like to accomplish:
Add powerups to upgrade lhe ship's weapons system
Add powerups to restore health and shields
Add powerups 10 gain extra score points

276

Chapter 1S

Galactic War: Finishing the Game

--

_.

'*

'"

F;gu~ 1S.1
GilacIic Wl1f hilS

so: ciftmnI: ~ dill fI'Nno:t Ihe slip Md inaNw JCU iIO:ft.

Figure 15.I.shows the six po.....erup$ that will beaddi to tlte PI1\(' in this chapter.
Tip

The (ompIet! VI'rnon of Ill! ~ is ""ai!able on m, W!'bI.ite. ~_ I am (omjnui"lllC imprtM'


Ill! galTll!, it may be SOITIl!'I'I'hal diffmnl by Ill! lilTll! yoo rud this.. Go hffl!: \W/W,jMrboUf.coml
BeogiMi~.a.

Ship and Bonus-Point Powerups


There are thrl'C' differrot po~p$ that simply inanse your $COK for 250, 500,
and 1,000 points.. 'ThcK powerup$ are rdascd randomly when you datroy a liny
asteroid-the bst stage of the asteroid's ckterioralion after the large, medium,
and small slages.
The shield powcrup will increase your shield strength by 1/4 (not Ihe full refill
that you wcrt' expect ing!). Likewise, lhe co la can increases your sh ip's health by
1/4, up to the maximum value displayed in 'he health bar at the top oftlte screen.
Figure 15.2 shows the Ihret' states the ship can take on duri llggameplay. Wail, the
ship doesn't have any shidds! Olt, right; that's a fcatUK we'll add to tlte game in
this chapter as wen.

Weapon Upgrades
The .....eapon upgrade poowerup is by far the most interesting new feature of the
game, and il is very .....e1come given how difficult it is 10 stay alive in this game!
Yo u ca n earn up 10 fivc levels of weapon upgrades in this game. I had my son,

Ll't's Talk about Powerups


lllRUSTERS

. ,, ,

F"lgun! 15.2
~ au now Ihrte modts tor lht ship: llOfII\lII, lhR5tels, ind shields.

,
{;!

...

"

{;!

00.
0' 0
000

..
;; ; ;

Vu

{;!

- - {;! - -

{;!

F1i Un! 15.3

The IiYe levels 01 WNJIOfI upgrades for

)'0'.1"

ship.

Jeremiah, help ~&sign thewrapon


shown in Figure 15.3.

p;1t1fT1\$

for each

upgrad~, and

the result is

Th~

upgrades w~r~ implemented a little di!Tn"Cntly than our design h~re, bUllh~
result i~ unmistakable. The biggest difft'rcnce i~ upgradt' level fiv~: Rather than
firing ~id~ to side, the twO addilionalshot5 go upward at a slight ;lllgle. I made
this adjustment whilt' playing th~ gamt' wh~n it seemed to be morc cffelivt' than
firing thcm at 9O-<Icgr~ angles. Let's take a tour of the fi,'c weapon upgrades a.s
!My werc impkmenled in th~ gam~.

Standard WHpon
Thestandard weapon is shown being fired in Figure 15.4. NOle lhesingle bullet kon
in the upper-right cortlt'r of the scrl'en, ~howing the current weapon upgrade level.

WHpon Levef Two


Th~

first wtlIpon upg.rack allows th~ ship to lir~ two shots at the Rme time, as
shown in Figure 15.5. There aer now IWO bullet icol1$ al the upperrighl. After

277

278

Chapter 1S Galactic War: Finishing the Game

Figu.. 15.4
The standanI wupon is J WIgle bulK.

Figure 15.5
Two bullets clefinilety 00

101 men

~l

lfl's Talk. about

~rups

play testing the g~me for a while, I decided to m~ke this the starting weafXln level.
You can stilllo~ this by ge1t ing hit and then drop down to the standard weapon
if you aren't careful
Weapon Level Three

WeafXln upgrade level three allows the ship to fire three shots at the same time, as
shown in Figure 15.6. There are now three bullet icons at the upper~righl.
Weapon Level Four

The fourth weapon upgrade gives you four shots at a time, spreading out at
slightly wider angles than the previous level, meting out ma"ive damage to the
horde of asteroids, as shown in Figure 15.7.
Weapon Level Five

Heavy gunner! Weapon levd five is truly ltaggering. delil'ering massive amounts
of firepower to the ship. Take care, though-if you get hit, your ship is taken
down a notch to level four again. The angles of spread at level five are slightly
I'<ider than 1.....e1 four, and two additional shots fire out roughly sideways from the
ship (sa: Figure 15.8).

279

280

Chapter 15 Galactic War: Finiih,ng the Game

Figure 15.7
ro.. shots at a time is good for 'tOO' SfIf-<OlllidMcf.

,.

.. , '

..-,.,' ,,

,
,

fjgure 15.8

,.,.

'""

, ,.
,

..... -.'~

WNpon U19ide Itvd five res 'tOO' VIp sill: pro,Ktilel

En~rxing

Galactic War

Enhancing Galactic War


I'm going to sta rt at the top of the GaLacticWar.java file and note Ihe changes
roaM as ...~ move down through the source code. We'll take a look at a few
~ along the way to explain what's happening in the code, As you an
mrly see in the pages to follow, the new game engine (via the Ga.... class) makes
enhancements incredibly easy \0 add 10 the g.lll1e.
Ti p
If yau 1m _

yau

ope1\lP

.".

tlle

probkoms updMJng tlle sane code 'MIll the MW in... o.......ool:>, I ~

comPttt GM1Ic W. P'Ol'(l, Ioc".-rI ... lilt CD-ROM .. ~ t,

New Sprite Types


The first change to the progr:llm involves adding some new sprite typedefinilions
in GabcticWar.p.va. Near the top oflhe pragr.t.rn listing is a set of sprite types.
Add the J>eW items shown in bold tUI.
IIsprtte t ypes
tnt SHlTE.5WIP -I:
tnt SP.IH....ASHROIO_RI6 -10,
tnt SP.ITE",lSHItOIOJlEDlLAt-ll,
tnt SPRlTEJ.SJEIlOIO_SllAtl -12,
fl~11 tnt SPRlTEJoSTEROIO_TlNY - 13:
fl~ll tnt SHlTL8ULLET - 100:
flnll tnt SPRITLEXPlOSION 200:
flnll tnt SnITLPOIfEIW'_SHIELD - 300:
fhal In! S'UTLI'OIIEIWPJjUlTM 301;
flulln! snlTLPOVuuPJSO-302:
ftnll tnt SPR ITE..-POIfERU L SOO 303;
flul t nt SPR ITLPOIIERUP_1000 _ 304:
flul In! SPIITLJ'(lII(lU' _6UN _ 305 :

flnll
flnll
flnll
flnll

New Game States


To give Ihe g~ ll1 e Ihe ab ilily to start, play, and cnd (with the option 10 restart), we
need 10 add some cond itional gameplay stales and make usc of Ihe pau$C
pro~rty in the sprite engiDe (found in the 61.... class). Add the following lines
jusl below the J>eW sprile definitions, above tnc toggle V;lI.ubles. ~ J>eW code is
shown in bold.
Jlgl.... I tltu

fl nil tnt lWlEJtENU 0;

28\

282

Chapter IS Galaaic War: finishing the <;,me

I""ogu.. 15.9

11ll' bile

KIftI'I

01 GlUcIic WlIl ~ Iht Uy <DnlnIlJ,.

fln.11nt IWlUlUnlllltl:
fln.11nt &o\IIE....OJEa!:
/furjous toggles
boolun showBounds hlse:
boolun coil islonTest lng - true:

When Iheg:une first starts up. youSttthe titlcscrecn, which ilimown in Figure IS.9.
This screen shows 1M kys you pre:lli to conlrolIM ship.

New Sprite Images


Now let's add some new sprite image definitions llSing the l~geEntltyclass. The
ship has a new shield feature, and we have a whole bunch of new images for
PQwerups and the updated user interface (such as Ihc hcahh and shield melers).
Scroll down a bi t more 10 the block of code showing definitions for all of the
laageEntlty objts used in the game. Add til<: fleW (Ode shown in bold. (Note
...tso the minor change to the sh1plaa<Je amy. which now has three demenlS.)

/fddlne the juges IIsHl jn the gil.,


lugeEnt I ty backgrollnd;
luge[nt 1ty bul1etlaage:

INgeElltlly{] b1gAsterolds new INgeEnt 111[5J:


INgeEnt1 ty[ J ..edAsterolds - new INgeEnt lty[lJ:
lIligeEnt I ty[J s.lAs terol ds ne. IN;eEnt !tIn J :
I~ilgeEnt I ty[ J t n,As te roj ds ne. I ~i1;eEnt Ity[ 4J :
lIligeEntl ty( J uplosions ne>! INgeEntl ty[2];
IMgeElltlty[] iM plMge new l..geEntlty[3]:
IMgeEnt1tl[] .... rl.. te .. new IMgeEntlty[l]:
l..geEntlty blrFril_:
l..geEnt1ty powrupSh1eld:
IMgeEntlty powel'1lpl!eillU:
IN;eEnt1tl pollu up2S0:
1.i1geEllt1tl po.u upSOO:
INgeEntlt, poftruplOOO:
l..geEnt1tl poftr up6ulI:

Health/ Shield Meters. S<.ore. Firepower. and


Game State Variables
Now let's add some g10lxll variables to keep tmck of such things as the ship's
health, shield power, and game state, as well as more obvious things such as
current score, high score, and weapon upgrade level. Add the following code after
the image definitions, before the Rilldc.line. New code is shown in bold
IIHillth/shleld .ters ilnd score
lilt hulth 20:
lilt shield 20:
ll1tseo r e-O:
1lit hi ghs co re - 0 :
ll1t f 1reponr- l ;
lilt gl.State" GoIJIEJlUU:
. . {crute I rind.. nUllber 'lI'neriltor
Rilnd.. rind new Rilnd..():

New Input Keys


We need to add support for the new shield abililY. I've defined the Shift key 10
xtil'ate the ship's shield, but you may change Ihis key if you prefer a different
one. Locate the key input trxking \'3riabks a few lines below the WI change you
just made, and noll' the new \'3rUble added in bold..

284

Chaptff 1S

Galactic WM: Finishing tt'H! GaIl'Il!'

The collision toggle and bounding box loggle are both still active in the game.
Although they were use4 for test ing, they are now known as undocumented
hidden CMtlI$ in the game!

I/s_ ~tJ Input tr,eUng urhble~


boolun ~eyLtft. keyRIgbt, ~eJUp. ~tyFlre.

~ey8, ~eyC.

hJSbleld;

Sound and Music Objects


Immediatdy bdow the key tnclting variable definitions. ..dd the following code
for the sound ..od music objects (or make sure the code looks like this, ifit differs
in your souree code listing). Note the changes in bold.
I/sortt

~eJ

boDlun

tnput

trllc~lng url.bte~

~eyLeft. ~eyRIgbt.

keJUp. hyFlre. hyl,

~eyC.

hyShleld;

1I10u~ eff~U'~ ....lle

"1 dISequence .us I c .. Mew "I dl Sequenc, ( ) ;

SOUMdCl Ip sboot new SoundC l lp():


SoulldCl1p uploslOll- ntw SoundCllp():

Loading Med ia Files


Unfortunately. all ofthese new feature:5come with a price-lood times. The game
loads up very fast on your locIl PC, but can lake 10 to 20 sonds to load from a
wdJ,site, depending on your eonneaion.speed. All the images and sounds used in
Ihis game arc fairly small because they are stored in the compressed PNG fomlat.
lbe biggesl file is the background, which is about 300 KB, All remaining image
and sound files are well under 100 KB, and mosl of them arc in the I to 10 KB
range, which is C'X tremdy small indrtd. I suspect thaI without the background
image and the large aplosion, lhe pme wouklload up almost iJl5tantly. When
packaged into a JAR (wh ich is covered in the next chapter), the entire game is

600 KB.
lc1's add all of the new code to garttSUrtuptl to load all of the new images.
sounds, and music in tM game. lbcn arealso.some gamcpby-rdated changt$ in
Ihis method that you should look out for. All new code and du:nges are high"
Hghted in bold,
void ga-eStartupO (
I/load sounds and ....Ile

Enhancing Galactic War

.,slc.l0.dl-..slc .ld-);
shoot.lold(-sboot.n-) ;
exp I osl on. 1oldl "explode .IU-) ;
l/lold the hnlth/sht.ld bars
birfr " new 'aageEnt1t,lthh );
birfr. loIdC-barfrl png-);
blrhMge[OJ new 'aageEntlt,Cthls);
barl..ge[O].lo.dl-barJlnlth.png-);
barl.age[!)" new l.. geEntl t,lth15);
hrl..gt[l]. loadl"bar_shield. png-I;
III o.d powel1olPS
powen,pShleld" new 'aaIlIEnt1t,(thh);
J/OVtrupShl11 d. load{ "powlrup_shl e I d2. png-) ;
powerup~ell th .. new ' ..gentlt,(thls);
J/OVerupllnl tho loadl-llO"lrup_coh .pnll");
powerup250 " new 'aagIEnt1tJlthh);
powerup2SO. loadl "POOlfl1olp_2SO .jlllg-) ;
jIOWlrupSOO" _'aageEnt1t,lthh);
powerup500. I oadl "poIffrup_500. png-);
poweruplOOO" new l..geEntl t,lthls);
powerup l OOO. lOld("powerup_lOOO.png");
powerupGun" new 'aalleEntlt,(thl s);
,-np&un. load( ",-I1oIP-llun. jlIIf-) ;

'Iold the blc<groul!d laage


backgro"no - new 1.lge; nt H,( this) ;
b!ckgroun d. 1Old( bluesplce.png");
IIcrelle the ~hlp~pr'le-flnt In t~1 ~prlte
~hlp! ..ge[OI" nft! [..ge[lItlt,lthls);
~hj pl... ~!OJ.loldl ~p.a,elhlp.P"l'll" ):
~hj plaage! I) .. nell IMlgl[nt i t,lthls);
~hl pIMlge!! J . 1Pld( ~1I1 p_ t hrult. png') ;
~htplaagl(21 .. nl" 'aaglEntlt,lthl I);
shl p[aagl[2]. IOld( "ship_shiel d .jIII,-) ;

Il~t

""laatedSprl u I~lp" _ A,laattdSpr' tl Itllis. graphlcsC ):


~h1 p. letSpr' teTJP'f (SPRI T[_Sill P) :
ship. IH[MIIII(shl plaageIO}.gel[.alle()};
ship. setrrneWldtll(shl p. laageWldth()):
sll Ip. set r ra'e~elght( sll 1p. 'aage~I l.htC ) ) :

285

286

Chapter 15 GaliKtlc War; Finishing the Game


Shl p. setPos1t 1on( n!'ll Pol nt20(SCRHll\Il OTII/2. 5CREENIIE IGIlT /2) ) ;
shlp.setAllve(true) ;
I/Hut ship off IS lnvulllerlble
,hi p. setStlte( Sf UE..U'lOOll6) ;
cOl11s1onT1.r - S,st_.CIlrrelltT1RlllI11s() :
sprl tes() .dd( ship) ;
1/10ld t~ b~11et sprite iN9t
bul1etlNge - n!'ll INgeEnt lty(thl s):
bulletlNge.lold( "plISNshot.png ") ;
1I10ld the explosion sprite lNge
nelf INgeEMI ty(this);
explos 10ns{0]. 1old{ 'expl oslo" .png"j;
exp1os i ons( 1] - nelf 1 geEnt 1ty (thi s) ;
explosi ons(l]. lOld( 'explos lon2 .png ) ;
explo~ions(O] -

1/10.d the big uterold lNges (5 totll)


for (Int n - 0; n<5; "..+) 1
bl~terolds(n]

-"til INge~nt1t,{th1s):

String fn - lSterold + ("..1) + " .PfI9 " ;


bl~terolds(nJ.lold(fn) ;
I
1/10ld thoe lledl ... UUrold INges (2 totlll
for lint n - 0: n<2; .... ) I
-.14tero l ds(n]INgeEntit,(tMs):
String fn - lIedil. + (...1) + " .png" :
wdAHerolds[n]. 101d( fn);
I
I/lold the ~Nll uterold lNges (3 totll)
for (Int n - 0; n<3; n++) (
sl1Asterolds{n] - nelf (NgeEntl ty(this);
String fn - sNll" + (n+l) + . png ;
s.1Asterol ds( n]. 1old (fn) ;
I
1/10ld the tlnylSUrold II.ges (( tot.ll
for(lntn - O;n(l;n.... )1
tnyAsterol4s(n] ~ nelf INgeEnt It,(thl s);
String fn - t In, " + (...ll .. ". png ":
tnyA.sterolds[n]. loud(fn);

"til

Enhancing Galactic: War


Jlsurl aff In plUse.ade
PIUSe61.{);

Jlde lete th Is lIIod of code. wlt lch h, s beetl .,red to , notller -ethod
, .,.ed to resetG, -e
/lcrelle the rlnda.l5l erold spdtes
far lint n -0; n(ASTROIOS; nH! (
Crelle,(slerold( !,

./

Game State Issue---Resetting the Game


Tht pme us! to just stan up with aSlt:roids flying at your ship, without any
chance to prepare yourself! To avoid this problem, I've addtd all overall state
system to the game, which now starts off in G,I.JIE..MENU mode. During normal
gamcp lay, the slatc is G,I.JICPLAYlHG. When your ship blows up, the state is
GAHL OYER. To make it possible to restart the game after dying (in which case, the
high score is retaintd), we nd a way to reset the key variables and objects- but
t~ game should nol rdoad any files! Add the res et Gue {1 rnrthod just below
g,.Surt up{J.
pt'hiU Ild rUlt6l.0 (

ffrUUrt tb,.slc saundtrtCk


_slc .setlD09lftg{ true) :
_slc.pllr{):
Ifnwe the ship for tbe rut,rt
Anl ..tedSprlU s hip (Ani..UdSpriteJ sprlUs( ) .Get (O):
Jlwl pe out t ilt sprite 115t to sU rt owerl
IprlteS( ).clur( J :
Jlldd tbe sned shIp to ttle sprite list
shlp.sttPasltlon(_ Paln\lD{SCIHIIIIDTIlIl. SCIEElIIIEI&H1ll11 :
shlp.s eUllwlltruel:
shi p. sttSUtel STATLElPlOOI.G) :
Col HslanT1.r. Slste. currentTl.eII1l Il$() :
shl p.$eUelacltl(ne~ PalntZD(O. Oll:

281

288

Chapter lS Galactic War, Finishing the Game


spr1tes() ,add(sh1pl:
IIcreate the rando.asterold sprites
for (1nt n - 0: n<ASTEROIOS: nH) {
createAsterol d( ) :

IIreset variables
health - 20:
sh1eld - 20:
score - 0:
flrepower - 2:

Detecting the Game-Over State


The next method in the source code listing is galleTl.edUpdHe(), an event passed
by the parent Game class. We need to add a bit of code here to handle the
GAHLOVER state, which occurs when there is only one sprite left in the game-the
ship. Figure 15.10 shows the game in this state after the health mder has dropped
to zero.

Figure 15.10
II )'our f1ealrl1 drops to zero. tile game is r:NeI-YOU lose!

Enhancing Galactic War


veld gUleTllledUpdate{) (
c~e<:U~put():

If

(lgl.'nsed() 1& sprltn().slzeO --1) {


resetGl..():
gl.sute - &AII.,..OVEI:

Screen Refresh Updates


I've made a whole bunch of changes 10 lhe game screen, which is refreshed
regularly during the applet's update(} and palnt(} events. I've removed lhe
leSling/debugging displays, which show~'d the ship's vitals and Olher lhings. We
wanllhe g.1me screen 10 look nice now, withoul any c1u1ter, lkcIuse there are so
many changes involved, you may want 10 just delele any commented-oul code
and rewrite th is method as indicaled. I will show new or changed code in bold
and deleted code in ilalics..
veld

ga.Refres~Screen()
Grap~lcs20

I '"

g2d -

grap~lcs():

CQWffJlT O/Ir THIS Btocr


/IIiif' ship Is ,'.ars tllf' first sprfte fn tile Ifnted list
AnINtedSprlte shfp - (Anf.. tedSprfte}sprltes( }.gel(OJ:

1l000~f OR

......,

{{drill t~e backgreund


92d.dr... IIlll9l'(backgrcu~ll.9l't [Iltgl'( ).O,O,SCRHM/10lH I,SCRUM [00 ,1, this);
011 CQHHfNr our THIS BLocr
Ilprlnt HHus fnferllHfen en the screen
gU. setCclor(Color.IIHlTfJ:
gU.drnStrfng((PS, .frlllellate(). 5. 10;,
leng x -IIUh . round(shfp.pesl! fen().X()):
leng y II. th. round( sh f p. pes I! fen( ). Y( ));
gld.dr'/fStrlng('Shfp: ' .. x" ', r, 5. l5J:
gld. drallStr fng( 'Ho~e ,ngle: ' . Hath. round(sh f p. .,~e.4llg let J )+90. S. 40);
gld.drilfStrlng((,cUrlgle: '.. Halh.NlIInd(shlp. ',ce.4llg1e(
S. S5};
ff (shlp.sUte( )-STATLIIORNAI}
gld.dr'llStrlng(St,le: IOmL', 5. 10J:
else ff (ship. st,te( }--STArc..COHIOfOJ

11 '''llfH()~f

I'

n.

289

290

Ch<iptl'f 15 Galae:tk War. Finishing the

~me

g2d.drlwStrfng( SUlt: COHIOCO ', S, 70);


..Is .. If (shfp.$l.t .. ()--STArCJXPUXUNG)
g2d.drawStrfng( StH.. : flPLOOING', S. 70J:
g2d.drawStrln'1(SprH ..s: '+ sprH..s( ).slz..(), 5. 1l0);
If (showBounds) f

'12d . s..Uo 10rfColor. '/lUll};


g2d.drlwStrlng("BOOltDfllG BOles ' , S(lIffJtllIDTH'ISO, 101;

i
If (collfsfonr..HfngJ I
'12d . seUolorlColor .GRUN);
'12d. drawStrln'1( ' COLLIS/OIf TESTING', SCRfClfIiIOTH/50, 25J;

......

IIwIIlt Is tile '11. sute!


if (gl.Stlte -- 6AIIE../lEJIU) I
g2d, ietFOIlt(new Font( "Verdln.-. FOlIt, BOLD. 36) l :
g2d, ietCo 1or(Co I or BUCK) :
g2d ,druStrl ng (-&ALACT I CWAR-, 252. 202):
g2d .setCo1or(new Color (200.30. 30}) :
g2d.drIWSlr1ng(-6ALACTlC WAR-. 250. 200);
lntll.-210.,/-15:
g2d .setFOIIt(new FOIIt("'lI.sl1ew IloMn" Font ,ITALIC r Font ,IOLO. 20);
g2d.setColor(Color. YELLOII):
g2d.draWStrlng(-COWTROLS:". ll.. ++,*20);
g2d.druStrlng("ROTATE - Left/Right ArroMS", x+20, ++,20):
g2d . drlwStr1 ngl "THRUST - Up Arrow" x+20, ++,/*20):
g2d,drawStr1ng("SHIELO - ShHt tel' (no scor1ng)", ll.+20. ++,'*20):
g2d,drl.Str1ng("flR[ - Cui ke,-. ll.+20. ++,/*20);
g2d, setColor(COlor ,IIHIU):
92d.draWStr1ng(-POIIIWPS IIICIDS flREPOWERI-, 240. 480):
g2d,ietFont(new Font("Arlel - , Font.BOlO, 24:
g2d. setColor{Color ,OUNGE):
g2d,drawStr1ng("Preu EMTER to stl rt-. 280. 510):

I
else 11 (ga.State &.IJt.ll:Unlll6) (
IIdraw Maltlllsbleld bars and .ters
g2d,drawl.ge(bifFra.,getl.. ~(). SCREflllIDTlt - 132. 18. this):
for (Int n D: n < health; n++l (

Enhancing Galactic: War


tnt clJ<. SCaEElIlIlOTH - 13(1 ... n .. 5;
gld.d.."llUge(bnllUge[O].getilUgeO. dx.lO. this);
}

gld. dr,,, I.age( b' rFrue. get luge( ), SCREENVIOTH - 132. 33. th1 s );
for lint n _ 0, n <sh i eld; nH) {
Tnt dx SCREEIIVI OTH 130 ... n" 5;
gld.d.."llUge(blrllUge[l].getlIUgeO. dx. 35. this);

IIdrl" the bullet uptrldu


for(lntn_O, n<f1repower; n+"') I
I nt dx. SCREE NV IOTH 220 '" n .. 13,
92d, dr'''llUgel\l<l''erupGun. get I.,gl( ), dx, 11, till s) :
""hpJI1 the s~ore
92d.utFontl_ Font{Vtl'4,nl. Font.SOLD. 24)1:
92d ...tCo10rlCoJor .WIH );
92d.dr,,,Strln9( '" scort, 20, 40),
g2d .tetColorlCol or, REO),
g2d .drl"Str1ng( ... htghscorl, 350, 40);
}

,lse 1f (gStltt __ WLOVER) {


g2d.sttFoatl_ Foatl'"1'tl'4,n,. Font, BOLO. 36)):
g2d.sttColorlnewColorl200. 30. 3(1);
g2d.drlllStrlnglWE ovn. 270. loo);
g2d.tetFont(ntw Font(Arl 11, Font.CEMHIt-8ASHlNE, 24)):
g2d. tetCo1ore COlor. ORANGE) :
g2d.drl"StrlnglPren ENHR to rUhrt", 260. 500);

Preparing to End
The galileShutdo"n() event comes next. As you'll recull. this method was left
empty in Ihe previous chapter, but now we need 10 IISC it properly. A wellbehaved la\"a program will free up resources before the program .-nds. In the casc
of Galactic War, I pmer to rely on/av:l's buill-in g;tr'Ngc colltelor to free up
I'OOUrccs automatically. HowcvfT, it is necessary 10 shut off I~ music and any
SOIInd ril'tctscurrmlly playing before the applet ends becaliSC' sorndimes a MIDI
sequence will keep playing after the game has ended.

291

292

Chapter IS Galactic War: Finishing the Game


'01d gaIl\'Sh utdown() I
.uSlt.stopt):
shoot.stopt) :
expl oslo" .stopt):

Updating New Sprites


Next up is the spri teUpdate( ) event method. There are a lot of new additions
hen.' but no changes, as we have aillhese new powerups that need to be handled
when they appear on the screen. The most imponant thing to do here is to warp
the powerups along wilh everything else in the game. Then, in addition, the
powerups need to wobble, or alternate the rotation back and fonh, so they stand
out (rom the other sprites.
Just as an example, take a look at Figure 15.11. This zoom -in of the ship firing
shows how much the sprite engine is handling alone time. In this figure, J count
60 sprites in just this small ponion of the screen (which, granted, is where most o(
the action is currently taking place). All of the asteroids are rotating by some
random value. The flaming bullets arc rotated and adjusted every time they move
along their paths. The ship rotates with user input. Every time you destroy a tiny

Figure 15.11
The<e arc a klt of sprites In arr; normal game. but thi' ;s 0011' 1/4 of the

SCfet'O.

Enhancing Galactic War


sprite, an cight.fr.ame animation is played. That 's a lot of action! It's a good thing
we developed the sprite engine in the last chapter, or nom: of this would have
been possible usi ng the old method of handling sprites with arrays. (Oh, and in
cue you wen: wondering-the bullet5 have IIOt puscd through any of those tiny
sprit~ they have just been SpawnN by the destruction of a larger sprite and wiD
soon be annihilated by the incom ing fire.)
rKW rode in sprt teUpd.tel), m<lrkcd in bold. adds addition,.}
SWitch statement for dealing with the pownups.

The

public yotd spr ItetJpdiltelAlltaltlNlSprl te sprite! ,


svltchlsprlte.SllrltdY\lf'I l
use SPRITLSHIP,
v,rlllSllrtte),
breit:
ciln SPRITL8ULln,
WilrlllSllrtte):
brut:
eise SPRITLEXPLOSION,
t f l sp r t te.currentrriv() e~ sprite. tout Frilllesl)']) I
sprite. seUI he(fiIlse),
I
bruk:
cne SPRl TEJ,STEROID_BIG,
case SfR ITEJ,S TE RO ID_MEO 1UM,
case SfRITEJ,STE RO ID_S"ALL ,
case SfRITEJ,STEROIO_TlNY:
virp( spri te):
brut:
cue SPRITLPONERUP_S"I ELO:
cue SPRITLPONERUP..HULl1l:
cue SPRITLl'OllERUPJSO:
cue SPRITLP\lllERUP_SDO:
cue SPRITLP\lllERtJPJOOO:
cue SPRITLPOlIERtJP_6UI:
Vill"p (sllrtte):
lIalke powerup i1n t altlon wobbl e
dOl/b l e ~t - spr t te.rotitlonRlte l),
If (sp r1 te. filceAn91e() > 3501 (
sprlte.utlotltlonitllte(~t 1):
sprl te. utFiceAntl el 350 ) :

cases to the

293

294

Chapter 15 Galactic War: Finishing the Game


else If (sprite.hceAngleO <10) {
spr1te.setRotat10nhte(rot .1);
spr1 te. setFlceAngl e( 10) ;
}

brelt;

Grabbing Powerups
Ncxl in the source code listing is the sprl teCo 11 I Slon() event. All we need to do
here is handle all the new powerups that are in the game; this means that only the
ship should collide with the powerups. I've moved the test for the collision
testing toggle to the top of this method, out of the keyboard handling code,
lx.'Cause it belongs here instead. Some new lines have been added to increase the
score whenever a bullet hits an asteroid aJld to deal with collisions when the
shield is up. Note the changes in bold, as usual.
pub11 e vo1 d sprl teCo 11 is i on (An hila tedSpr I te spr1. An1 lIa tedSpr1 te spr2) l
Iljullp out qu1ckly if eoll islons are off
if (lcollhionTest1ng) return;
I Ifi gu re out

IIh~t type of spri te ha s co11 I ded


sll1tch(spr1.sprjteType()) l
case SPRlTLBULLET,
IId1d bullet hit an aster01d?
if (isAsterold(spr2.sprjteType())} I
bU llpScore(Sl:
spr1 . setAl i ve(fal se):
spr2. setAl ive(fa1 se):
brea kAs te roj d(spr2) :

bruk:
case SPRIH_SHiP,
IIdld asteroid crash 1nto the ship?
I f (I sAsterol d{ spr2. spri teType( ) )) I
if (sprl.stateO STATLNORIIAU {
1f (keyShieldl {
shield -1:
}

el se {
colI ls i onTjIller - Systell. current HlleH1 11 j s (1:

Enhancing Ga lactic War

spr1. utYelocitJ(n... Point211Cll. 011:


double l- sprl.posHiOll() . IO - 10;
double J - sprl.positiOfll) . YO 10;
startB ighplosion(Mw Pofnt211Cl . Jll:
sp r 1. set5ta t e( STA1E...EX'LOOI NI>l ;
ff relluce 5hlp health Ifter a hit
health-l:
if (health (0) (
galleStite - WE...OVER;
)

Illou f1 repower when)'011 get hIt

flrepower-;
11 (ffreJlllftr (1) f1rej)Olf1!r - I;

I
spr2 . setAl heC h l se} :
b rea kA5tero i dC sp r2}:

IllOi ke s ~ i p t e"llo r l rl ))' 1 nval nerl b)e


else if (spr l. stlteO -- STAiE...EX'LOOI NG) I
11 (coillsionlille r" J.OOO (
S)'ste..currentTi...lllis() I
sprl. setStateCSiATE....llRllAtl ;

I
bre~k:

cue SPRITU'OMERU'_SIIIELO:
1f (spr2.sprlteTJpeC )--SPRITE...SIIIP I I
shield"- S;
If Cshield) to ) shield - to;
sprl. setAl I u Cfa I $II;
)

break;
clle SPRI TE.,POWERUP~HEAL TH:
1f Csprt.sprltlTJpeC)--SPRITLSHIP) (
hulth"-S;
If C/Iotalth) to) hulth - to;
sprl.$ItAI1u(flhal;
)

break:

295

296

Chapter lS Ga lactic War: Finishing the Game


CGSf SPRITLPOWERUP_250:
1f (spr2.spr1teT~ pe{}--SPRITE...SHIP) {
bu.pScore( 250} ;
sprl.setA11 ve(hlse):
)

break;
cue SPR ITE...POWERUP_500;
If (spr2.spr\teT~pe()--SPRITE...SHIP) {
bu . pScore (500) ;
sprl. setA1he(hl se}:
)

brfGk:
case SPRITLPOWERUP_1000:
lf (spr2.sprlteType(J --SPRITE...SHIP) {
bu.pScore< 1000};
sprl. seUI he<h1se}:
)

break:
case SPRITLPOWERUP_GUN:
1f (spr2.spr1teT~pe(}--SPRITE...SHIP) {
flrepower++;
1f (fl repower ) 5) fl repower - 5;
sprl.setAll ve(false);
)

break:

New Input Keys


The game now uses the Shift key to eng:Jge the ship's shields and the Enter key to
continue when the g:;nne is in the GAHE-"EN Uor GAHL OVER state. There's also a way
to exit out of the game now: When the game is running, you can hit Escape to end
the game (see Figure 15.1 2) .
Here arc the new key handlers in bold.
pu b11c voi d

9~I.eK e~Oown ( 1nt ke~ Code l

sw it cn ( ke ~ Code)

c~se K eyEveo t.~K..- LHT,

Enhancing Galactic War

F"ogUnI! 15.12
The bupf ~ ... end the game immediatfly and . . . you to

keyleft - true ;
break;
cue keyEven t. VK.JIGHT,
keyRlght - true;
br eak ;
cue keyEvenLVICUP:
keyUp - true;
break;
cue keyEnnLVICCONlROL;
keyFlre-true;
brUk;
cue keyhenLICB;
II toggle boundlnll rKUnll1es
$horiounds - ! sllowllounds;
bruk;
cue hyEwent. 'ICC;
IIt0ll91e col11s1on testllll1
(011 hlonTestlnll - !collls1onTest 11ll1:
break;
CUI keJ'Eunt.VK..SHIFT:
11 (( l tlYUp) &I (sh1e l d ) 0))
hJ'Shfeld true;

$~ - .

297

298

Chapter 15 Galactic War; Finishing the Game


eln
keySh1eld" (the;
brelk:
cue leyEnnt.VIl.EITER:
1f (gl_Stlu.. &AltLIIElUl (
reset&l_O:
res.-GI_():
glieStite" &AltEJUIIII6:

I
lin 11 (gl_sutt! ... W...OVU) {
rentGl_Cl:
res~_Cl:

gl_SUte WLRUUII6:
}
(1$1

brelk,
Klybent. VIl.ESUJ'E:
11 (gl_Stlte &AHE.JtuNMI16l I
PIUse61_Cl:
gl_Stlte &AHLOVER:

I
break:

Now leCs add a single new case 10 the gaJlleKeyUp() even t as well.
pUb lIc void g~-eKeyUp(1nt keyCode) {
switch(keyCode) (
cue KeyE_ent. YK..LEfT:
keyLeft hlse:
brett:
clse Key[ _ent. VIl.RIGIlT :
teyRigkt - false:
brUk:
CiSe Keyhent. VIl.UP:
keyUp - false:
brUk:
CiSe leyE_ent. VK..COI'lROL:
kerFlre - false:
flrehlletll:
bre.k:
cue ltYhtnt.fk....SHIFT:
kerShl,ld" false:

Enhancing Galactic War

Spawning Powerups
In the pr~ious chapter _ adckd a ~ method 10 the game ailed SlltlfllPover",pt I.
which was kft empty at ttJ.c, lime. Due to that foresighl, _ do not havt to mak any
dlangt:5to the brea kAsterot d( I mdhod that makt:5thisaillnsltad, here is the fully
functional sp,lInPover\lpt I. Attbe lOP of tht codt, a random ptrttnlage: dettnnilltS
whether the powerup is act",altyCTtaltd. 1havt it cumondy S('tiO 12 ptTctnt, which
providts somt fair gameplay.lf)'Qu want to make Iht game mort difficult, reduce
this value. To make il easier, increase it.
Even though 12 percent dotsn't sound like very many powerups, keep in mind
lh~l every I:lrge asteroid produces three "mediums," each of which products
threc "smalls," t~ch of which produces threc "tinys" (see Figure 15.13). That's a
whopping 27 tiny asteroids forevcry I:trge one, and since the game starts out with
10 large ones-well, you can do that kind of math. In a single game session,
12 percrnt will grnmlfe about 30 powerops! I think this value should be rtductd
10 about 20 to make the game a bi l more challrnging, but t<lCh powerep tw a
limited lifetime, so it's possible in tbe htat of battle that tbe player will only
manage: to grab a few of them.

The sllallnPOIIerupl I method creates a singk PO"'"CT1lp sprite with 5011'It standard
properties Ihat all powerups share, and thrn it $tIS the specilk properties Wiing a
random number. Since there are six powerops, this random number determines
the type of po_rup.

299

300

Chapter 15

Galactic War:

F;ni~hin9

the Game

prlnte wo16 splllllPlM!rup(Anlute6SprUe sprlte) I


lIo~11a fltll tll\1 spritel Iplt out I potterup
lnt n" ran6 .nutlnttl00l:
If 1ft) 12) return;
line tMs powerup sprlte
.....1..tedSprlte spr" new Anlute<lSprltetthll. graphles(:

spr .setRotit IOIIbut8}:


spr. nt'oll tton( Iprl te. poll ttOll() ):
60ublt nlx - rIM.llelltDoullh():
600b1e nl1" rancl.~utDollb1e():
Ipr.setYeloc1t1(new 'olnt20(nb. wely)):
spr.setLlfesputlSOOl:
spr .setAHn(true):
lIentOlllze the sprlte based on potterup type
IwUth(rln6.nutlnt(6 {
cue 0:
IItrnte I new shle16 powrup Iprlte
spr. setluge( poolerupShl e16. get luge( l l ;
Ipr. setSprl teType (SPRlTE",.POllERUP_SKIELO) ;
Iprl test ) .166 (Ipr l ;
brnk;
tUe I:
IItrnte I ~ew hn1th powerup I prlte
spr .set luget poweruplleal th. getluge( l l ;
spr .setSprl teType( SPRITE",.POllERUP-.HEALTil) :
sprltl$t ).ld6{lpr):
brnk:
cue 2,
lIerelte I nltll250'polnt poolerup sprlte
Ipr set l..gftpowerup250 .'lItluge( 1l :
Ipr . setSprl teT)1lI(5PIITE",.POIIIERUPJ50):
sprl tel () .166( Ipr):
brelk;
cue l,
lIereltf I .... SOD'polnt power"p sprlte
spr set luge( powerupSOD .'lItlugel 1l :
spr setSprl teT)1lI(SPIITE.J'OIlERUP_SODI:
sprl til t) .Iddl Ipr):
break;
eue4,
lIerelte I new lOGO'polnt powenlP sprUe

Enhancing Gala(tk War


$pr . set huge (powerupl 000 . getl.age( ) ) ;
spr .setSprl te Type (SPRI TE.,.POWERUP_1000) ;
sprltes() .add(sprl;
breal;;
elSe 5,
I{create a new gun powerup sprite
spr .setluge(powerupGun .getl.age()) ;
s pr .setSprl teType (SPRI TE.,.POWERUP_GUN) ;
sp r 1tes ( ) .add( sPrl ;
breat;

Making the Shield Work


Ahhough lhe key evenls turn the ship's shield on or ofT, the real work is done in
the checklnputO method shown here. Let's l'lke a close-up look at lhe shield in
a(tion. Figure 15.14 shows the ship bomb.uded Wilh asteroids, but the shield is
taking all of the impact and protecting the ship (at least until the shield Tllns
Ollt!),
We also need 10 make a change to the new global game state so it will ignore input
events unless the game is running-in olher words, it should ignore gameplay

Figure 15. 14
Thi' doloe'up view shows multi~ a'teroids impac1ing the ship', shields (aM bfeaking aparI iOlQ small
asteroids Of jusl blowing up).

301

302

Chapter 15 Galactic War:

Fini~hing

the Game

input changes when in lhe G.o.HE_MENU or GAME_OVER stale. The new code is shown
in bold.
publ ie void eheeklnput() {
if (galleState I- WE.....RUNNINGl return;
lIthe ship is always the first ~prite in the linked list
Ani lila tedSp rite s hi p - (An i la tedspri te) spri tes ( ) . get (0) ;
if (~eyleft) (
Illeft arrow rotates ship left 5 degree~
s hi p. seO' aceAng I e( shi p. faceAng 1e() SH I PROTATI ON) ;
if (shIp . hceAngleO <0)
ship.setfaceAng l e060SHIPROTATIONl ;

I else If (hyRlght) {
Ilright arrow rotates ship right 5 degree~
shi p.setfaceAngl e( shi p. faceAng I e (l + SH IPROTA Tl ON 1;
if (ship. hceAngleO >360)
shl P. setFaceAng 1e( SH I PROTATION) ;

I
ifUeyUp) (
Ilup arrow applies thrust to ship
shlp.setlmage( shipllllage[11. getlll~ge());
~pplyThrust() ;

I
else if (keyShleld) {
shi p, seUlllage (shl plnge[Z]. get Inge( ) ) ;

I
else
Ilset ship illlage to norlllal non-thtu$! IlIIage
s hi p, seUlllage (shl pl.age[O). getl.age ( 1) ;

Making Use of Weapon Upgrade Powerups


The new weapon upgrades are aWL'SOllle, as you saw earlier in the chapter~
wouldn't you agree? This is the most interesting new gameplay feature, without a
doubt. Each time you get a weapon upgrade, it adds another gun to your ship.
However, if your ship gets hit, you Jose an upgrade, so it's pretty tough to keep
those upgrades. The goo<! news is, when you get level-four or -five guns, your
ship is so powerful that it's fairly easy to clear out the asteroids in short order.

Enhancing Galactic War

To support weapon upgrades, the code in nreBullet() has been completely


rewril1en, and two ncw support mcthods were needed: adjustDlrectlonO and
stodBullet() . The new "bullets" emerge (rom Ihe cemer o(the ship, and then
spread out in various pallerns, oosed on the upgrade level (which is in a variable
called ftrepower). rll show yo u the new codc for ftreBull et() first
pub l ic void flreBullet() (
IIcrea t e t he new bullet sprit e
Ani.~tedSpri te[] bull ets - new An t IHt edSprl te(6] ;
slf1tch(firellOl'er) {
cne 1: IIsingle shot
bull e15[ 0] - stoc~Bull et ( ) ;
spri test }. ~dd (bull ets(O] ) ;
bru ~ ;

cue l: Ifdouble shot


bul l e15[O] - stoc ~ Bullet{} ;
~dj ustDI r ect i on( bull ets [OJ, -4};
spri test }. ~dd (bull et$[O] ) :
bul lets[l] - stoc~Bul1 e t(} ;
adj ustOi r ect i On(bu1lets [1J, 4};
spri test }. ~dd (bull e15D] ) ;
brea ~;

cue 3: IItripl e shot


bul I e15(O] - stoc kBull et ( ) ;
adj u$\Ol r ecti on(b"1Iets COl.
spri test }.add (bul 1e15(O] ) ;
bull e15(l] - stoc ~ Bu 11 etO ;
spri test ) . ~dd (bull ets(l]) :
bull e15(Z] - stockBull et();
adj "5101 rec t i On(b"1IelS [ll,
spri test ) .add (bull e15(Z]) :
break ;
cne 4: 114-shot
bull e15(O] - stockBu11 etC ) :
adj "stOl r ect i on( b"1IelS [0],
spri test ) .add (bull e15(O] ) ;
bul le15(l] - stockBull et(}:
adj "stOl r ecti on( bul IelS [1].
spri tes ( ) .add(bull e150] ) ;
bul1et$(2] _ stockSullet():
adj "stOl r ecti on( b" I IelS ell.

-4);

4};

- 5) :

5):

- 10} ;

303

304

Ch.pter 15 Galaeti< War. finiihing the Game


sprlUs( l.addlbulllh[Z]):
bullth[3] - stocUullet():
Idjntlllrletlon(bulleu[3) . 10):
sprlUs( ) .ddlbulleu[3]):
bruk:
cue 5: IISshot
bull,U[Il] - stoekBul1et():
Idjntlllrletlon(bulleh[Il). -6):
sprl US( ). add l bull eh[Il]):
bulltU[I] - stocUlIllet():
.djllstlllreetlon(bulleu[I].6);
sprl US( ) .dd(bul1 eh{ I)):
buIIIU(Z] - stoeUulleU):
.dj utlll reetl on (bull 1'1$(2]. '15):
sp r ltu{ ) . dd(bullets[Z]):
bulltU(3] - stodBulletl):
.djustOlreetlon(bulleu(3].IS):
sprl tu( ) dd(bull ets[3)}:
bill leU[4] - stocUlIlletO:
.dj IIstOl reetl DIll bull ets(I]. -60 I:
sprltesl) .ldd(blllleu(4]1:
bulleh(5] .. stoekBlIlletO:
.dj ustlll reet Ion l bull ets[5]. 60).
sprltesl) .dd(bullets[5]):
bruk:
}

shoot.pli1(I:
Hen's the new .djllslOlrectlonO support I1ldhod. which basically just cuts
down on the amount of codc in ftreBul leU) becaU5C this code is repeated for
~ry single bullet launched. This method is new, so you should add it ~low the
ftreBullet(} method in your code listing for GalaeticWar.java.
prIvate voId adjustOlreetlo n{AnlaatedSprlte sprlU, double Inglt) {
.ngle - spr Ite. flceAngle() + Ingle:
11 {.nllle (01 In91e +- 360.
11 se 11 (Inlll' >360) In91 I' - .. 3fiO:
sprl te. setF.ewgle( IIIg1 e):
sprl te. seUlovWllle(sprl te. f.(Wlll el) -110) :
.nllle - sprlte ..auM9le( I:
double SYJl" calcAnllleltoveI(.nglel - BULLET_SPHO:
double sv)' - ealeAnllleltouHlnglel- BULLECSPEEIl:

Enhancing Galactic War


,prite.seUelocity(new Po1nt2D(svJl, svy)):
The Ilellt support method thaI helps ou t ftreBulletOis called stodBulletO.
This melhod creales a slocl:: bullel sprite with ,Ill of the standard values need~>d 10
lire a single bulle! from the center of Ihe ship. The cuSlom \lpgrad~-d bulldS arc
modified from t his slock build to create Ihl.'" v'lriolls firl.'"powu patterns you see in
the game. This method returns a new AnlllatedSprlte objc<:l.
prhate Ani.atedSprlte $toetBulletO (
lithe shIp Is always t~e f1ut sprite 1n t~e l1nted list
AnllllltedSprite shl p - (Anl.atedSprttehprltu() ,get(O):
An1ntedSpr1te bul _ nell An1I111tedSpr1te(t~t"
bul . ,etA11 vet true) :
bul.setlnge(bulletl.age.getlllllge( :
bul . setFrallCWI dt~ (bull etl.age. wt dt~ ( ) );
bul .setF rallCHet g~t(bu 11 et I. age. ~et g~t( ) ) :
bul.setSprlteType(SPRITLBUllETl :
bul .setL1fespan( 90) :
bul .setFaceAng1e (shl p. faceAngl e( ) );
bul.setMoveAngle(shtp.f&ceAngle() 90):

grap~lts(:

({set the bullet's velocity


double angl e - bu1.llOveAngl e () :
double svx - ealcAngleHoveX(angle). BULLET_SPEElI;
double svy - ealcJ.ngleHoveY(angle) BULLET_SPEED;
bul,setYeloclty(new Po1nt2D(su, SVy;
Ifset the bullet's ,tartlng pOs\t10n
double Jl - shl p. center( ). XO . bu1, t.agelll dth( )/2;
double y - shl p. center( ). Y() bu1. t.aQeHel ghtl lf2;
bul.setposttlon(new Polnt2D(x.y:
return bul:

Tallying the Score


The final change to the Galactic War sourcecodl.'" is the addition of a new method
calkd bumpScore(). 'Ibis is called in the colli sion routine to increase the player's
sco re for every asteroid hit by a weapon. (Collisions with the ship don't Counl.)

305

306

Chapter 15 Galactic War: finishing the Game


publtc ,old bupScorl(lnt h~h) (
score +. "-Icll:
If (score> 1I1ghcorel
1I1911score score:

What You Have learned


This hasccrtalnlybecn an eyc-opcningchapter! It's amazing what is possible now
that we have a sprite engine with such dynamic sprite-handling capabilities. It's
now possible, as you have seen, to add new powerups and ent irely new gamepJay
elements by simply add ing new cases to the swt tch statements in the key event
methods, as well as adding Ihe few lines of code to load new images. The end
result is now a fully polished, retail-quality game that's ready to take on any game
in the web-ba5ed casual game market.
Here's whal you have lnrMd:
How to add powerup$ to the game
How to enhance gameplay with new features
How to fire a spread of bullets al various angles
How 10 add a game Slate 10 give the game a start and an ending

Review Questions
The following questions will help you to determine how well you have learned
the subjects dUcus.sed in this chapter. The answns are provided in AppendiX A,
" Chapter Qu~ Answen."
I. What method in GalacticWar.jaVlll makes il possible 10 add powerups to the

game when a liny asteroid i$

dGtroyed~

2. What construct does lhe sprile engine (in Game.java) use 10 manage the
sprites~

3. How many weapon upgrades are available now in Gabetie War?


4. How many differenl point-value powerups are Ihere in Ihe game?
5. What method in GaJaetkWar.java returns a slock bullel sprile ob;t, which
IS Ihen tweoaked to produce lhe upgraded bullet spreads!

On Your Own
6. How many different asteroid images are there in Galactic War?
7. If you wanted to add another weapon upgrade to the game, which method
would you need to modify?
8. How many sprites is the sprite engine capable of handling at a time1
9. How many bullets are fired at a time with the fifth-level weapon upgrade?
10. What is the name of the static j nt that represents the game state when the
game is running normally?

On Your Own
There are so many possibilities with this game that I hardly know where to start.
Since 1 consider the game finished in the sense that it is sufficiently stocked with
featur~'S and gameplay elemellls to meet the goals I laid out for this book, I will
just make some suggestions for the game.
I would like to add a black hole that randomly crosses the screen from time to
time, sucking in everything it touches. Wouldn't that be cool?
Another great feature would be to have an alien spacecraft come onto the screen
from time to time and shoot at the player. To keep the alien ship from getting hit
by asteroids, the ship would engage a shield whenever it collides with an asteroid;
otherwise, it would have to navigate through the asteroid field, and that's some
code I would not care to write!
Here is yet another idea to improve gameplay, since the game is really hard. The
game could start off with a single big asteroid for L<..>vel I, and then add an
additional big asteroid to each level the player completes. Although the game can
handle an unlimited numocr of sprites, I would end the game at level 10 to keep it
reasonable. Since the game currently just throws 10 asteroids at the player from
the start, switching to a level-based system would greatly improve the fun factor!
For the ultimate version of Galactic War, I refer you to a derivative applet
available at www.starnightgame.com. where the team has posted a "space
combat demo" of the gameplay in Swrflight: TIle Lost CO/OilY using the source
code for Galactic War as a basis. You will find many aspects of this game that are
1miliar after working through the Galactic War project.

307

CHAPTER 16

GALACTIC WAR:
WEB DEPLOYMENT

This chapter finishes the book by explaining the all-importan t subje<:t of how to
deploy your Java applet~based games to the web. I aSSllllle you already have some
knowledge about how to usc FTPtocopyyourgame to a web server. I will show
you how to prepare the applet so that it will TUn from your own web page! (Evcn
if you use a free hosting service, if you can upload till.' files to your website, then
very likely the game will run from your site.) You will also learn how to usc the
Java Archi,'e 1001 to bundle your entire game (with class files and all media files
together) in a lava Arch ive file.
Here are the key topics in this chapter:
Packaging an applet in a Java Archive (JAR)
Using the JAR command-line program
Packaging Galactic War into a JAR file
Creating a hoSI HTf.,lllile for the applet

Packaging an Applet in a Java Archive (JAR)


The Java Devdopment Kit (lDK) comes with a command-line tool ",lied jar.exe
that is used to create Java Archive files. J/\Rs, as Ihey ar(' known, use the ZIP
compression Illethod when storing files. JARs can greatly reduce the size of a Java
applet- which is crucial for web deployment.

""

310

Chapter 16 Galactic War: Web Deployment


To use the JAR tool, you will need to open a command-prompt window (also
known as a slwll in some operating systems), and then set the path to the 10K if it
is not already set By default, on a Windows system, the JDK is inslalled al
C:IProgram Files\java, and under this folder there will be a folder containing the
lava Development Kit (JDK) and the lava Runlime Environment ORE). You
need 10 S<.1the path to include Ihe \bin folder located in the 10K. This will differ
depending on the version ofJDK you have inslalled. Currenlly on my syslem, Ihe
jar.exe tool is located here:
C:\Program FileslJava\jdk t.5.0_06\bin
You can open the command prompi by going to Start, Program Files., Accessories. You can also run cmd.exe manually using Start, Run. On Linux and Mac
systems, the 10K is usually already added 10 Ihe path when it is installed.

Using the jar .exe Program


The JAR tool is a bit finicky. If you don't use the parameters exactly right and in
the corred order, JAR will complain and fail to create the IAR file you wanted it
to create. The order of the parameters should not be significant, but it is in this
case. The general syntax ortlle JAR command can be viewed by typing JAR ar rile
command line. The output looks something like Figure 16.1.

Crea ting a New JAR File

The parameters in this Help listing are de<:eptive. Not only should you llpt use the
dash (-), bUi these parameters must be spe<:ified in a spedfic order. For instance,
we use the c parameter to tclllAR to creatc a new JAR file. Bm this parameler
I1u,SI be used along wilh f 10 specify Ihe file name. I can'l imagine a situ'ltion
where you would want to use the JAR tool without using a IAR file, bUl I guess
that's just me. After the cf parameters, you specify the JAR file name, and then
the files you want to add. Here is an example:
jar cf test.jar '.class
This command will create a new JAR file called test jar and add all .class files
found in Ihe current folder to the JAR file. Aft er doing so, if Ihe JAR 1001
successfully created the new JAR file, il will simply exit and not print anything
oul. (So remember, no display equals no problems.)

Packaging an Apple! in a Java Archive (JAR)

Fi\lllre 16.1
\lerifyirlg mat the JAR program

is il'Iailable al the command prorr>pL

Listing the Contents of a JAR File

To display the contents of a JAR filt,

liSt

the tf par:amettT, like so:

jar tf test.jlr
You an also include the option to display the contents of the JAR filt with
dtuils. This option also works when creating a new JAR file. hUI you mUSI he
artful 10 include tht v option afttr tht c or t paramtltr. Here's an example of
bot h cases:
Jar c, f tesLjar '.class
Jar tv f tesLjar

EJrtr.tctjng Files

from

a JAR

Fi~

You an extract a single filt or all lilts from a JAR filt using the ... option, like this:
Jar xvf test. Jar ',.

Updating it JAR File

You can update a JAR file using the II parameter, Any files you specify will replace
existing files in the archive, and any new files will be added.

311

312

Chapter t6

Galactic War: Web Deployment

Jtr uot test.Jar HelloWo r td clin


M.nifest Files
Jav.l archiva an include: a manifest fik !Nt tdIs the' JRE the' ~ of the' .cbss fik it
should nm (automatically) when il opms the JAR fik. Since this in fairiyrommon
OCCUrTtnCle.:md manifest fiks ~ a cioch. it rnaU$ ~ to include: OM in a JAR fik
!Nt wi1I run on the' web. The gmer.tI fortn:.lt of tilt manifest fik Joob JW> this:

.... tnClus: Ftlena.


You should not ilKlude the .class extension. There are more options for manifest
files, but this is the only Olle you neal to be cOlKemed with when the goal is to
run an appld stored in a IAR file on the web. Be sure to add a blank line after this
single line in the manifest file, or lhe JAR 1001 will complain.
To use a manifest file WhCll creating a new archive, you can use the III parameter
option. lust be sure that this is the Imt [eller in lhe options you include.

J ar

C~ f.

test . ja r lUn! fest. txt '. cl us '. ?"g

Hou
~ . . J.w.J cormtI.nIY's ~ WIth didlk rm ~!he JAR proopm _1lOt t*d
MUG mINd, Un Oflf clol!S IlOt usuaIy drri: a hot be!fage ~ JAR.

Packag ing Galactic War in a Java Archive


The JAR program is fairiy easy 10 ~ olKe you grt used to its specific nquirelTICrltS. Now let's ~ this tool to pack.a~ Galactic War inlo a Jav.l archive. This
will SOlve a lillle space and will keep the game together in a single file 50 you won.
leave any media files behind when copying the game or uploading illo a _!lsite.
C&ution
You must load fill'S in a ctftain W/l1 in \'OIK codlo 50 that the JRE

wi~

know how 10 reild t!ltm from

a JAR file when you have drployed the apple! to a ~ite. Ioe s~ you a couple of diffe<ent
ways to load im.>gl'S and other media files in th;s book.
The method you must use when a game is deoployed in ~ JAIl uses the J~va. net. URL dass and
!he getResource() method to aute a URl that \'011 can pass to !he appropiaIe .... <;f
sound loader. The getResou rce ( ) method is available from thi s. getel a55 (J. This mtthod
wiI Cll're(tIy puI a rnetilo hom ttw loaI fiI. 'Y'~ or from ;I JAR fiI. whrn leCUl'Ce5 ;Ire
sand wilhin a JAR. 11M is an eumpIe:
~Rl

urI- thls.getClanlJ.j)etResouru{fl1el\;l.);

Packaging an Applet in a Java Archive (JAR)


The fim ordl'T of business is to copy your project foldn- to a new location so you
don'. aidenlally mess up the original. Essentially, this ~ foldn- contains the
run-time fib for me game-the .class fib and all assc1S.
Reviewing the Pr'ojt Fi/e$
Now let's create a Java archi'l< to con lain the fib needed by this game. l1J<,
manifest.lXt file and index.hlml file (covn-ed nat) arc found in the GalacticWar
foldtr. I have copied all of the Galactic War media and class files into a folder
called GalaeticWar\project. Included are 30 image (PNG) files. two audio files,
and one MIDI file. In addition, we have these nine lava class files:
AnimatedSprite.class
BaseG:mleEntity.class
Game.class
lmageEnlity.class
Point2D.class
Sprite.class
GabcticWar.dass
MidiSequmce.dass
SoundOip.class
These files wn-e compiled using Java SF.. 6. That's a lot of files for a single game!
The last thing I want to do is deploy this gamt to a web stever by copying all
42 files along with index.html, although that is definitely a workable option. In
fact, ;fyou just edit the HTML file (which you'll learn to do here shortly), you Cln
simply copy these files to a website and run the game over the web. But a Java
archive works so much kller, and it saVC$ some space too.

Building the Java Archive


Using the CD command in the command prompt, 1''1< changed the current

folder 10 \Beginning Java\chapterI2\G;oI<tcticWar. (This may be slightly diffn-enl


on your s)'5lem.) You can perform this step from any folder ....'here your project

313

314

Chapter 16 Galactic War: Web Dep loyment

' ' ' ' _ "', _ ,."".....


......".. . ,..'......,.,.
,.
f..'- t_,
0, --... _
, .""
lm: ::: 11m
."' "",...
",....
,,,., " -_,, . ..
~'"

.. , _

l.t - . . .. to _

-.

~'"

>l_

~:::l;;:;
.,'1 _ ,,,,,..

0;,

....

",

..
r'-' . -____
. ._.r

m"o>

".'.'

".,... ...,"

-~

,-

_~

figure 16.2
Listiog the contents of the GaladicWar Iokle,.
files are located. I've copied all of Ihe class and media files to a subfolder called
project to keep things tidy. So, all I h~ve in this m~in G~laclicWar folder are
index.htm!, manifesuxt. and the projecl subfolder (see Figure 16.2).
The manifesl.lxt file for Galaclic War conlains this line:
Mal nCl ass, G!la el i cWH
This tells the jRE which of the .class files 10 open up and start runn ing after
opening the JAR file. (Ik sure to include a blank line after the Mai nClass
property line.)
You will need to use an optional parameter of the JAR program that lets you
specify a subfolder where the actual files are located. You don't want to just tell it
to include .Iprojectl. bc<:.lUse that will add .Iprojeclto the internal structure of
the JAR file. Instead, you want to grab all the files inside of .Iprojecl, bUI not
include the folder name. The option is C (uppercase is important). Here's the
command to create the GalaclicWar.jar file:
ja r cvflll Ga I ac t I cllar . jar 'un1f es I . tx I . C proj eCI

This line tells JAR to create a new java archive called GalacticWar.jar, to include
the manifest informalion stored in manifest.txt, to use the projeCI subfolder, and

Creating an HTML Host File for Your Applet

Figure 16.3
Cn>.~ng

the deployable JAR role

to add all files in Ihat subfolder to the JAR file. Figure \6.3 shows the output of
the command.
If thes<' additional steps get on your nerves, just lump everything together in a
single folder and fun the JAR program in the same folder as all your Java projcrt's
files, without using the Coption, like so:
j ar c.fll

G~l acticWar

.jar lIal'll hst. txt . '

Creating an HTMl Host File for Your Applet


HTML is short for Hyper/ext Markrlp ulIIgllIIge, and it is the w;lter nowing through
the world wide web. To run a Java applet on the web, you have to ellllmithe applet
inside a webpage. This involves creating an HTML file with an <applet> tag that
specifies the details about how to run the applet and where it is contained.

A Simple HTMl File


The most oosic format for an HTML file that will host an applet looks like the
following code. This code assumes that a file called game.class is available in the
same web folder as the HTML file.

315

316

Chapter 16 Galae;t;c

w"" Web Oeployment

cht.lll)(hU4>
ctHle>Thl$ 1$ ., giwclt HIe>
clMid>
(bod,)

(ipplu tode e glw.cl.lSS


wldtll-flOO helght.ofiOO)
(1Ippltt>
(lbod,)(lht.l)

The key to running an applet inside a Java archive is 10 add anolheroplion within
the <applet) tag called archive.
(apple t tode-gaw.clus
Irthtve-gI .Jar
wldtll-800 helght.ofiOO>

(/Ipplet>

1be wdlpa~ file is usualIyCl1kd inda..html ~\l.S(' that is w roune-of a 6.~ that
wdl .sc.-rven will send the: wdl broWStt autolNtically if you don't specify the:
HTML file directly. For inslalKe, when you go to www.jharbour.tom, W ..-cb
~r...er deli...ers index.html automat ically. You ClIn create Ihis simple HTML 6.~
using a text editor such as Notepad (as shown in Figure 16.4). If you want your
applet to IX' stored with your other web files, induding your already existing
index file, then just use a different name, suth as GalaclicWar.hlml.

~
~~

>

_.<1>

U.I~>G.oh.tl

-.

. ... ~ loy

<~.<I>

,_.tt....

$ . . . .. -......U.ln

... Iet . _ " " hc.le"u. c h ..

wi....."I....,.
<l,""'"..."'"h" .t1c...
....
<"!'Plet>

~ .J U

0600>

~~:l;

figure 16."

CRating

II

wKJpige IiIe CiIIed GaliclKWar.html.

Cnoating an HTML Host File for Your Applet

Testing the Oep\oyed A.pplet Game


\...hm you have the HTML hoslliJe and a JAR file rcadyto go, iI's time to upload
Ihem to your web server. I'll usc a folder on my web server called IBcginninglav;(
to deploy the Galactic War filcs. The indcx.html and GalaClicWar.j.ar files are
both uploadt"d to www.jharbour.comlBcginningJavaand are r~Jdy to run from
this location. When you open this URl, the web browser fires up the JRE, which
displays an attractive logo image and a progress bar while it downloads tbe JAR
file (Stt Figure 16.5). (Note tha i this logo may look slightly different on your
system.)

---........-~_

.~

j)

--

Java

---

.-

Fli ure 16.5

!be laYa IUflttrne displays this progTMI boar wl1ile

down~i"ll

the JAA file

317

318

Chapter 16 Galactic War: Web Deployment


Tip

When me applet ha$ compl~dy down1Dackd 10 )'OUr local ~m1, il Will access tn.:files in the JAR locally rllther than tuning Ilk- web.serwT for e'Vl"rf fi~. Rrmnnbn
tlk- list of 42 rMdia files in Galactic Wa~ If Ilk- game ""ere deployed to the web
server wilh all of those individual files, tlk- app~ would have to downlood C\1:ry
single file individually over Ilk- Internet. ThaI's a lot offik transfers! But when your
applet is stored in a JAR, that single JAR file is downloaded to your PC. and the
applct runs from there. All media files are drawn directly from lhe JAR file instead
offrom the web server. If all goes wen during the downloading of lhe JAR file, the
game should wme up as shown in Figu re 16.6.

.....
-_.

..

-.

_---

.-

-jI- -Rgure 16.6

GiIietIe War is . - IIlf"fling on ~ ~ -.II _ _ ..... WlIhn

III

tfIidfnl .JAR fill!..

Review Questions

What You Have Learned


Java

~ppkts

can grow quit..

ga~ ~ dozms of m.-di~

I~rg<"

.....h..n you aT<" writing

files.. 8y packaging a Java

gam.. becaus.- most

~ppl<"l-~ gam.. into ~

J;lVil Archive (JAR) li.Ic. you dranu.tically impro\'e th.. lilTl<" il uk.-s to download
game' from ~ web SC1'V<"r. You a1so cut down on the: number of
tnnsfen that must be mad.. whc:n individual filesaT<"stor<"d dirt.ly on th.- SC1'V<"r
inst....d of iruid.. m ;m:hive filt.

:and run th.-

HCT<" ~r.. th.. key topics you I.... med:


P~cbging ~n ~ppkt insid.. ~ J~Vil ~rchiv..
Creating ~n HTML host file for your appl.-t
Running the applet from a website

Review Questions
The following questions will hel p you to delermine how well you have learned
the subjects discussed in this chapter.
I. What does lhe: acronym JAR stand for?

2. What is the: name of the: program used to work ",ith JAR files?
3. What types of files can be stored inside a JAR file?
4. What compression method does the: JAR format us.c?
5. What mrthod must you ~ in conjunction with the J'YiI. net.URL class for
loading m<"dia files when an applet has bttn deployed in a JAR file?
6. What command would you enter to create a new JAR file, called test.ja r, that
con tains all files in the current folder?
7. What command would you enter to creat e the same archive bu t also include
a m anifest file called manifesuxt?
8. What command would you enl CT to list the contents of a file Gllled MyGam...jar ",ith verbose list ing t'nabled?
9.

Wh~t JAR parnmetCT option Gluses files to bc added from a djff..rmt fold..r
without adding tht' folder name to tlK files stored in the: archive?

10. What

t~

of Wt'b server do you nd to host a lava apple1-based game?

319

320

Chapter 16

Galactic War: Web Deployment

Epilogue
This concludes the final chapter of the book! I hope you have enjoyed reading
this book as much as I enjoyed working 011 it. I'll admit that it was quite a
challenge! For a while I didn't think this Galactic War game would ever sec the
light of day. There are so many advanced topics that we didn't have time to cover
in this book, the likes of which a diehard Java programmer would have liked to
sec. However, I believe a completely functional game, created from scratch and
actllally finished within the pages of a book, is c'u more educational than any socalled "advanced" material we might have looked at instead.
The game engine developed in the previous chapter, which was based on all the
material in this book, is a viable game engine that can be used for many difTerent
types of games. To see one example ofa game created using this engine, check out
the Space Combat mini-game at """,,.starfiightgame.com. This applet is a demo
of the ship-to-ship combat that takes place in Srarjligllt; TIle Lost CoI,my.
My hope is that you have learned enough from this book to build your own Java
applet-bascd games and that you will create the next blockbuster game for the
online casual g<Jme market. Good luck!

APPENDIX

CHAPTER QUIZ ANSWERS

Her? arc tht answtn: to tht quines at 11K: md of nch dtapttr.

Chapter 1
l. Whal dOt$ tht acronym JDK stand for!

Answer. Java Development Kit


2. What version of the )DK are we focusing on in this book!

Answer: 6 or Java 6 or Java SE 6


3. What is the name of the company that created Java!
Answer: Sun Microsystems
4. Where on the web will you find the text editor called TextPad!
Answer. www.tulpad.com
5. In what year was Java first released?
Answer. 1995

6. Where on tht web is the prinury download site for JavaJ


Answer. http://java..$un.com
7. '\fhat t)'p(' or Java program do you run with 11K: jav;un tool?
Answa: appliQ.IKll1

322

Appendix A

Chapter Quiz Answers

8. What type of lava program runs in a web browser?


Answer: applet
9. What is the name of the command-line tool used to run a web-based java
program?
Answer: appletviewer.exe
10. What is the name of the parameter passed to the
in an applet?
Answer: Graphics 9

p~t

nt() event method

Chapter 2
I. What is the name of the JDK tool used to compile lava programs?
Answer: javac.exe
2. Which JDK command-line tool is used to run a lava application1
Answer: java.exe
3. Which IDK command-line tool is used to run a lava applet?
Answer: applctviewer.exe
4. What arc two good, free lava IDEs recommended in this chapter?
Answer: Eclipse, Net Beans, or TextPad
5. Encapsulation, polymorphism, and inheritance are the keys to what
programming methodology?
Answer: OOP: Object-Oriented Programming
6. What's the main difference between a lava application and an applet?
Answer: Applets run in a web browser.
7. Which method of the Gr~ph! cs class can you use to print a text message on
the screen?
Answer: drawStr! ng()
8. How many bits make up a lava integer (the j nt data type)?
Answer: 32
9. How many bits are there in a java long integer (the long data type)?
Answer: 64

Ch<lpter 4

10. What programming Ianguagr was Java based on~


Answer:: C++

Chapter 3
I. What is the name of the method Ihat calculales the velocily for X!

Answer: cal cAngleMoveX()

2. What is the base class from which Ship,Asterold, and Bullet are lnherittd!
Answrr. BHeYectorShape
3. Which classic Atari game inspired the game de\'ffiped in this ,hapter?
Answer: Asluoids
4. Wltich type of collision testing does this game use?
Answer. Bounding Redangle

5. Which method of the Shape class doc this game use for ,ollision testing!
Answer: contaln${)
6. Which geometric shape class do the Ship and Asteroid classes use?
Answer. Po I ,gon
7. Whkh geome1rK: sha~ class does the Bullet 'lass use?
Ans...." t:r. RecUngle (Phel
:aceept;Wle)

:loo

8. Which applet evmt actually draws the screen?


Answer: pal ntl)
9. What is the n;lme of Ihe in terface class

used 10 add threading support to Ihe

game!
Answer: Runnable
10. What math function does ulwglellovd use 10 calculate the X "ekx:ity?
Answer: KoIth.cos( )

Chapter 4
1. What is the primary class we've been using to manipulate Vedor graphics in
this chapter!
Answer: GraphlcsZO

323

324

Appendix A Chapter Quiz Answers


2. Wh,t is the name of the Appltt event tmat refn'Shes the
Answer: pilnt! I or upd,te! I

scrttll~

3. WI.... t ill Ihe name: of the Gr.phlC$20 method tholt draws, filled reaangle?
Answer: 1'111 Rect! I
4. Define the words comprising the acronym AWl'.
Answer. Abstract Window Toolkit

5. What class nlakt"S it possible to perform translation, rotalion, and scaling of


shapes?
Answer: AfftneTransfcr.
6. Which Griphlcs2D method dr.tws
Answer. dr'lI()
7.

a polygon?

Which tnnsform method moves a $hal"" 10 a new Ioalion~


Answer: tr,nshteO

8. What mdhod initializes the keyboard lilltmer intffi'a~


Answer. addKeyLt stener! I
9. What method in the RandOll class returns, double-precision floating-point
value!
Answer: nextDcubl e()
10. Which Key Listener event detecls key presses?
Answer. keyPressed!)

Chapter 5
I. WhoIl ill t~ primary class we've ~ using to manipuble bit mapped

graphics in Ihis cmapler?


Answer. Grapht C$20
2. What method initializes the keyboard listener inlerface?
Answer: addKeyLI stener!)
3. What Graphtcs20 method is used to draw an image?
Answer: dralllaage()

Ch~PIe<

4. Which Ja\'a cbss contains \he ')etl. . ~el)


Am....cr: "pplet

S.

Wh~t

melhod~

dass makn it possit>1c to pcrl"orm tnnsbtion. rotalion, aod s<:a1ing of

lmages~

Ans....er.

"nntrra~sfp...

6. Which Graphfcs2D melhod clr;'ws ~n image?


Answer: drawlqge()
7. Which transform method mOI"CS an image to a new 100<lIlioo?
Answer. trl~S lne()

8. What is \he name of the "tn1l5p,armcy" chanDd in <lI 3!-bn PNG image?
Answer: alpha chanDd

9. What is the "-pplet class method u5Cd to lO.:Id a resource from a JAR?
Answer: getRuQurce()

10. Which Xeyl1 stener evenl detcrlS key presses!


Answer. keyPressed()

Chapter 6
l. Whal is the name of the support class created ill this dwpter to hdp the
Sorlte class m.lIlage position and velocily?
Answer: Polnt2D

2. During which keyboard evenl should you di",blc a key-press \"ri:,ble when
deteaing multiple key presses with global variables?
Answer.

key~elused()

3. Which three tvpn; of~nmettn un you ~ to the colildulflth()


mdhod?
Answer. ~~rUngle. SprIte. aoo 'olntlO

4. Which Ja~a daM pro~ides an alternate method for lO.:Iding images that is nol
lied to the applee
Answer: TOQlklt

32S

326

Appendix A Chaptef Quiz Answt>rs


S. Whkh Java pack:age do you nl"ed to impon to use' tM 6uplltcs2D cbssr
Answer. ja\la.aWl.Graphics2D
6. Which numeric data lype does the Point2D class (crea led in this cha pler ) use
for internal storage of the X and Y values!
Answ~ dOllttle
7. Which dab types can 1M Potnt2D class work with at 1M conslructor kIlel?
Answer: int, ftoat, and dOllb1e
8. Which sp rite propeny determincs the angle at which the sprile will mOlle?
Answer. .cnAngle
9. 'YhidJ sprite property determines at which angle an
r~rdless of mo~ment dirKtionr
Ans..'Cr: hceAng1e

ima~

i$ pointed,

10. Which AfftneTransforll methods allow you to tranSlale, rotale, and scale a
sprile?
Answer: trans 1ateC I, rotate( 1. and scalel 1

Cha pte r 7
I. Whal is the name of Ihe animation class created in lh is chapter?
Answer. AnlaatedSprlte
2. From which class does tM nnY animation class inMritr
Answer. Sprt U
3. How many frames of animation were there in the animaled ball sprile!
Answer: 64
4. Whal do you call an animation lhat is slored insi<:k many fib?
Answer. stquertU

S. \\fhat do you call an animation lhal is all Slored in a single file?


Answer: liled
6. What type of parameter does the AntaatedSprt te.setVeloclty melhod
accept?
Al'ISWC'r: Polnl20

Chapter 8
7. What arithmetic opel'31ion

is uS! 10 calculale an animation frome's \'

poliition!
Amwer. division

8. Whal arithmetic operalion is uS! to c.akubte:;m anim.ation frame's X


poliilion!
Al1>wer: modulus
9. Whal is a good cbss 10 use whm you nd 10 CTeate a billl1J.P in memory!
An~wer: 8ufferedlaage

10. Which

A.~1I1:HedSprite

method d raws lhe current frame of animalion?

Answer: drawl l

Chapter 8
I. Whm is lhe name of the method used to enable keyboard events in your

progrom!
Answer: .dd~eyL i

ste~er(

2. Whal ~ lhe n.aIDe of the keyboard ~'enl inlerface-!

Answer. (eytlstener
J. Whal is the 'inual k~ code for lhe Enter ~
Answer: Y(..EntR
4. Which keyboard ",,'ent ..ill ldl you lhe code of.t pressed

key!

Aru....cr: Teehnic.aUy, any of lhe folJowing three (teyPreued(),


keyReleised(), and keyTYl)edl) )

5. Which keyboard event willlell you when a key h"s been relcaS!!
Answer: keyPressed( l or keyTyped( l
6, Which keyboard eve nt will tdl you the character of" preSSl...d key!
Answer: Techn ically, any of lhe following three (keyPreuell{ l,
keyReleasell{), and keyTypedl) )

7. Which (eyEven! melhod relurns a key <:ode value-!


Answer: getKeyCode( l
8. Wlul is the n.ame of the method us-ed 10 enable mouse motion
Answer: .ddllo"seKot tonLtstener()

~ts?

327

328

Appendix A

Chapler Quiz Antwers

9. What is the nilme of the class wed as a

pa~mtter

for all mouSe.' event

methods~

Answtt HousH_tnt
10. Which mouse event reports the actual movancnt of the
Aru;wer: -.;!ustOr.ggtd() or -.;!us~_td()

Chapter 9
l. What is the name of Java's digital sound S)'$tem class?

Answer: AudloSyS!tll
2. What is the name of lava's MIDI music system class?
Answer: HI dl System (Sequencer also acceptable)
3. Which Java class handles the loading of a sample file?
Answer: Audl olnputStrea_ (AudloSysttll also acceptable)
4. Which Java class handles the loading of a MIDI file?
Answer: Sequence (HtdtSyS!e. also acceptable)
5. What type of exception error will Java generate when it cannot ~ a 5(ltInd

''''

Answer: UnsupporttdAudloft leExcept Ion (llnetJuui l.bleucept Ion and


IO[xceptlon arr also technically accq>ubk)
6. Which mcthod of the MIDI system returns the sequencer objed~
Answer: Hldl Syste.. getSeQuencer()
7. What is the main lava class hi erarchy for the audio system
Answer: javax. sou nd.u.p l ed

class~

8. What is the main Java class hiera rchy for the M IDI system class?
Answer: javax.sound .tdt
9. What three digital sound file formats does lava suppo rt~
Answer: AIFF, AU, and WAY
10. What rare aception elT(lT will occur ...men no MIDI sequencer is ava.ilable?
Answtt MidlUnn.ll.bleuception

Chapt~r

\1

Chapter 10
L What is the name of thc inlerface class that provides lhread support?
Answcr: Runnable
2. "'lut is the IUme of the thrnd encution method tlut

)'()U

an usc to run

code inside the separale thn:ad~


Answer. r\ln( I
3. What is the name of the class that handlcs vector-based graphics!
Answer: Shpe
4. Wlut Thrud method G1usn the thread 10 p.lIusc: nution for a spifitd
tim~

Answer: sleep( I
5. Whal Syste.. method n!Urns the current time in milliseconds!
Answer: current TilieHi 1\ 1$( l
6. \\''}ut is the IUme of the: method tlut refums tlK' dirtctory containing the
applet (or HTML contail"lC"l'l lilt?
Answer: getCode8ase( I
7. What i~ lhe name of the method that returns thlentire UR Lstring including
the applet (or IITM L colllainl'r) file?
Answer: getGoc\llIentBase( J
8, Wlu.t class do you use to store a bitmap inuge?
Answer: [lUge or BufferedllUge
9. Which Graphlcs20 mel hod is used 10 draw a bi tmap!
Answer. drawllllge(}
10. Which class hclps to improve g;lJTKpby by proriding random numbc'T'$?
Answn: R,,~do.

Chapter 11
L W}ut is the rurnt of the class tlut lundks bitmaps!
Answn: h ...ge or Buffe'edllUge

329

330

Appeodil A

Chapt~

Quiz Amwers

class in Galactic War ddts when builds hit the asteroids?


Answer: Rectlngle or Bullet

2. Which

3. What is the maximum number of spritCS that can be supponed by the game?
AI1$W('r: virtually unlimited (based on avaibble mmlOry)
4. Which method in the 6riphlu2D class aetually dr;lws the image: of a sprite?

Answer: driw[lIIgel)

5. Whal is the name of the Applet ml.'1hod that rcdra ....'S the window?
Answer: pitntO

6. How many key presses can the game dett at a single tilm'?
Answer: virtually unlimited (limited only by the oper'1lting system)

7. What method do you use to track the mouse's movemenl?


Answer: IlOlIstl'lovedl 1 or IlOlIseDrigged()

8. What type of graphiQ mtity does the game

for the asteroids?

Answer: Shipe
9. Regarding ship rotalion, by how many anglcs can the ship be rota led?
Answer: 360

10. What lm'thod provides the game ..ith RIppon for conision detection?
Answer: Rectingle.contalnsl 1

Chapter 12
1. Which RIppon c!ass helps manage: the position and velocity of spritt:S?
Answer: Point2D

2. During which kryboard f,"\'enl should you disable a key-press variable when
detecting multiple key presses with global variables?
Answer: keyRelused
3. What is the lWTll' ofttK- sprite collision detection routine used in Galactic Warl
Answer: Rectlngle.contiins( l

4. Which method in the Appl et class provides a way to load images from a JAR

file?
Answer: getRnollrce()

(hapter 13
5.

Which '~\'~ (QCbge do you need to impon to U5t the Grlllhlcs2D class~
Ans",1'I": j;I\'1I.~",'.Graphics..!D

6. What numeric dau typt' docs the Po!nt2D class (created in this chaptCfl
for internal storage: of the X and Y v.t.lues!
Answer. do~blt

U5t

7. How docs Ihe liS(' ofa closs such as Point2D improve a g;ulle's source code,
,'er$us using simple variables?
An5\\o1'l": A single I"'rameter handlts t....o \'ariablcs (,; :;md yl.
8. Which propcrty in the Spri tf class detennincs the angle at ....hich the: sprite....ill mo,e?
Answer. ..,.tAngle
9. Which property in the Spr! te class determincs the angle at which a sprite is
poink-d?
AII,wer: faceAngle
10. How many milliSI.'Conds must lhe game uS(' as a delay in order 10
fr.mle rate of 60 frames pcr S('("ond?
Answer. 1000 J 60 - 16.67 ms

achic,",~ ~

Chapter 13
I. What is the name of the mc:lhod thai makes collision lktcttion possible?
Answer. Rectangll".cnntalnsO
2. Ilow many collisions c.m the g;lllle dcl~'<:t within a single update of the g.1111C
loop?
Answer. the ;quare of the number of spritcs
3. Woot would happen ifthc: ship ..-m: to firu projectile that ~warps" around the
SCrrl:;md tlv:n hi~ the shiiY. Would ~ collmn taU place? Why or ",f,y OOI?
Answn: 1\;0 collision i:s handkd brt~ the ship:;md buIJcu.
4. What should happen 10 the pl~YCf's ship ~ftC'r it has bct'n dcstrortd by a
collision with aJl asteroid? DCS(ribc a beller way to "rcspawn" the ship than
whal is currently being done.
Answer: II should hed<:stroycd and respawned. A better way might be to give
the plarer some "invulncrabk" time aft~.,. rcspawn to improve gameplay.

]]1

332

Appendix A Chapter Quiz Answers

s. What typeof lransform could you apply to the explosion sprite to change its
size?
Answer. scaling

6. How does 1M ship's vclocity afft 1M result of a collision ,,-ben the ship is
deslro~~ Should lhe ship continue to am momentum rven while
blowing up~
Answer: ~ ship culTently slOPS when il explodes. A more realistic
aplosion would conlinuc 10 move a little ways along the ship's trajectory.
7. How can the collision routine be impro\'! upon,.so that collisions are more
prKise?
Answer: Either smaller bounding boxes can !x used, or every pixel of two
sprites can be compared (which is usually overkill),
8. What is the name of the constant appli!lo the ship when a collision has
taken place?
Answer. $TATLHPlOOING
9. What is the nameohhe method that updates a sprite's animation sequence?
Answer: updateAntllitton()

10. What is 1M name of the method that handles the game loop for Gabelic
Warr
Answer: qaIlO!Update('

Chapter 14
I. What is the name of the new game engilM' dass developed in this chapter!
Answer: Galle
2, How many sprites can the new engine handle on the screen
Answer: unlimited (with available memory)

simuhancollsly~

3. Which of the four key dasses in the gilme engine ha ndles image load ing?
Answer: '.aqcEntity
4. How many different asteroid sizes does the game
Answer. four

use-r

Chapter IS
S. True or False: Coll~ions are handled inside the game engine.
Answer: nlse (collisions are only detected, not handled)
6. What type of object IS .nl.l.. ge, a private "aliable
Answer: [..geEM 1~7

In

Ml.. tedSprite~

7. Which class is responsible for rendering a single fnunt of an animation in

An,.. tedSprite?
Answer: Sprite (which, inlum, uses l .. ge(ntity)
8. What is the maximum velocity value for the player's spaceship?
Answer: 10
9. What class docs the pmdsprite I"nginl" pass in some of its evel1ls?
Answer. Arli.. tedSprlte

10. What is the rwne ohhc wpport method in A1II.. te<lSprlte that returns a
properly formed URL for a file to be ~~
Answer. get~Rl

Chapter 15
I. What method in G:I\acticWar.java makes it possible to add powerups to the

game when a tiny asuroid is destroYl-d!


Answer: spawnPollerup(}

2. What construct docs the sprite engine (in Game.java) uS(' to managl" the
spril cs~

Answt'r. Unkellllst
3. How nuny ,,tapon
Answer: 1i,,1"

u ~ are

a''ailahle now in Gabdi<: War!

4. How many differl"nl point-value powerups are Ihel"(' in ,he game?


Answer: Ihree
5. What method in G:llacticWar.j'lV3 returns a stock bullet sprite ollj"':I, which
is then tweaked to produce the upgraded bullet spreads!
Answer: stockBul1et(}

333

334

Appendix A

CNpte< Quil AnsweI'l

6. How man)" different asteroid images arc there in GalaC1ic: War?


Answer. 14
7. If you wanted to add another weapon upgrade to the game, which method
would you need to modify?
Answ~ ftrtBullttl
8. How manr srrit~ is Ihe sprite engine capabk of handling at a t ime'?
Answer: unlimited (with available memory)
9. How many bullets are fi red at a time wi th the fifthlevel weapon upgrade?
Answer: sUi
10. What is the name of the static: tnt that rcprescnu the game state when the
game is running normally?
Answer: GAMCRUNNING

Chapter 16
I. What does the acronym JAR stand for?
Answer: la,,, Archi\"(.'

2. What is the name of the program used to work with JAR files?
Answ....r: jar.e:'(e
3. Whal types of files <:an he stored inside a JAR file?
Answer. anr t)l'C of file
4. What eomrrC'Mion m....thod does the JAR format use!
Answer: ZIP compr....ssion
5. What method must you usc in conjunction with the jawa .net .URL class for

loading mtdia fUes when an applct has bctn


Answer. getRtso~rce(

deplo~

in a JAR file?

6. Whal command .....ould you ....nter to create a new tAR file, called tcst.jar, thaI
contains all files in the current (tllder?
Answer: jar cf test.jar "."

Chapter t6
7. What command woukl you entn 10 ont~ 1M SUlK' archivt' bUI also ilKlud~
a manifest file called manikst.txt?
Answer: jar ct_ test .jar ..,nlfest. txt '.'

8. What command would you enter to list the contents of a file called
MyGame.jar with verbose listing enabled?
AnS\'<'er: Jar hf MyGa-e.jlr
9. What JAR paramctn option ClIuse5 files to be added from a diffnent fokln
without adding tlK' fokln lW1K' 10 tlK' files sto~ in 1M archivr?
Answer. C

10. What type o( web server do you need to host a Java applel-bascd gam~?
Answer: Any server will do; applel$ are client-side programs.

335

INDEX

Sw<
-"iIT IAS"ETl ...... l4
ooIdJ(qoU
O _ - . 142_IU

odi-o;~l _hod. JOO-)(IS

....,...*J1

......

A
_ _. _ - . . . . . .... u
lor If"*'. ' __

'M

<l,ffi...T",nsfot'"

"ad.

I'h~.x

obJ't. \10, 'l-94

s... .1," ""nod dl",

_o>r,.

A.....

_t.

A.J~

:!!

lJ

121

"";mo,;"nT... P"'I"''''. 1lS-lll


" ....11< ..<11 kn'<r, U
AI'I'I., Vi"""", wi"""w, UI
16-!O. S,.. .1$4> Galo<t;c ...... prnjl:
,
"'",hi... U.... ~ ........
.fUt<IIlI. 17 10

.r

!.

....... ".",..", wMh, J9

.....riOIint ddI....

'r~"" 1'fOV''''' II

111-181

~J

ss

dnt""" '"'._

...>11

< "RJ. S3
d"wA"mRd,{l n,,,,It.>d kor. ~
d"wRnll.{j """"'nl ~". 61-61
d"w<;b;p;J.,.,ht>d for,61
..,.....t'pdaI<l) ......hod. 6k>4
......
b,_

-_

....--......I.r

''Pb'> of.

_\"'k>f5hartdooo .....
...... d-. Sl>-S1

~.

.......... ! ...
! m l l _ .... la-I.
~

7J.s..-.'",~

1'0..........

" _ _ a,S7

_ _ _ s... 01.., " ' " " _....

&Dod.

--._m

1IfdIj... " " )1.

............ s... "'",.......... . - : G.la<1k


"'or ,....;.a _ _

A.._<dSprio< ~ 1-' IU. 2)7


n;o!o_ ,. ~ ..... """"'.~.
,. ._ U5-IJ,
nploooiuM .. , ......... .... u

-,,"no.-, I

"'.... oJ;~ ..... r>-J'>-.lO


r'>r potvppn.. 31
"",nolo", w;,h,)7
" ...roid 01_
fut '" tt'rl>ld>-....... JoImt. S7
.,. \;.u..,... w.. 1""'1<'<'. 19'-196

<nt;l..., 9

AII'F fil<s. ISJ- I56.


..'"rilh..... 69_7G
AI...,... librur. 1st
..phom......lla,....,_

Oftl_';". ....... lU
"",-iono;m:h_1l

1_"""_1'>-17

Actiq

to9>-.,

-.0< COok

fiI< b . 0;\1

~1.-b.6J

............ ',-)

\l>ip_b,~lo6

Ihrnd.-. b. 6J
"I'dooe<' m<lIIoW.._7
At.... ,~ JI<roid.. 4'J

33B

Index
Atori l'g,w\4
AU fil.., 155--1S6. Su ~I.o sound di[>l
Aud.o<i'y,IS5--156
'h.nging digital ,,011'1, fom"t
...,ln8' in. 158
.ndi... Su rnu. i<; _nds
Au,HolnputS',..m d _ IS7. 1S8-1S9
<fro, "'ndl<,.. 163
Soun<lClip d... 'n<'l"ul1'ing, 17Il-171
AndioSyot<rn .l_ IS'
ro, ~,ndl= 163
Ioodi"tl ",und.. 1S9
Sonn<lClip d ... 'n<'p"'I"ing,
170-172

A....II"

(io"..

Conft"n, 2005. 9

A\\IT (Ab"r'O<I Window Toolkit),

Tool.i, , ..... !loI

to.... nd )8uil<l<. Su )8ulld"


bounding box... Sn Golocti< W., projKt
bounding '"'t.~,... .56-52
roO;,ion d<1""ion. 5-1
8ull" d ....
fo, AsI""id..... y\< pm<, S6-57
fOf GoI..." W" pro;.rt. 1%-197
bnmpScor) m,1hod, lM-:106
bylu, 2'

c
c provom.. )7-.38
C++.1.l, )II
;n.... ri'."'" in, 42
po,....-of.l6
w<b """'" wri"<o in. 24
CI,24

b ed'S""'"d rnnok. 5 m",,;,


boll .ni"'''ion. 128
8...<;amcEJ"ity d ...
..pIoring, 236_237
in G.lxli, War pr<>jl. 108. 195
8...<;a_Enlity.j.ovo.211
8"",V"'torShope d ....
fo, Aslrrnid...ty\< pm<. 5J-S5
in G.I... i< W., projl. 19J-I'}<
fu,.pril'" 111>-111
bi .... II
bi'mopped ,",phies, 89-105
AIlin<T......fo,m obJt. 92-9-1
nim.llion "ri", in. 124
d",wing irn>.g<o. 91>-91
(0' G.I.o"k W., pro;.rt. lO8-2W
Ioodlng Im'g", 91>-91
n".bel PSG i""lI'. , ...."i"g,
9')-102
OMU' im>.gcs. 95--97
p"'l!"'mm ing, 89-90
TooIIO' , .... fo,. ""
,,,",fo,m, .pplying, 92--'14
'''''''I''"n, i""S'" 901. 97_99
8it_pT.., propom. 95--97
block .. lran'P"",n' >10<. <n
81iu..ard En""';,,",,,"t, 4-5.'
bln_"pnS "',k8'O""d im'8<, 208
8MI' (Wlndo..'. 8it_p) fil... 90
bool<.n i.... "i.<{) m<1hod., 184
Bool.. n.. ll-J4

a.kAngI'Mo~J m<thods..

7l_n

in G,I..." W" projKt, 224


Calipri trutSv-e" 91
<a>1I, imoS" 90-91
...noJ gom<' m.rt<1. 6-9
CD ,om"..,.,d. 15
<ho, voriobl" JO.-n
<b...."
1....1.. 36
<ho'm
14l
ch<1ot.. Sn Gal"'t" ""... pn>jKt
<....ck80nndsO _thod. l3-~
<h<dJJutlO"(J mbod, ISO
<MckColli.io... !) m.. bod
fO, kllm>i.Ity\< g.om<. 6ll-(,9
in GoI",,, W" pmjt, 2.2-233
<h.UnpntO m<1hod in G.I."i< Wor

pro;ro

)(jl-JOI

lib,,'Y. H6_H7

Wi..rd dioJog """ 43


, 25--27, l7-1lI. Su ~I." inl><ri'.n<<;
",rit..
d<fit><d.I46
findinS infO,,",,tion on, 31-3l
.....1')' , 1.0.., 108
in... "", of <I.... <,... ing. 31
""" ,Ws., .<lJ ing, 42
s,mp")'V& do.ss., lS-J9
Oipd.... 170-ln
<lfot hoind..... 16J
fO, pI.ring "'nnds. 159-1bO
""",hi< <I.... 169--17<1
..,.nd dil'" pbring, 161
SonodClip ,b.. ,nc.psub'ing, 171>-172
0

<h

J~irr!rri!.rJlr!11~~!{if~~ [~I~~ C jj r!!liIJtrJr~t~~


~'!ff!ff ~ f{l tt~:~~}~-tj!i!{j! .,,!~ ~ i'! 1 Jl-<~ ~ EK" t:' I
1~~~~ilJI~~l : ~!f
ill =. . i r [
t.1
JI i'"I';-~[~~i~ f~~ ~p
f~~~IIR,'.
!!~'~Il'if
_ 3-"
Ii III t _. !
J J"~
~Fl

l.!~__ I
-

~. ..
~~ ~~

2.

~!"~~rl
~

i'
<

~
~

~t

~;"
~ ~

~ c> pwrprp

HlI~

~~~~ c

i -

"'H _, Ifill- _jl fflf

~'I'"""'i'l-".
,="
~~] ~
! ;"0:;;
~.

"

HmUifI
PHf!UUJiHUHh
rt~
Wl,j~~ fi ~ HE! - HnH! I

-1,J'd
i . . ~ '1iI~..

~ ~~ r._

I 'H

,,"

". "

.,
~ P'f
'Ht"
O'

] i' ii

jj.q.[

!I i

~!:

r.

J
~

11lll~,.~fllt'~.r.lll!11!!!,j!,~IIJII'lttl[II'il I1
1IIIt'~!11 ErllJlfll' Ij " ! I!I,~tl!I!ljjl_ r 1111
~~~

I~[r

~~

i~t

~rr

!~l~flt~f~!
~t~!,j~~fltillt~1'1 fl~t~l~
;l;JI;~;~f!
Jilt!
.,'}."
.'"'
J"
-.
.
""_."t~"
".~ (-. ,;:'1, ",'f -" ~~,.;:.
~ ~t ~" """'1
-~1:n.~=

.F-

t'~ ~ 1~

~ .~

>;:r".'

'f

.. Q"'S

Ig~

"HI
'"'1'11;

~..!

"

It

~flH}HHmh uUW11'j till llmt1 ~~llJ UHlli 'I


tti ~J~t~~~
"l~~lf;~
f~!~!!tj!~~ t!l.ll'.l
. , . . . " ill"
"'. tt ,- if~~ll{~~
H ,<t,
t

J ~I; ! j'
rn

t
~ ~!
i
.

"~
~

-t'

~1 "-~~ ~"~.
~ ~

'j;

e-

r~

I.
!:!

~f~~~

;-

lnde~

~~I""'11Il

....,I_...

IU-IIJ

~,-

r..r oppIrtI. II-I'

no.... n...,,~Il5--I"

Gobct>.

e - ~io:rllll;"0...
ni", ,lir;..

"'.I"""i<a."-" r..r.1Il-11~

I"

_K<rVpO ......- . :.-_


1buT., Edpr. lOll

GMltJoIENU . - . 2f7
G M!....OVER mod<, U7~l"
G MI!...PUYING mod., lS7
_.R<f~(J

IBM', 81l1<G<DCfL. lS
III ....yo, )7
IIle. (In' .... '1 dcY<IOpm.. ,
"''''ronm<nto), n

m.. hood,

"'~

..-st>IMcIowu() - - . U),

~ I ~~l

..--..0 -..hod, u.o.-m


_
0 - - . W-~

1. . . .1!n';lf - . IDS
Aftimo~

-"..-..0 "'-'~217~1l1

-'-~

d-.

:Je-:27

'P'* ............. :":-11

r..r~III_IU

...... ~-l7
..... ~\\'.~~I-]9l

I_Eatitr.jna.211

~"'W~,

~krr-d.I'l

.._""'0

Ibr .....<roid ,

57

with lI.I""V<rtorS!lap< ,l.$o, 54


WI,b Hull<1 duo, 1%-197

. .B"01....0 _
ptCnphio()

, 141_148
hood, 1011

"'''''"pO'-bo4, _ I

... fo'_"""'111

~""".I......h'ml.l,.

inb,ri'"n, I II., 4l-U


.00 _Vt<>rSh>1'" ,Lus. ~l-55
io 1,:. . inhcri...... in, 4l
iaitO _booi. 145
wilh.Nd~ mtthod. IC-I~5

""""'""""
........
""
w.

... MJO-"'-'I4,J

...fI:<rCodt ......... 14,J


~l""""'.

ptURL() - - . 1M
TIo< GII>1P, 'J'S, 101

&Iobtl ..aion, l19


&,01:>.1 ..ri.ble> for
lll_llI

--

~,

J'"

;mo

Golo<", Wor project,


'ion ..... n

GPS (pokI

;1

~--'I'"
..It-...Q...-. ,ID
. . _IionV<locity,57

_.-

p<Ojo<cI.lIl-U4

.. III

'" ~ 1-. ,"


""""*
In.... 6It l'al.. ........

'

Dr

9-11

io........ -.14l,17lI

en,wa'-'ll

,-~

s..../ooo .....

""m>fI'<d~_
_b RoNIomSIu.... 1'fOv.'m. 7'J-8O

H
h.n,JJ<ShipColli'iono() method. l.ll-llJ

Ioc."f do.., lOS

1~

l'i lJ of W.,cll>/l. 7
i...'"n'i.'in" 4)
in' in'
'l"J'<'. 27
in'
br.
.16-';
in'
!7-U
1.1<1 eo.,,: 0-. 134

pool
.,. OJOl.-). l'J
~ Sn--."...t~_

............... "...l.l
Cnploia.W - . n.

:27

P-l "'-

Boo--,.

.,.-Wl.... I.

1. . . . . ~,.,l4
1,:,,-':0-

__""Ion.n

IEsuptioe K<06 "'-IkT, 111_1.


in,
I0() ...... hood, ..
IOF.J<pIiom <rr<>< haodl.... 165
Ion S,o...... S-6
i...."ivel ) method. 1M
IT (inror.n..,iom ' .....0010Sl'), 7-&

In'

341

342

Index

J
JAil. Sn J..... A",hi.. UAR)
jac."". prognom, u';ng. J08-.ll0

/a"",

n" C""'plrtr Rrlr"acr, Sro<ath

[diri"" (S<hild'I,.ll

J"" 2.
J
Ja

I~

Appl;c.,;"ns, 10
A",hi... (jAR),?-l
<<>fI'm" of JAR fik. li"iog,)OII
..1ra<1iog fik> from JAR filt,)OII
Gai><1ic W" pro;.c, in IAR fik. I",bging.

.llo--ll)
;O r.,x< Prosn>"'. u""&- J08-.!IO
",. nif<>! fi...
fo' Galactic W.. proj<ct. 1I1
wmking wilh. llO
!KW IAR fik. ","'ing..108-)OII
pxuging 'prkt. in. )07-liO
l"'h 1o IAR. ><t'ing, lO8
updaling JAR fil.... J(l9-l10

J...

l
Thr lIgrnd 01 Zrida: A Link '0
rhr Pa.., 4
LEeO,6
I<nglh mnhod, II

Bb...ri<s
Alkgtn libt. <y. 189

""t.

b",h-;" ,"ppor1
2.1
pthrndlibrat)'. 189
lib....,. d
fa, ....Y" l4
li"<Una i1.bkEx<<rtion <tTO, !>andl, lfiJ
linkodl;". In _
<agin<, 239-240

,,,"'p;!,,,. II

for i.lfinlJ..... P"'S"''''. 15- 16


..ing, lS
J. .... lkv<lopm<nl Ki' ODI::), 10--11, 26, I<S.
Sn 01." J..... Archi .. (lAR)

/n"" PTl>grn""ning fo' tho Ab.alat, B<ginn".


Snand [diti"" (f1rnlJ, II
I. .... Ruo';10< Environm<n, (JRE), 10. lOS

.nd d." t}-p<>. ,8


J Scr>'''' P.ge. (151'), 24
J Sound API, IS5-IS7
J
S"nd.>nl Edi'ion 6 (J ..... SE 6), 10
ul>J".. '0, 11

J..... Wd> ScfY<, (lW5). 24


ja",nfl.URL d .... lIO
j......"lil.Unk<dLi>' ,Lou, 2.1?
javu......nd...mrll, IS5-156
impo"ing, 1~7
JBuildr,. lS,)2
;'..,.IOund.,.mplrd. imponing,
loa.r.o

K<yU",nr, inwfa, 69, 142


in Gal><lk W., proj<ct. ll1_11l
with ROI.trPo!ygon J"OP"'Il. 8l
~<yp,eaoe<I() mett>od, 69. 1<1, 14l
in G.... <1i< W" ptoi', 211
k<yR<IftI~O mnhod, 69, 142
in GoI><lic W" rtoj<cl. 211
'u)'T'ypc<I() m<thod. 69, 142, 14J

un"x, 2J

C ++ . nd. U.
<,,"figuring I, ... fat. 12
li>l<o.,. ....'hods, 141_1<2
1"5i"i<& ~fappl .... lllI
10"5 in'<!Iff 'lJ'<" 27
loopL Sn also gam. loop
i'",,';ng 'hrongh. lS
GoIo<1;< 1'1'" pro;.c,. urJ'" for,
lSJ-lS6
"",nd dips. pl,oying. 161

""ys,

'~7

Moc OS, C++ .nd. 26


moeros in T""tr,..{, 18
M.gic Woad ,001, 11'-102
""';n ,Iaso d<finition fo, GoI""i< War pro;":t,
211_212
moin fu""!o", 19-4O
monil... fil<L 5.. I'''' Ardlivr (JAIl)

M'rrr, .!6-37

M~riJlr

k<}-boanl inp"" 141_1<S


in A,teroid'."y!< gam<, 69-71
in Gal>"i< W. , proj<ct. 2ll_224.
261_26l
KeyboardT.., program, 14,l-I<S
k<yEvrn'U "",hod, 1<2
k<}- <0<1<1. 14.1

m. ds f~, ' ... n,p.a..ncy I.yao,


99-102
Thr M.trix Onl;nr. 6
MAx.,IIALUE property, 2B
m<mory
.....ys, .llocating 10. 3-l
sarbas< >1\1;on .nd. 26-27

.i"....

Bla,t, 9

Index

............ Ikt ./... opifo< - - - .


Iloolt.- b. )2-JJ

-"-'--

.. OOP,......--.:t ..""

........:rtl'-.,
.""'""""'"

~s....J..,I
C~.

-'

.... ~

~",""711

_ _ .1_14

'-_~71_7}

_bln'o.lS

1 _ ~ S<rwr ,115.2)
1<. _ _
Ian .... II_lZ

MIDI ..... I "


NIDI _ _ 161.

,.14

_r-

~I...-tI ~ ~

II'
_

5.u..._.....

i==' _ ' -

]I

...

-)6

1"1.... 1"

.. Galaru< 1<.... _ _ l:I4

IoodooI& \IIDlIiIcs. '66-1'.

P\.q'M""" F'"'F"" 167_16'J

M"',..........~-.17!_I.~

~liI<o.l6f>

"""1M .... "...,.,."'" 167-16'1


,"""""" ,bloc>, 169-170
.\(;<lJS<q~",,~ cl
177_17'
Mld;Sy"<m d
166_167
Midis..I""""< d ... <",",p",l>ti0llo In-17~
Mid".. y. ~
MIN V"IUfrrormy.~
MIolOIlPG ( ,..;...11 molti"""" 0fI11...
rok-plaji p_~ 7
............ <Aanod...., l IS

_,/.,.a.,

14~ 141

.. ~ 1<.. rr..,ct. 2}1, 26.2-2toJ


...... -_I_I~~

-....14i-1l,2

,"_I -.

N<lll. .n.. lS

Ninl<od", 4
No'q>oH!

'1'1'1<1.. , ... ,,"10 17


J"OP1'm, """J- 16
IlTMl t\lt.
314
nodl-,.,.nl....
Ii,;,,&

lIU-JlU

~1-'147

_.,.........---.'47

.... '"

-....I~I

M......E

t.a.;.a......... ,....

' po""",, '47-141

.........E>:I,<.I() .....- . 1.7


.... io..

I~I

l4li-l.7

wiLh ll<>"I~l'ol'l"'n p,"V"n. $.J


M......:Iol .. lonU'I<n<' In' <ria, I n
_Mo,....n
147

......... IS2
.........1' 0 M<thod, 261_U3

_l'rfto<dIl-.tlood, 1.7
,-.ISI
~1"'-'147

I<M1JII. lSI

_ 1... _141.--1502

Ii

w;.m.. S.IM""- ~

.......
,*
lell;o---,............,,_

<lOP
..... hO:loIrog._I
.....pq4_ 41 .... 2
~.42 ....J
................ 1

~4l- ...

_ " " i.......

I~I

Mo~..lh...... ;n,<rfK~.

..

eII;o-,-I , . . . . _. . Sn 00f'

_Emft!ll - - . 147
_,

C.,
"""i_

...... ,'~5-ln

-tlr\S--.

N."""",.
.NET 'Iu>olotl'. 14

","",'"

ro, bi'~ r;nphic<, 9S-97

<>Y<rloodi"" U

p
"o <-Mon,~,

6
poin'O ",~,hod,
lOt Au""",._ .. y\< IOn><. 100. 6.l
to<

("...lo<1"

It,

w., jH'<>IK'l. ZI5-.l.11

""I'I<mml'... 1'1

"'-"'P"'.I,".I~

--

...... Sloop ..... ,.


.....ml ""'; ..... ~ 110-101

............,1 '

343

344

_.

Inde~

pIMb. SN '.........-cT Ioyn>

......... _ - . - . . . . 1
l1oy"'_ _ 161_1"

1'\orSoo-oI,.....-.
I'NG

liIot."

... GoIarn<

. . .... ,. _IJ)-Il
Sl(;11 (Rt<I, G"""," 11I...1.

,.,

R.ahoBIi':. ,

~_(UGoI..

1.......1..

....... ..... u-.

w. ~ ztt

.-0 -w. tJ,

-'P

~ I'NG . . . . ~ "'-Ill..!

~ia6x--."

_lOdo. ............. l09-I.O

_v.;o-m
....... n
IWIdomNrpu

1M

l O r _ & . - . . 127

_~~II5

......... - - . tJ-6oI. SN./U.-o

-~

............... k<ywonI WIth, 1.2


ill Simpl<loop I"Ol!J"O"'. 17&-110
.... Iht<'oO<li'll <uppon. 184

1I-aJ

~"l!wi""80-13

polym<>tvhum, ~
pooltlotl. opri1< cl....... ndll~.. lot

row''''P'- ~2. s.... ./,a ".I.<tl< W., p.ojt

l>rojrr' Calh.m Rolr;..g. 9

Sompl<l... cl.... .I3-.I'I

I'Tok.I~.

R.in". 91, 2/l8., 109. 2211


p"'f'<11i<s. s.. I'&riobl..
p1hrnd lib..".. 189

oo:oIi",1II._ 1)-16
Scl>ildI.. H......... 31
GoIo<tic: Wu

-....... s..

I'f"iect

StpGt-n. 4
~wll!l....,..l1

s..,...... <I-. 11}-11S


_

--...-_71-10
~

do., 1

167
_ _;.;.

R
....... <1-.1.
.......1. . . .

_':-94

......."-'7P- _
.I-aJ
,
9p_"""

...... .,.....oa.50

......u- .. ~(n5)_.
~n.s...a1s<>-"~
.....p"" md, 10
~pnt<

duo coIioion .... roio>n. III

1'<1"....."""""""

(..". A<lmHa"->lr\< p"". 6J


i~ G.Iaci'" W.r proj1. lS}-2:1l>. 2&9-191

ur<l.t<O n><thOO fo,. III


M.I~.r'. TU
coU.lon. 201
........Ind.. < """.... 115
""",intO mnhod, 111
aU 1<>, 189

...,.a.no -'-L II

-~)

-'-L 2II1-la

.......... "'" GoIatic: W.. pnojert, 2m


wpont< _

lOr. 2JS

......... 1_171)

..... .-..~II.

_V<ioociry()

--

.a-l...-

II

liIll_
"

11Zo-11S

51

5'

"""""" WIth, 11-1

SIup cloM.. s... .1.., Golonic: W..


.... ~>lr\< pm<, 55-\06

JI"'IIcd

....... InlOV' Iyp<O, 11


abowBoundo(} "'<thod, 212
ihowS'OIua() ....thod. 1&1-150. 119
Sitrn Ent.n.in"","
SimpltOo.. Pros......2-01
Simpkloop p ~ 171_110
pPda.tc(1 mclhod, oddins. 181-112

.;........................ ....,..l1
- , 0 -.hood, IV-I.
SSE$, 4

Selorio, C

-...l

s....

boo (lip do.


...
Ii pU.,.. 161
.............. I~I6J

Inde~

;" G.Ioo:,,, 1'0.. pooi'. ZS4


1ootolIftJ .............

ourt() .............. IU-Il)

- - ' dIvo.

110

~ I_I~I

..................... It.o-lg 1M-I,,",

....... ,..... 01

~"""'I"""'I7\l

......

..odB.Bdtl ...-. .-s

~I<>':

......... . . - dipo. 16J-1604


~dMo,l_1T.I

.-.. s.... .1... Clip daoc Mlolii_


~"-dip<
' " ' "'" b WNnd cook, l~; ll>l

.. {.....,'" 1'0 .. 1""IfCI. l:I4

....0...-.

.,..,.....
"""""'' ' _)(11
'l'ril<

.ni ....' ....


.....1"".."
1)z..lJ~
fum'''.... f<", ,>kulo"ng fram l1S
,....i.,J""1 t...",... d""'ing, 1:4-115
Ih"'qu.. 1'.", l:~
l<>Iin(l. lZ' 111
I,",Lng
n<>, 120- III
Spril< do>&. 101. S .1
~1010

.n,,,,,',,,,, (

sm.cc:lo-.,-JZ

-..."

S - M ~............

P...... II

T
T.i,

s.... al... colli.ion ckt",,'inn

,...j

~h<

W., rn>I"" .1Il-Jlb


1411-1~Z

........,

.uo--n

,10K _

....... ll

S<tp Mona 8"",_ 4


~' ..... l.lI
~ ", ilily. "'1-' Coo>lrtIl

...... - " " '.... 11.


fnr ( ......~" \'i.r rn-t. 2In
~["",.

,'l

Stn.c "'PI : , . . - " . "

,.,

.."....-

~1~160

"fM I~"""""

t.-Mna."

~U9

........N.- 'f'I'I'< dooo.~ II.


_ . <Odo< b. 114-11'

>vrit<
Sp<iI. dooo, 1I
_
i
ZlI
T

117
1l1

ZlI

~1'~"
.,.il<{Ae;.....O . . . - . lS6., m-l6.l

opplco.. . - - . I"

~I"'-'Z"
fot \
1'0. P"*"- ~ _ ~
Spril<.jo
III
.,n.... fiN .1... GooIo<ti< W.-pnoj<; .....

HZ-It)

'hrad

,10K fnr. liD-Ill


...... ~ n><d>od fnr. H __W
I~hn"'y

II~

..

fnr. Ill- III

phl&'.,nm,"S

,,"'r'< >P"i'' ' . 101_1[1'/


114

~mpk 'p,n... ID7-111


....i'<O(1
_ . Z4II
.sp.;l<T
JH'<IS'*"'o. 1Ill-l21
.,-ll<lIpd.<'ril _hod. HD
'" {~'" 1'0.. 1""IfCI, lSI>-lS9. ltl-l'J4
Stor 0pn0fli. C... - . Z6

S,., '\..... '

,IJ7-I..
'1...., _

1Il-1II9

Ino- J\<J....,..t,.- .. yk gon><. l


II.IndI<n Ino-. 1.7-1~
",,,hnhr<ooJ,"Io 171
wilh Runnobl. ,n"'ot". d .... 1114
fu' w"nd d; .... 170
...nll ..... _Ino-.Ifl.l-IIS
Thr<aolNLoor P....."'". las-la9
TOr

P",n,lll ,\000 lor. 109-110

m,.. hk 'P' ;'" d .... ""'ing,

"""" " II
duoo,---.,

.... ~ _ a 6 l " ~ I.........


1110

cnlI_ do1,,"'

..

.-.<l

fnr~-

.....

_\'''-'"

u:

51., " ..., C.I........


51 .c"'/I. "S
51..,.r: no. '-I C"'-J, l

....._-_ ..-...T h ~ roIL

1171

Iii, )7

..........'ioIl ..... bod, 11.

.......

T.... ""
.
T_c:Io-.,94

,17

345

346

Indt~

T"""",_._

v...... f.Iotity() _had.

..... F ~ ......... U7

.... ,.

....U7

~...-en-n

. . - . 0 _willi

...

~_eL,-,n

,.,...aulI_
.......... IU-ltJ
..,........Id>....'-Dy _ _ lU-ltJ
~

"-,,,,71-71

Goio<b<

r. -.ppm ~ M, '1-"
r.._lo.l

amp. ls-31

I~IlI6

-~
.....
"

~"""''''''''n-M
t~~'"
~W_

..-

w. -".......-s r.. 11"-U1l

.,.....
__

ll-ll

tOl

. . 14--1}

...m.-. II

YIrtoool kq....D, In
ploriomo- ...........

I'~

IluI<, U

V'

llJlui( C~_ f'ro>;fr_.....


50...1 1;4;';0'" }6
11....""1 U"lvm.<l G&n><o.
void dnl<oy\) ",,,hod. Is<
void "."0 m<ll>o<I. 1114
Vi

u
lIft....l
... l. 9
U,,"'., To"'''~,,,,,,, 1/1. ,
~ftBi

un.uppOrtcd 61< format <rro,. 157


lIft..."""rtcdAudioFiIcEJlcol"..... <......
looftdlot. 16.J
.pcLol<O M<th.od
r. ~-"'l'I< pm<. 60-61
r. GoIoctI< W., f"'Oitct. llS-221

...

~ 0<IftII .......

Sompl<I.<>op _ _

_7

odd.Ic ....
_

......c5IoirO
- . t6
UaL.o (\1

"",,0 ,_hod. 1M

W
W.rCN" Mrico,

-..0 .-tloo<I,

... _-

4-}

17' Sn./SO -""" di""

WAil fila, 1-1l6.

III

........-..0

void

i.., IlK r........

Ilt_tll

.-s)

Golo<ti<W. f"'Oitct ifllAlt ilot.~ liO


",l.'nll-mod. 160
1IlL. _ ... 159

- . m-

s... GoIo<ti< W...

- " - . . . . . ..... 10.

s..-./so ..........

..... ..,,114

...u"'
........

"'..._ n
I\

'I\'.,c../f. <-5,.

r..IHJ
7

_ _ .16.)-160

opnII<> .... """"" V)

v
...riabI<o, 40.

Sn .,"" ..... ~

a1aorith"" and. fIIJ


to, V*Ph....

~1'

SC....,."liting vecto, ,lul.cs, 19<...197


p,"S"'mnung. 77_78
.inp.d"u..... ionol
l1

"""yo,

Z
.......,...lty _tloft......ti" l0 10

license Agreement/Notice of limited Warranty


By opening I"" oeaIcd diK conlain... Ln IMI book, )'OIl1gft 10 th< folIowU>B lemu
and coDditiotu. If, ..pon lft<iing th< followinB ~ """'-"1 and .....ia '"
Lilllil"", warranty,)'OIl ca..- Igft In th< I....... and eoo><Iiliools M1 fnrth, r'dlOnl th<
......-1 book witlt """l"ft'Cd dioc In II.. J>l- ~ f'O" porchaKd i1 for refwML

license
n.. mdoJcd ~ is copyrichlCd"" tit< coprtichl hoIdttfl' indial...J on 1M fIOI'tw= due.

"""""ttr

Y.... .,., b<..-IIO cop,. th< ooItwar< _


....
lOr .... br .u+ ...... and to.
bod..p disc. Y may 1>01 .....,.ooua, maR cop;.s, Of dislribul<: copia Of mil Of ~ 1M
M>fi:~ in
or in put, opI wilh writttrl pmnisJion "'d.. copyriBhlltoklct(l). Y.... ....,.
lransfC'f It.. mdoJcd <fuc only tosrth<r with this Iicnu<, and only if f'O" dtslroy aU oth<r COP'"
of lhe toft...... ~"" lhe Ir:onlk'.... vea 101M lmm oftb< I"'........ Y.... may nol dccompil<,
~ -.nbI<. Of ............ nISi- the IOfh.,,",.

Notice of limited Warranty


The enclosed di$< i, "';".... nled by Tho,ns(ln COa.... Thnology I'TR to b< fi-... of phy.ical
.1"11 in nl.teri.l nd wo,km.n..nip ro,. VCrioo (If .i.cy (60) days fronl end u...,'. purch ..,
of the bool<Jdi.., OOnlbinotion. During ,he .i~ty.dar
of lhe limited ....rnnly. 11>omoon
ColO..... Thnolog PTR win l'fO"idc I ~Imcnt di$< upon the relu,n of. d<fti.. dis<.

'rtm

limited liability
ruE SOLE REMEDY FOR 8REAOl OF nilS UMfTED WAIUlAt\'TY SHAll CONSIST
F.N11RELY OF REPl.ACL\IE..>n' OF mE DEfECllVE DIsc. IN NO EVL'lT SHAll
TIIOMSON rouRSE TECIlNOLOGY I'TR OR mE AtmlOR a UAIIlE fOR ANY onlER
DAMAGES, INCLUDING LOSS OR CORRUYnOS OF DATA. OIANGES IN THE
FUNCI'IONAL OiARACTEIUS11CS OF ruE HAKDWARE OR OI'EJlATTh:G SYSTEM,
DELTUUOUS IN'Tl:RAcnON Wmi OTlfER SOFTWARE, OR,.u..", OTllER SPECIAL,
INQDEZ-1'AL OR COSSQUll."'AL DAMAGES THAT MAY ARISE, EVEN IF TliOMSOS
rouRSE TEOISOLOGY I'TR AND/OR mE Al1T1iOR lIAS PREVIOUSLY BEEN SOTlAED
niAT TIlE POSSI81LITY OF SUCH DAMAGlS EXISf"S.

Disclaimer of Warranties
THOMSON COURSE TFOfNOUX;Y I'TR ASD TIlE AIJl'HOR SPEClFlCAllY DISCLAIM
ANY AND ALL OTHER WARRAJ'l'm:s. EITHER EXPRFSS OR IMPLIED. INCLUDlNG
WARRA~1'IFS OF MERCIlA~1'AIlILlTY, SUITABILITY TO A PARTICULAR TASI( OR
I'URI'OSE. OR FREEDOM FROM ERRORS. SOME STATFS 1)0 NOT ALLOW FOR
EXCLUSION OF IMPLIED WARRANTIES OR UMITATION OF II':CtDENTAL OR
CONSEQUENTIAL DAMAGFS. SO TIlFSE LIMITATIONS MIGHT SOT APPLY TO YOU,

Other
Thit Ap'mmt is .,...,.....J br thclowJ 0( lho SUte 0( M-.chllM11O wil"""l reprd l<l dooice of
bw prilIo<ipI<a.
U",ud Coafttllion 0( Contr:octs for the 1.o,cnwi(>Qol ~ of Goods it
>pifiaIy tIixIwn<d. T'hio ............... OOftAilul<S the alii............. bcIwccn l"-"" and
Thn.oIov PTR nopnti"l"" of It.. ""'-ro.

n..

n.c-- e-.....

Vous aimerez peut-être aussi