You are previewing Beginning Java 8 Games Development.
O'Reilly logo
Beginning Java 8 Games Development

Book Description

Beginning Java 8 Games Development, written by Java expert and author Wallace Jackson, teaches you the fundamentals of building a highly illustrative game using the Java 8 programming language. In this book, you'll employ open source software as tools to help you quickly and efficiently build your Java game applications. You'll learn how to utilize vector and bit-wise graphics; create sprites and sprite animations; handle events; process inputs; create and insert multimedia and audio files; and more.

Furthermore, you'll learn about JavaFX 8, now integrated into Java 8 and which gives you additional APIs that will make your game application more fun and dynamic as well as give it a smaller foot-print; so, your game application can run on your PC, mobile and embedded devices.

After reading and using this tutorial, you'll come away with a cool Java-based 2D game application template that you can re-use and apply to your own game making ambitions or for fun.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Setting Up a Java 8 Game Development Environment
    1. Prepare a Workstation for Java 8 Game Development
    2. Downloading Java JDK 8 and NetBeans 8.0
    3. Installing the Java 8 Software Development Environment
    4. Installing NetBeans IDE 8.0
    5. Installing New Media Content Production Software
      1. Downloading and Installing Inkscape
      2. Downloading and Installing GIMP
      3. Downloading and Installing Audacity
      4. Downloading and Installing EditShare Lightworks
      5. Downloading and Installing Blender
    6. Other Open-Source Software Packages of Interest
    7. Organizing Quick Launch Icons in Your Taskbar Area
    8. Summary
  12. Chapter 2: Setting Up Your Java 8 IDE: An Introduction to NetBeans 8.0
    1. Primary Attributes of NetBeans 8.0: An Intelligent IDE
      1. NetBeans 8.0 Is Smart: Put Your Code Editing into Hyperdrive
      2. NetBeans 8.0 Is Extensible: Code Editing with Many Languages
      3. NetBeans 8.0 Is Efficient: Organized Project Management Tools
      4. NetBeans 8.0 Is User Interface Design Friendly: UI Design Tools
      5. NetBeans 8.0 Is not Bug Friendly: Squash Bugs with a Debugger
      6. NetBeans 8.0 Is a Speed Freak: Optimize the Code with a Profiler
    2. Creating Your Java 8 Game Project: The InvinciBagel
    3. Compiling Your Java 8 Game Project in NetBeans 8.0
    4. Running Your Java 8 Game Project in NetBeans 8.0
    5. Profiling Your Java 8 Game Project in NetBeans 8.0
      1. Profiling Your Java 8 Game Application CPU Usage
      2. Profiling Your Java 8 Game Application Memory Usage
    6. Summary
  13. Chapter 3: A Java 8 Primer: An Introduction to Java 8 Concepts and Principles
    1. The Syntax of Java: Comments and Code Delimiters
    2. Java APIs: Using Packages to Organize by Function
    3. Java Classes: Logical Java Constructs to Build On
      1. Nested Classes: Java Classes Living Inside Other Classes
      2. Inner Classes: Different Types of Nonstatic Nested Classes
    4. Java Methods: Core Java Function Code Constructs
      1. Creating a Java Object: Invoking a Class’s Constructor Method
      2. Creating a Constructor Method: Coding an Object’s Structure
    5. Java Variables and Constants: Values in Data Fields
      1. Fixing Data Values in Memory: Defining a Data Constant in Java
    6. Java Modifier Keywords: Access Control and More
      1. Access Control Modifiers: Public, Protected, Private, Package Private
      2. Nonaccess Control Modifiers: final, static, abstract, volatile, synchronized
    7. Java Data Types: Defining Data Type in Applications
      1. Primitive Data Types: Characters, Numbers, and Boolean (Flags)
      2. Reference Data Types: Objects and Arrays
    8. Java Operators: Manipulating Data in the Application
      1. Java Arithmetic Operators
      2. Java Relational Operators
      3. Java Logical Operators:
      4. Java Assignment Operators
      5. Java Conditional Operators
    9. Java Conditional Control: Decision Making or Loops
      1. Decision-Making Control Structures: Switch-Case and If-Else
      2. Looping Control Structures: While, Do-While, and For
    10. Java Objects: Virtual Reality, Using Java Constructs
      1. Creating an InvinciBagel Object: Attributes, States, and Behavior
      2. Creating an InvinciBagel Blueprint: Create the GamePiece Class
      3. Creating a GamePiece() Constructor: Overloading a GamePiece
    11. Summary
  14. Chapter 4: An Introduction to JavaFX 8: Exploring the Capabilities of the Java 8 Multimedia Engine
    1. Overview of JavaFX: From Scene Graph Down to OS
    2. JavaFX Scene Package: 16 Core Java 8 Classes
      1. JavaFX Scene Class: Scene Size and Color and Scene Graph Nodes
      2. JavaFX Scene Graph: Organizing Scenes, Using Parent Nodes
      3. JavaFX Scene Content: Lights, Camera, Cursor, Action!
      4. JavaFX Scene Utilities: Scene Snapshots and Antialiasing
    3. Scene Subpackages: The 13 Other Scene Packages
    4. Other JavaFX Packages: The 15 Top-Level Packages
      1. JavaFX Animation for Games: Using javafx.animation Classes
      2. JavaFX Screen and Window Control: Using javafx.stage Classes
      3. JavaFX Bounds and Dimensions: Using javafx.geometry Classes
      4. JavaFX Input Control for Games: Using javafx.event Classes
      5. JavaFX Thread Control for Games: javafx.concurrent Package
    5. Summary
  15. Chapter 5: An Introduction to Game Design: Concepts, Multimedia, and Using Scene Builder
    1. High-Level Concept: Static vs. Dynamic
    2. Game Optimization: Balancing Static Elements with Dynamic
    3. Game Design Concepts: Sprites, Physics, Collision
    4. Types of Games: Puzzles, Board Games, Arcade Games, Hybrids
    5. Game Design Assets: New Media Content Concepts
      1. Digital Imaging Concepts: Resolution, Color Depth, Alpha, Layers
      2. Digital Video and Animation: Frames, Rate, Looping, Direction
      3. Digital Audio Concepts: Amplitude, Frequency, Samples
    6. JavaFX Scene Builder: Using FXML for UI Design
      1. FXML Definition: Anatomy of an XML UI Definition Construct
      2. Hello World UI FXML Definition: Replicating Your Current UI Design, Using FXML
    7. Summary
  16. Chapter 6: The Foundation of Game Design: The JavaFX Scene Graph and the InvinciBagel Game Infrastructure
    1. Game Design Foundation: Primary Function Screens
    2. Java Class Structure Design: Game Engine Support
    3. JavaFX Scene Graph Design: Minimizing UI Nodes
      1. Scene Graph Code: Optimizing Your Current InvinciBagel Class
      2. Scene Graph Design: Streamlining the Existing .start() Method
    4. JavaFX UI Classes: HBox, Pos, Insets, and ImageView
      1. The JavaFX Pos Class: Generalized Screen Position Constants
      2. The JavaFX Insets Class: Providing Padding Values for Your UI
      3. The JavaFX HBox Class: Using a Layout Container in a Design
      4. The JavaFX Image Class: Referencing Digital Images in a Design
      5. JavaFX ImageView Class: Displaying Digital Images in a Design
      6. The JavaFX TableView Class: Displaying Data Tables in a Design
    5. Scene Graph Nodes: .createSplashScreenNodes( )
      1. Adding Nodes to the Scene Graph: .addStackPaneNodes()
    6. Testing the InvinciBagel Application: Pulse the Scene Graph
    7. Finishing an InvinciBagel UI Screen Design: Add Images
    8. Interactivity: Wiring the InvinciBagel Buttons for Use
      1. Testing the Final InvinciBagel UI Design
      2. Profiling the InvinciBagel Scene Graph for Pulse Efficiency
    9. Summary
  17. Chapter 7: The Foundation of Game Play Loop: The JavaFX Pulse System and the Game Processing Architecture
    1. Game Loop Processing: Harnessing a JavaFX Pulse
    2. Creating a New Java Class: GamePlayLoop.java
    3. Creating the GamePlayLoop Class Structure: Implementing Your .handle( ) Method
    4. Creating a GamePlayLoop Object: Adding Pulse Control
    5. Profiling the GamePlayLoop Object: Running NetBeans Profiler
    6. Controlling Your GamePlayLoop: .start( ) and .stop( )
    7. InvinciBagel Diagram: Package, Classes, and Objects
    8. Testing the GamePlayLoop: Animating the UI Container
    9. Profiling the GamePlayLoop: Pulse Engine
    10. Summary
  18. Chapter 8: Creating Your Actor Engine: Design the Characters for Your Game and Define Their Capabilities
    1. Game Actor Design: Defining the Attributes Up Front
    2. The InvinciBagel Sprite Images: Visual Action States
    3. Creating an Actor Superclass: Fixed Actor Attributes
      1. Creating an .update() Method: Connect to GamePlayLoop Engine
      2. Adding Sprite Control and Definition Variables to an Actor Class
      3. Accessing Actor Variables: Creating Getter and Setter Methods
    4. Creating a Hero Superclass: Motion Actor Attributes
      1. Adding Update and Collision Methods: .update() and .collide()
      2. Adding Sprite Control and Definition Variables to the Hero Class
      3. Accessing Hero Variables: Creating Getter and Setter Methods
    5. Updating the Game Design: How Actor or Hero Fit In
    6. Summary
  19. Chapter 9: Controlling Your Action Figure: Implementing Java Event Handlers and Using Lambda Expressions
    1. Game Surface Design: Adding Resolution Flexibility
    2. Finishing the UI Design: Coding a Game Play Button
      1. Testing the Game Play Button: Making Sure Your Code Works
      2. Upgrading the Other UI Button Code: Making ImageView Visible
    3. Lambda Expression: A Powerful New Java 8 Feature
      1. Handling NetBeans Unexpected Updates and Incorrect Warnings
    4. Event Handling: Adding Interactivity to Your Games
      1. Types of Controllers: What Types of Events Should We Handle?
    5. Java 8 and JavaFX Events: javafx.event and java.util
      1. JavaFX Input Event Classes: The javafx.scene.input Package
    6. Adding Keyboard Event Handling: Using KeyEvents
      1. Processing Your KeyEvent: Using the Switch-Case Statement
      2. Creating the KeyPressed KeyEvent Handling Structure
    7. Adding Alternate KeyEvent Mapping: Using A-S-D-W
    8. Updating Our Game Design: Adding Event Handling
    9. Summary
  20. Chapter 10: Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class
    1. Game Design: Adding Our CastingDirector.java class
    2. List and ArrayList: Using java.util List Management
      1. The Java Interface: Defining Rules for Implementing Your Class
      2. The List<E> Public Interface: A List Collection of Java Objects
    3. Set and HashSet: Using java.util Unordered Sets
      1. The java.util HashSet Class: Using Unordered Sets of Objects
    4. Creating Your Casting Engine: CastingDirector.java
      1. Creating an ArrayList Object: CURRENT_CAST Data Store List
      2. Another ArrayList Object: COLLIDE_CHECKLIST Data Store List
      3. Creating a HashSet Object: REMOVED_ACTORS Data Store Set<Actor>
      4. CastingDirector() Constructor: Having NetBeans Write the Code
    5. Creating Our Main Actor: The Bagel Hero Subclass
    6. Summary
  21. Chapter 11: Moving Your Action Figure in 2D: Controlling the X and Y Display Screen Coordinates
    1. InvinciBagel.java Redesign: Adding Logical Methods
      1. The Scene Event Handling Method: .createSceneEventHandling()
      2. Adding InvinciBagel: Declare Image, Bagel, and CastingDirector
      3. The Actor Image Assets Loading Method: .loadImageAssets()
      4. Creating Your InvinciBagel Bagel Object: .createGameActors()
      5. Adding Your iBagel to the Scene Graph: .addGameActorNodes()
      6. Creating and Managing Your Cast: .createCastingDirection()
      7. Create and Start Your GamePlayLoop: .createStartGameLoop
      8. Update Splashscreen Scene Graph: .createSplashScreenNodes()
    2. Powering the iBagel Actor: Using the GamePlayLoop
      1. Moving the iBagel Actor Object: Coding Your .update() Method
    3. Testing Our New Game Design: Moving InvinciBagel
    4. Summary
  22. Chapter 12: Setting Boundaries for Your Action Figure in 2D: Using the Node Class LocalToParent Attribute
    1. InvinciBagel Privatization: Removing Static Modifiers
      1. Passing Context from InvinciBagel to Bagel: Using this Keyword
      2. Removing a Static iBagel Reference: Revise the Handle( ) Method
      3. Using this in GamePlayLoop() Constructor: GamePlayLoop(this)
      4. Removing the Rest of the Static Variables: StackPane and HBox
    2. Organizing the .update( ) Method: .moveInvinciBagel( )
      1. Further Modularization of the .update() Method: .setXYLocation()
    3. Setting Screen Boundaries: .setBoundaries( ) Method
      1. Testing the InvinciBagel Sprite Boundaries: Run Project
    4. Summary
  23. Chapter 13: Animating Your Action Figure States: Setting the Image States Based on KeyEvent Processing
    1. InvinciBagel Animation: The .setImageState( ) Method
      1. The InvinciBagel Wait State: If No Key Pressed Set imageState(0)
      2. The InvinciBagel Run State: If KeyPressed Set imageState(1 & 2)
      3. The InvinciBagel Fly State: If KeyPressed Set imageState(3 & 4)
      4. Mirroring Sprites: Quadrupling Your Image Assets from 9 to 36
      5. Animating Your Run Cycle: Creating a Nested If-Else Structure
      6. Coding Your Run Cycle Throttle: Triple Nested If-Else Structures
    2. Adding Event Handling: Giving ASDW Keys Function
      1. Creating ASDW Key Get and Set Methods: NetBeans Insert Code
      2. Adding Jump and Evade Animation: Using the W and S Keys
    3. Last Minute Details: Setting the isFlipH Property
      1. Testing the InvinciBagel Sprite Animation States: Run Project
    4. Summary
  24. Chapter 14: Setting Up the Game Environment: Creating Fixed Sprite Classes Using the Actor Superclass
    1. Creating the Prop.java Class: Extending Actor.java
      1. Mirrored Prop Classes: Set the isFlip Property in the Constructor
    2. Using the Prop Class: Creating Fixed Scene Objects
      1. Adding Prop and Image Declarations: Prop and Image Objects
      2. Instantiate Image Objects: Using the .loadImageAssets( ) Method
      3. Adding Fixed Sprites Using Prop Objects: .addGameActors( )
    3. Using Larger Scene Props: Compositing with JavaFX
    4. Summary
  25. Chapter 15: Implementing Game Audio Assets: Using the JavaFX AudioClip Class Audio Sequencing Engine
    1. JavaFX AudioClip Class: A Digital Audio Sequencer
    2. Creating and Optimizing Digital Audio: Audacity 2.0.6
      1. Optimization Versus Compression: The Audio Memory Footprint
    3. Adding Audio to InvinciBagel.java: Using AudioClip
      1. Referencing AudioClip Assets: Using the java.net.URL Class
      2. Adding Your Audio Asset Loading Method: .loadAudioAssets( )
      3. Providing Access to Your AudioClip: The .playiSound( ) Methods
    4. Summary
  26. Chapter 16: Collision Detection: Creating SVG Polygons for the Game Actors and Writing Code to Detect Collision
    1. The SVG Data Format: Hand Coding Vector Shapes
    2. Creating and Optimizing Collision Data: Using GIMP
      1. Creating an Optimized Collision Polygon: Using the Path Tool
      2. Refining SVG Path Collision Shapes in GIMP: Using Import Path
    3. Creating and Optimizing Physics Data: Using PhysEd
    4. Replacing Dummy Collision Data: InvinciBagel.java
    5. Bagel Class Collision Detection: .checkCollision( )
    6. Locating a Node Object: Using the Bounds Object
      1. Using Node Local Bounds: The .getBoundsInLocal() Method
      2. Using Node Parent Bounds: The .getBoundsInParent() Method
      3. Using Node Intersection: The .intersects(Bounds object) Method
    7. Using Shape Class Intersect: The .intersect( ) Method
    8. Overriding the Abstract Hero Class: .collide( ) Method
      1. If Collision Detected: Manipulating the CastingDirector Object
      2. Removing Actors from the Scene Graph: .getChildren().remove()
      3. Reset the Removed Actor List: .resetRemovedActors() Method
      4. Optimizing Collision Detection Processing: if(collide(object))
    9. Optimizing the Scene Graph: Using the Group Class
    10. Creating a Scoring Engine Method: .scoringEngine( )
    11. Summary
  27. Chapter 17: Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine
    1. Creating the Score UI Design: Text and Font Objects
      1. Creating a SCORE Label: Adding the Second Text Object
    2. Creating the Scoring Engine Logic: .scoringEngine( )
      1. Optimizing the scoringEngine() Method: Using Logical If Else If
    3. Adding Bounty to the Game: The Treasure.java Class
      1. Using the Treasure Class: Create Treasure Objects in the Game
      2. Adding Treasure Collision Detection: Updating .scoringEngine()
    4. Adding Enemies: The Enemy and Projectile Classes
      1. Creating Cream Cheese Bullets: Coding a Projectile.java Class
      2. Adding an Enemy and Projectiles to the Game: InvinciBagel.java
    5. Adding a Background Image: Using .toBack( ) Method
    6. Using Random Number Generators: java.util.Random
    7. Mounting the Attack: Coding the Enemy Onslaught
      1. The Foundation of an Enemy Class Attack: The .update() Method
      2. Attacking on Both Sides of the Screen: .initiateAttack() Method
      3. Adding the Element of Surprise: Animating Your Enemy Attack
    8. Weaponizing the Enemy: Shooting Projectile Objects
      1. Creating a Projectile Infrastructure: Adding Projectile Variables
      2. Invoking a .shootProjectile() Method: Setting shootBullet to True
      3. Shooting Projectiles: Coding the .shootProjectile( ) Method
      4. Making the Enemy Pause Before Firing: pauseCounter Variable
      5. Shoot the Bullet: Pulling the Trigger Using the launchIt Variable
    9. Update the .scoringEngine( ) Method: Using .equals( )
    10. Adding Bullets to a Clip: Updating .addCurrentCast( )
    11. Shooting Cream Cheese Balls: Different Bullet Types
    12. Tweaking a Game: Fine-Tuning the User Experience
      1. Randomizing an Auto-Attack: Using .nextBoolean with takeSides
      2. Add the Element of Surprise: Randomizing the Attack Frequency
      3. Targeting the InvinciBagel: Adding Enemy Artificial Intelligence
    13. Adding Gravity to the Bullets: Intro to Game Physics
    14. Summary
  28. Index