Home arrow Java & J2EE arrow Page 8 - Gaming Development Setup

Drawing the background and target - Java

One important step in creating your own computer games is configuring your development environment. Keep reading to learn how. This article is taken from chapter one of Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232).

TABLE OF CONTENTS:
  1. Gaming Development Setup
  2. Playing the Demo Online
  3. Introducing XML
  4. Using Open Source
  5. Finding Multimedia for Your Games
  6. Basics Example
  7. Generating an event
  8. Drawing the background and target
By: Apress Publishing
Rating: starstarstarstarstar / 33
March 02, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As described in Chapter 3, the paint() method is called whenever the screen needs to be repainted, usually once per game loop during animation. The calling of the paint() method is managed by the superclass AnimatedApplet and is triggered by the repaint requests made in the update() method.

graphics.setColor ( BACKGROUND_COLOR );

graphics.fill ( componentBounds );

The background is always drawn first.

icon.paintIcon ( component, graphics, targetRectangle.x, 0 );

The target image is painted.

graphics.setColor ( BALL_COLOR );

graphics.fillOval (
  ballRectangle.x,
  ballRectangle.y,
  ballRectangle.width,
  ballRectangle.height );

The ball is drawn.

graphics.setColor ( SCORE_COLOR );

graphics.drawString (
  "Score: " + Integer.toString ( score ),
  0, componentBounds.height );
}

Finally the score text is drawn. Because the score is drawn after the ball in the paint() method, the score cannot be occluded by the ball.

Notice that no game state is updated in the paint() method and no graphics are painted in the update() method. It is a sin to do otherwise.

Modifying the Build

After playing with the game example and reviewing its source code, you might want to go ahead and use it as a template for a game of your own invention. I recommend that you do so using an incremental approach, building and testing after each small change. The first change you might want to make is to change the package name for the class. Even this one-line change to the code can be frustrating if you do not consider the simultaneous changes that need to be made to the Ant build file. The following code documents the required changes.

<target name="basics_prep" depends="init">
  <copy todir="${tmp_dir}/media/basics">
    <fileset dir="${res_dir}/ajgp/basics/media">
      <include name="croftsoft.png"/>
      <include name="drip.wav"/>
    </fileset>
  </copy>
</target>

If you add or remove media files for the game, you must modify target basics_prep.

<target name="basics" depends="basics_prep">
  <javac srcdir="${src_dir}" destdir="${tmp_dir}">
    <include name="com/croftsoft/ajgp/basics/BasicsExample.java"/>
  </javac>
  <echo
    file="manifest.txt" 
    message="Main-Class:
    com.croftsoft.ajgp.basics.BasicsExample" />
  <jar
    basedir="${tmp_dir}"
    destfile="${arc_dir}/basics.jar"
    manifest="manifest.txt"
    update="false"/>
  <delete file="manifest.txt"/>
  <delete dir="${tmp_dir}"/>
  <java fork="true" jar="${arc_dir}/basics.jar"/>
</target>

If you change the package or class name, you must modify target basics.

This wraps up the Java game programming basics example. If any of it is a bit of a mystery, be assured that all will be explained in subsequent chapters. Later chapters will also make the point that while this code serves as a useful example and an initial template, there are a number of improvements that could be made.

Summary

In this chapter, you learned how to compile the example code used throughout the book using the development build tool Ant. You also learned about other development tools such as image and audio editors. I identified sources for code and multimedia you can incorporate into your games, and explained Open Source licensing terms. I documented the source code for an example game demonstrating the Java game programming basics for reuse as a template. In the following chapters, I describe the classes available to you within the reusable game library in detail.

Further Reading

Eckstein, Robert. XML Pocket Reference, 2nd edition. Sebastopol, CA: O’Reilly & Associates, 2001. Feldman, Ari. Designing Arcade Computer Game Graphics. Plano, TX: Wordware Publishing, 2000. Fishman, Stephen. The Public Domain: How to Find & Use Copyright-Free Writings, Music, Art & More. Berkeley, CA: Nolo Press, 2001. Fishman, Stephen. Web & Software Development: A Legal Guide, 3rd edition. Berkeley, CA: Nolo Press, 2002.


(1). http://www.opensource.org/
(2). http://www.godaddy.com/
(3). http://www.sourceforge.net/
(4). http://games.dev.java.net/
(5). http://www.GameJUG.org/
(6). http://www.arifeldman.com/
(7). http://www.dimensionality.com/free.html
(8). http://www.gimp.org/
(9). http://www.squirreldome.com/cyberop.htm, http://www.ab.wakwak.com/~knight/
(10). http://www.cosmi.com/
(11). http://www.goldwave.com/

This article is excerpted from Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232). Check it out at your favorite bookstore today. Buy this book now.



 
 
>>> More Java & J2EE Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: