The Animation Framework

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).

Warning

BladeRunner fans will ...

Get Killer Game Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.