Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 10 ■ ANIMATION


it might do harm, by tying up the system such that it can't do other work (either for your process or for
other processes).
In creating samples for this chapter, we chose frame rates between 4 and 25 FPS. That might seem
low, but it's adequate for the sake of examples. A low frame rate might even help you understand how
animation works, as you can then see it happen. If you watch the sprite animation example closely, you
can see the individual images being drawn.
By the way, humans (and cats, dogs, and anything else with non-mechanical eyes) don't see the
world as frames going by every so often. Being analog, the human eye and brain respond to a steady
input of light, motion, and color. The question that usually concerns game makers and other animators
(such as the folks who re-create car accidents for use in court rooms) is at what rate can the viewers feel
like they're seeing something realistic (which, again, is not the same as seeing something real). That
number varies hugely according to a number of factors, such as resolution, light/dark balance of the
content on the screen, the lighting in the viewer's area, the visual acuity of the viewer (which is itself
dictated by a number of variables, such as age, fatigue, experience with animation, and so on).
So what is the right FPS for your application? The only real way to know is to put it in front of several
different potential users and ask them if it looks good. Also, if you do a lot of animation, you'll probably
develop a feel for the right frame rate. Finally, game makers often concern themselves with what the
competition is doing. If their games run at 75 FPS, your game better be able to do that, too, or have a
good reason for not doing so.

Animation: A Simple Example


Let's start with the basics: getting an object to move from one side of the screen to the other. My friends
in the game industry say this is the “Hello, World” stage of game development. To do it, we create two
classes: a field for the object to cross and a frame to be the main program. (We could do it in a single
class, but Swing makes a separate drawing field easier to do.) Listing 10-1 shows the program class
(called ScootBall because a ball scoots across the screen).

Listing 10-1. The ScootBall class

package com.bryantcs.examples.animation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
Free download pdf