Interested in learning how to program with Java? Let’s face it, the best way to learn to program is by writing programs. This can be a daunting proposition with the specter of hours of simple command line example programs hanging over your head. Fear not! Now you can learn to program in Java in a fun way by working on video games. With this book, you’ll get to work with three Java game projects and have access to the complete game code for each project, including a full Java game engine. After completing Introduction to Java through Game Developmen t, you’ll be proficient in Java programming, having worked with the language’s fundamental aspects throughout the text, and will be ready to further your Java and game programming expertise with confidence. What You'll Master the fundamentals of the Java programming language Use different data structures like arrays, lists, stacks, and queues Understand game programming basics including the main game loop Gain experience working with three different game projects via the book’s coding challenges Work with the 2D game engine that powers the book's included games and learn to create your own new game projects Understand advanced Java topics like classes, encapsulation, inheritance, and polymorphism Work with exceptions and how to use debugging techniques to trace through code Sharpen your skills with over a dozen coding challenges that test your abilities with a development task on a real game project Who This Book Is For This book requires little to no programming experience to understand and benefit from the text. Table of Contents 5 About the Author 11 Introduction 12 Chapter 1: Introduction 13 About This Text 13 Notes on Formatting 14 Notes on Conventions 16 Objectives 18 Java Fundamental Topics 18 Java Advanced Topics 19 Game Development Topics 20 Setting Up Your Environment 21 Getting the Game Projects Setup 25 Checking Out the Games 32 Conclusion 38 What We Covered 39 Chapter 2: What Is Java Programming 40 Computers and Programming 40 Programming Computers 41 Programming Languages 41 Types of Programs/Programming 43 The Java Programming Language 44 The JRE 45 The JDK 46 Syntax and Semantics 46 Basic Syntax Rules 47 Keywords/Reserved Words 48 Game Programming 49 The Main Game Loop 50 Program Structure 51 Overview of Included Games 51 Conclusion 52 What We Covered 53 Chapter 3: Variables 54 Data Types 54 Basic Data Types 55 Using Basic Data Types 57 Challenge: Basic Data Types 60 Challenge Solution 62 Advanced Data Types 63 The var Keyword and Dynamic Typing 63 Arrays 65 Using Arrays 67 Challenge: Arrays 74 Challenge Solution 76 Lists 77 Using Lists 80 Challenge: ArrayLists 83 Challenge Solution 84 Conclusion 84 What We Covered 85 Chapter 4: Expressions and Operators, Flow Control, and More on Variables 86 Expressions and Operators 87 Numeric Expressions 91 Boolean Expressions 93 Assignment Expressions 94 Increment/Decrement Expressions 95 Bitwise Expressions 96 Bit-Shift Expressions 97 Operators and Operator Precedence 98 Flow Control 99 If-Else Statements 100 Switch Statements 102 Try-Catch Statements 104 Challenge: Flow Control 106 Challenge Solution 107 More on Variables 109 Enumerations 109 Very Basic Java Classes 111 Casting and Conversion 112 Challenge: Enumerations 117 Challenge: Solution 118 Conclusion 120 What We Covered 120 Chapter 5: More Data Structures 122 Multidimensional Arrays 123 Declaring Multidimensional Arrays 123 Using Multidimensional Arrays 124 Hashes 127 Declaring Hashtables 128 Using Hashtables 130 Stacks 133 Declaring Stacks 134 Using Stacks 135 Queues 139 Parameterized Types and Data Structures 141 Challenge: Stacks 142 Challenge Solution 143 Conclusion 145 What We Covered 145 Chapter 6: Looping and Iteration 146 For Loops 147 Basic For Loop 147 For-Each Loop 151 While Loops 153 Basic While Loop 153 Main Game Loop 155 Do-While Loops 162 Basic Do-While Loop 162 Break and Continue 163 Challenge: For-Each Loops 164 Challenge Solution 164 Conclusion 165 What We Covered 166 Chapter 7: Objects, Classes, and OOP 167 Classes 168 Fields 170 Methods 173 Static Members 179 Constructors 182 Challenge: The MmgBmp Class 184 Challenge Solution 186 Challenge: The ScreenGame Class 187 Challenge Solution 188 Advanced Class Topics 188 Access 188 Class Design 189 Static Main Entry Point 189 Challenge: Dungeon Trap’s Static Main 191 Challenge Solution 193 Conclusion 195 What We Covered 196 Chapter 8: Encapsulation, Inheritance, and Polymorphism 198 Encapsulation 199 Inheritance 200 Challenge: Inheritance 202 Challenge Solution 203 Polymorphism 204 Challenge: Polymorphism 211 Challenge Solution 213 Importing Class Libraries 214 Video Game Project Structure 216 Challenge: Create a New Game Project 218 Challenge Solution 219 Step 1: Create a New Project to Customize 219 Step 2: Add the Default Project Libraries 221 Step 3: Copy Over the Resource Folder 223 Step 4: Copy Over the PongClone Java Classes 225 Step 5: Configure Project Settings 228 Step 6: Execute the Pong Clone Copy 229 Conclusion 230 What We Covered 231 Chapter 9: Debugging Techniques 232 Output Trace 232 IDE Debugging Features 233 Exceptions 237 Handling Exceptions 237 Defining Exceptions 240 The Stack Trace 242 Conclusion 243 What We Covered 243 Chapter 10: Conclusion 245 Accomplishments 245 Acknowledgments 248 Where You Go from Here 250 Saying Goodbye 250 Index 251