コード例 #1
0
  public void run() {
    // Create and add sprites to the
    // sprite manager
    spriteManager = new SpriteManager(new BackgroundImage(this, backGroundImage));
    // Create 15 sprites from 6 gif
    // files.
    for (int cnt = 0; cnt < 15; cnt++) {
      Point position =
          spriteManager.getEmptyPosition(
              new Dimension(gifImages[0].getWidth(this), gifImages[0].getHeight(this)));
      spriteManager.addSprite(makeSprite(position, cnt % 6));
    } // end for loop

    // Loop, sleep, and update sprite
    // positions once each 83
    // milliseconds
    long time = System.currentTimeMillis();
    while (true) { // infinite loop
      spriteManager.update();
      repaint();
      try {
        time += animationDelay;
        Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
      } catch (InterruptedException e) {
        System.out.println(e);
      } // end catch
    } // end while loop
  } // end run method
コード例 #2
0
  // Overridden graphics update method
  // on the Frame
  public void update(Graphics g) {
    // Create the offscreen graphics
    // context
    if (offScreenGraphicsCtx == null) {
      offScreenImage = createImage(getSize().width, getSize().height);
      offScreenGraphicsCtx = offScreenImage.getGraphics();
    } // end if

    // Draw the sprites offscreen
    spriteManager.drawScene(offScreenGraphicsCtx);
    // Draw the scene onto the screen
    if (offScreenImage != null) {
      g.drawImage(offScreenImage, 0, 0, this);
    } // end if
  } // end overridden update method