Gaming Development Setup - Drawing the background and target (Page 8 of 8 )
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. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |