45
Session #3322 Java For The Fun of It! Jack Frosch Frosch Enterprises, Inc. [email protected]

Session #3322 Java For The Fun of It! Jack Frosch Frosch Enterprises, Inc. [email protected]

Embed Size (px)

Citation preview

Session #3322Java For The Fun of It!

Jack FroschFrosch Enterprises, Inc.

[email protected]

OverviewOverview• Introduction• Game Development Overview• Air Campaign Commander Demo• How It Was Done• Resources

IntroductionIntroduction• About Me• Why Game Development At All?• Game Development in Java?• Disclaimers & Caveats

Jack FroschFounder & President Frosch Enterprises, Inc.

Jack FroschFounder & President Frosch Enterprises, Inc.

“Just 'cause they're paying us doesn't mean we can't have fun!”

“Just 'cause they're paying us doesn't mean we can't have fun!”

About MeAbout Me

Why Game Development At All?Why Game Development At All?• Games Are Fundamental to Our

Nature

• Helps You To Stretch Your Thinking

• To Balance Technical and Natural

• We All Need A Break

Game Development in Java??Game Development in Java??

Why Not?Why Not?• Since JDK 1.02

• Java APIs have come a long way

• Java VMs have come a long way

• Java is showing up everywhere

• Hardware has come a long way

• Linux + Java = Sony's PS2

• Doing Java For The Fun of It!

Disclaimers and CaveatsDisclaimers and Caveats• You know you have to learn to walk before

you can run; this session is to teach you how to crawl before you learn to walk!

• The session is about the basic elements of game development and doesn't address AI, multi-player, or network gaming. My game is a beta of a prototype to an alpha version of a "real" game!

• Legal Disclaimers: Java is a registered trademark of Sun Microsystems; Borland is a registered trademark of Borland Inc; IBM is a registered trademark of International Business Machines; PS2 is a registered trademark of Sony Corporation; JavaOne is a registered trademark of Sun Microsystems; JBuilder is a registered trademark of Borland Inc; StartOffice is a registered trademark of Sun Microsystems; Magic Draw UML is a trademark of No Magic, Inc.; Websphere is a registered trademark of International Business Machines, Inc.; PalmPilot is a registered trademark of Palm, Inc.; these slides are Copyright 2001, Frosch Enterprise, Inc. All other trademarks and copyrights are retained by their respective owners. ..........................................

You Are Here…You Are Here…• Introduction

• Game Development Overview

• Air Campaign Commander

• How It Was Done

• Resources

Game Development OverviewGame Development Overview• General Game Design

• The Java Technology Platform

• Java APIs Useful For Gaming

• Development Tools

General Game DesignElements of A GameGeneral Game DesignElements of A Game

Some central theme A Beginning and End

Fits Into A Genre

A Sense of Drama

An Unknowable Outcome

Fun and/or Entertaining

A Setting Somewhere Else

Characters or players, either

human, machine, or animal

Suspension of Disbelief

Java Technology PlatformProspects for Game DevelopmentJava Technology PlatformProspects for Game Development

• J2ME• Cell phones & PDAs

• Game development exploding on this platform

• J2EE• Back-end servers

• Support for multi-player games on Internet

• J2SE• Desktops & Consoles

• Game development is growing

Java APIs Useful For GamingJava APIs Useful For Gaming• Java Swing (included in JDK 1.3)

• JavaSound (included in JDK 1.3)

• Java2D (included in JDK 1.3)

• Collections (included in JDK 1.3)

• Java Media Framework (add-on)

• Java 3D (add-on)

• Others (JAXn, Batik)

Development ToolsDevelopment Tools• Java SDK and add-on APIs are free

• Here's What I Used• JDK 1.3.0 / 1.3.1 and JMF 2.1.1 (WPK)

• JBuilder 4.0 / 5.0 Enterprise

• Sonic Foundry Acid Style 1.0c and Sound Forge XP 4.5b (for Sound Editing)

• No Magic's MagicDraw 4.5 (beta)

• MS PowerPoint 2002 (Presentation)

You Are Here…You Are Here…• Introduction

• Game Development Overview

• Air Campaign Commander

• How It Was Done

• Resources

Air Campaign CommanderAir Campaign Commander

Air Campaign CommanderAir Campaign Commander• Theme

• Design of The Game

• Demo• Configuring Forces

• Game Play Demos

Air Campaign CommanderThemeAir Campaign CommanderTheme

• Capture The Flag

• Two combatants wage war using modern aircraft for attack and missiles, anti-aircraft artillery, and radar for defense

• As Air Campaign Commander, you get to acquire and organize assets into operational units

Air Campaign CommanderDesign OverviewAir Campaign CommanderDesign Overview

• Java 1.3 Standard Edition

• Swing Based UI

• Custom drawing using Java2D

• Persistence Using JDataStore

• Sound via JavaSound (through JMF)

• Media via Java Media Framework 2.1.1

Air Campaign CommanderOO Design OverviewAir Campaign CommanderOO Design Overview

• Asset Classes

• GUI Classes

• Miscellaneous Classes

• Collections API

Air Campaign CommanderOO Design: Asset ClassesAir Campaign CommanderOO Design: Asset Classes

Air Campaign CommanderOO Design: GUI ClassesAir Campaign CommanderOO Design: GUI Classes

Air Campaign CommanderOO Design: Miscellaneous ClassesAir Campaign CommanderOO Design: Miscellaneous Classes

Air Campaign CommanderDemo: Configuring ForcesAir Campaign CommanderDemo: Configuring Forces

• Combatant Editor

• Asset Editor

• Building Units

• Deploying Units

Air Campaign CommanderDemo: Game PlayAir Campaign CommanderDemo: Game Play

• The Ultimate Irony

• No Sound Effects, Video, or Music

• Sound Effects Only

• Background Music Track

You Are Here…You Are Here…• Introduction

• Game Development Overview

• Air Campaign Commander

• How It Was Done

• Resources

How It Was DoneAn Overview of Key APIsHow It Was DoneAn Overview of Key APIs

• Java Swing / Java2D

• Collections API

• JDBC/JDataStore

• JavaSound

• Java Media Framework

• Noteworthy Core Language Elements

How It Was DoneJava Swing OverviewHow It Was DoneJava Swing Overview

• Classes in javax.swing.** & java.awt.**

• The Modified MVC Model

• Containers

• Controls

• Layout Managers

How It Was DoneJava Swing: Modified MVCHow It Was DoneJava Swing: Modified MVC• MVC = Model-View-Controller

• In this architecture, the presentation layer (View) is separated from the data (Model). The management of messages between the two is handled by a Controller

• For better performance, Swing uses a Modified MVC architecture.

• The Swing approach is to put the Controller functionality in the GUI control, but still keep a separate Model for storing the data

• For Swing developers, the pattern is simple:• Construct and populate the proper model• Construct the GUI component• Add xxxListener(s) to the component• Bind the model to the component• Don't be afraid to subclass models!

How It Was DoneJava Swing: Container OverviewHow It Was DoneJava Swing: Container Overview

• Windows: JFrame and JDialog• The main application frame window is a

JFrame

• The editor windows (AssetEditor, CombatantEditor, etc.) are modal JDialog windows

• JPanel• A generic container for holding other

controls and custom drawing. The nesting of panels is used to support more complex layouts

How It Was DoneJava Swing: Window TipsHow It Was DoneJava Swing: Window Tips

• All windows use BorderLayout by default• When adding components, don't add directly

to window, but add to contentPane() instead; e.g.

• Windows have no size by default, so you must call setSize() to initialize the size of the window

• Somewhat surprisingly, windows have no close() method; call win.dispose() to send WindowEvent to WindowListeners

How It Was DoneJava Swing: JPanel TipsHow It Was DoneJava Swing: JPanel Tips

• All JPanels use FlowLayout by default• JPanels are very lightweight, so don't be too

concerned about resource usage when nesting them. They are NOT derived from awt.Panel, but are lightweight JComponents

• It's easy to create a border around a panel: panel.setBorder(BorderFactory.createEtchedBorder());

• Do custom painting using Java 2D in a panel that subclasses JPanel. You usually only need to override the paintComponent(Graphics) method. In this method, you have to redraw all graphics that need to be redrawn

• Components are added directly to the panel

How It Was DoneJava Swing: Control OverviewHow It Was DoneJava Swing: Control Overview

• Swing offers many rich controls for a game developer; i.e. JButton, JCheckbox, JComboBox, JLabel, JList, JRadioButton, JTable, and JTree

• JLabel is a wonderfully versatile control. Not only does it display text, but it is used to display graphics

• Each control can dispatch one or more message types to one or more listeners. This allows a great deal of flexibility by promoting "loose coupling". For instance, all JComponents support mouse, keyboard, and focus events, while buttons all support ActionEvents, and JLists and JTables support ListSelectionEvents, etc.

How It Was DoneJava Swing: Layout ManagersHow It Was DoneJava Swing: Layout Managers

• A Layout Manager enables the proper presentation of controls in a container when that container is redrawn

• The key concept with most Layout Managers is understanding how a control's preferredSize dimensions will be dealt with: honored, ignored, or resized

• BorderLayout, used on windows by default, divides the container into North, South, East, West, and Center regions

• FlowLayout is used on panels by default• CardLayout is good for stacking panels• XYLayout /null Layout makes prototyping

easy

How It Was DoneCollections APIHow It Was DoneCollections API

• The Collections API classes are in java.util.*. Not thread-safe by default, but can be made so easily

• The game uses HashMap (and a derivative), HashSet, TreeMap, and ArrayList

• xxxMap collections have unique keys, but can have duplicate (and null) values; xxxSet collections store unique keys and no values

• Hashxxx collections are unordered; Treexxx collections are ordered

• ArrayList is a great substitute for Vector

How It Was DoneJDBC & JDataStoreHow It Was DoneJDBC & JDataStore

• Data in the game is persisted locally in Borland's JDataStore object database management system

• JBuilder provides a great set of JavaBeans that hide most of the JDBC complexity. The game uses the QueryDataSet (like PowerBuilder DataStore), DataBase, Connection, and TxManager beans.

• Using an OO database generally made persistence really easy since no OO-relational mapping was needed, but it often got in the way during development

• Performance was surprisingly good

How It Was DoneJavaSoundHow It Was DoneJavaSound

• The JavaSound API is included in JDK 1.3 and the Java Media Framework

• JavaSound supports playback, recording, and MIDI

• Since the game uses the JMF anyway, the separate JavaSound API isn't used

• JavaSound class files are in javax.sound.**

• Note: When you download JDK 1.3, you won't get a MIDI SoundBank and need to download one separately

• If Time Allows: JavaSoundDemo

How It Was DoneJava Media FrameworkHow It Was DoneJava Media Framework

• The Java Media Framework API is an add-on API and handles time-synchronized rendering of audio and video

• It is an amazingly rich multimedia player capable of capturing and playing audio and video, clips and streams

• The game uses javax.media.bean.playerbean.MediaPlayer

• Need to set up a ControllerListener to handle events dispatched by the MediaPlayer

• V2.1.1 is much better, but still has quirks. Recommend playing large files in "static" situations, not during game play

How It Was DoneCore Language Elements: RandomHow It Was DoneCore Language Elements: Random

• AirCampCdr.CustomRandom extends java.util.Random to generate random numbers, sometimes with bias

• Math.random()• Seed set on first call

• Math class is final

• Probabilities calculated:• Radar Detection Probability

• Firing Probability

• Hit Probability

• Damage Probability

How It Was DoneCore Language Elements: Arrays & ComparableHow It Was DoneCore Language Elements: Arrays & Comparable

• java.util.Arrays was used to sort Collection data; i.e. Arrays.sort(arrayList.toArray() )

• Objects in ArrayList must implement java.lang.Comparable interface which specifies a compareTo method

• Classes implement the compareTo method to specify how object's of that class are to be compared to other objects

How It Was DoneCore Language Elements: Timer & TimerTaskHow It Was DoneCore Language Elements: Timer & TimerTask

• java.util.Timer and java.util.TimerTask

• GameTurn owns a Timer and GameInstance owns a GameTurn

• Subclass TimerTask and code your own run() method. Then pass that task to one of the timer.scheduleXXX methods

• When the Timer fires at the interval specified, the TimerTask subclass' run() method is executed

Bonus DemosBonus Demos• Java 3D

• Scalable Vector Graphics (Batik)

You Are Here…You Are Here…• Introduction

• Game Development Overview

• Air Campaign Commander

• How It Was Done

• Resources

ResourcesResources• JBuilder (www.borland.com/jbuilder)

• JMF (java.sun.com/products/java-media/jmf/index.html)

• Swing (java.sun.com/products/jfc/tsc/index.html)

• Acid Style and Sound Forge XP (www.sonicfoundry.com)

• Magic Draw UML (www.nomagic.com)

• Gaming: (www.javagaming.org; www.gamasutra.com)

• Movie Trailers: (www.killermovies.com)

Thank You

Please fill out the speaker evaluation

You can contact me further at ... [email protected]