public SpriteAnimation(String imageFile, int numFrames, int ticksPerFrame) {
    try {
      // Load the raw frames into a frame buffer
      GreenfootImage baseImg = new GreenfootImage(imageFile);
      GreenfootImage[] baseFrames = new GreenfootImage[numFrames];
      int frameWidth = baseImg.getWidth() / numFrames;
      int frameHeight = baseImg.getHeight();
      BufferedImage rawImg = baseImg.getAwtImage();
      for (int x = 0; x < numFrames; x++) {
        baseFrames[x] = new GreenfootImage(frameWidth, frameHeight);
        baseFrames[x]
            .getAwtImage()
            .setData(rawImg.getSubimage(x * frameWidth, 0, frameWidth, frameHeight).getData());
      }

      // Re-use object references to save from copying the images multiple times
      frames = new GreenfootImage[numFrames * ticksPerFrame];
      flipped = new GreenfootImage[numFrames * ticksPerFrame];
      for (int f = 0; f < numFrames; f++) {
        for (int t = 0; t < ticksPerFrame; t++) {
          frames[f * ticksPerFrame + t] = baseFrames[f];
          flipped[f * ticksPerFrame + t] = new GreenfootImage(baseFrames[f]);
          flipped[f * ticksPerFrame + t].mirrorHorizontally();
        }
      }
    } catch (IllegalArgumentException e) {
      frames = flipped = new GreenfootImage[1];
      frames[0] = ERROR_IMAGE;
    }
  }
예제 #2
0
  /** Load the images */
  private void loadImages() {
    GifDecoder decode = new GifDecoder();
    decode.read(file);
    int numFrames = decode.getFrameCount();
    if (numFrames > 0) {
      images = new GreenfootImage[numFrames];
      delay = new int[numFrames];
    } else {
      images = new GreenfootImage[1];
      images[0] = new GreenfootImage(1, 1);
    }

    for (int i = 0; i < numFrames; i++) {
      GreenfootImage image =
          new GreenfootImage(decode.getFrame(i).getWidth(), decode.getFrame(i).getHeight());
      BufferedImage frame = image.getAwtImage();
      Graphics2D g = (Graphics2D) frame.getGraphics();
      g.drawImage(decode.getFrame(i), null, 0, 0);
      delay[i] = decode.getDelay(i);
      images[i] = image;
    }
    time = System.currentTimeMillis();
  }