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

JackPanel is a subclass of JPanel and implements the animation framework described in Chapters 2 and 3; JackPanel resembles the BugPanel class of Chapter 11.

The JackPanel() constructor in Example 12-1 creates the game entities: the RibbonsManager, BricksManager, JumperSprite, and FireBallSprite objects. It prepares the explosion animation and the title/help screen.

Example 12-1. The JackPanel constructor

    // some of the globals
    private JumpingJack jackTop;
    private JumperSprite jack;          // the sprites
    private FireBallSprite fireball;
    private RibbonsManager ribsMan;     // the ribbons manager
    private BricksManager bricksMan;    // the bricks manager

    // to display the title/help screen
    private boolean showHelp;
    private BufferedImage helpIm;

    // explosion-related
    private ImagesPlayer explosionPlayer = null;
    private boolean showExplosion = false;
    private int explWidth, explHeight;   // image dimensions
    private int xExpl, yExpl;   // coords where image is drawn


    public JackPanel(JumpingJack jj, long period)
    {
      jackTop = jj;
      this.period = period;

      setDoubleBuffered(false);
      setBackground(Color.white);
      setPreferredSize( new Dimension(PWIDTH, PHEIGHT));

      setFocusable(true);
      requestFocus();    // so receives key events

      addKeyListener( new KeyAdapter() {
        public void keyPressed(KeyEvent e)
        { processKey(e); }
      });

      // initialise the loaders
      ImagesLoader imsLoader = new ImagesLoader(IMS_INFO);
      clipsLoader = new ClipsLoader(SNDS_FILE);

      // initialise the game entities bricksMan = new BricksManager(PWIDTH, ...

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