May 2005
Intermediate to advanced
998 pages
26h
English
BugPanel
is a subclass of JPanel that implements the animation framework described in Chapters 2 and 3; BugPanel closely resembles the WormPanel class. The constructor sets up keyboard and mouse listeners, prepares the ImagesLoader and ClipsLoader objects, and creates the bat and ball sprites:
public BugPanel(BugRunner br, long period)
{
bugTop = br;
this.period = period;
setDoubleBuffered(false);
setBackground(Color.black);
setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
setFocusable(true);
requestFocus(); // now has focus, so receives key events
addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e)
{ processKey(e); } // handle key presses
});
// load the background image
ImagesLoader imsLoader = new ImagesLoader(IMS_INFO);
bgImage = imsLoader.getImage("bladerunner");
// initialise the clips loader
clipsLoader = new ClipsLoader(SNDS_FILE);
// create game sprites
bat = new BatSprite(PWIDTH, PHEIGHT, imsLoader,
(int)(period/1000000L) ); // in ms
ball = new BallSprite(PWIDTH, PHEIGHT, imsLoader,
clipsLoader, this, bat);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e)
{ testPress(e.getX()); } // handle mouse presses
});
// set up message font
msgsFont = new Font("SansSerif", Font.BOLD, 24);
metrics = this.getFontMetrics(msgsFont);
} // end of BugPanel()The image loaded by ImagesLoader is stored in the global bgImage and later used as the game's background image (see Figure 11-1).
BladeRunner fans will ...
Read now
Unlock full access