O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required