0
\$\begingroup\$

I'm working on a puzzle game, I've implemented a sprite class that uses an array of images and cycle through the array to draw each frame as required. This works fine!

Here's the original code:

    String number;
    Image sprite;
    ArrayList <ImageIcon> spriteAnimation = new ArrayList();
    int _imageCount = 0;
    int _animationStart = 0;

   public Sprite(String spriteType, int totalFrames){

        for(int x =1; x <= totalFrames; x++){
          spriteAnimation.add(new ImageIcon("Images/" + spriteType + "/" + spriteType + x + ".png"));
          System.out.println("Added " + spriteType + x + ".png to the ArrayList");   

        }

    }

    public void updateSprite(){

        sprite = spriteAnimation.get(_imageCount).getImage();

        System.out.println("image count is " + _imageCount);

        if(_imageCount < spriteAnimation.size()-1){

           _imageCount += 1;

       }else{

           _imageCount = _animationStart;

       }

    }      

    public Image getSprite(){

      return sprite;

      }
}

However, after researching the difference between using the method above and using a single sprite sheet (there must be a good reason to use a sprite sheet I thought, and I was right) I've decided to use a single sprite sheet to represent all sprites in my game.

While I fully understand the principle of using the sprite sheet, I cannot get my head around actually drawing the selected part of the sprite sheet. I thought there might be a constructor in the ImageIcon or Image classes that would allow me to do something like pass in the size of the image to be drawn, the x and y co-ordinates to start at and the original sprite sheet as arguments - but apparently not. I'm obviously looking in the wrong place.

I would be very grateful if someone could advise me on how to do this in Java.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Have a look at these questions: How to read sprites from a spritesheet in Java? and What's the best way of reading a sprite sheet in Java? \$\endgroup\$
    – Jannie
    Jan 26, 2012 at 9:44
  • \$\begingroup\$ Thanks, That was really helpful. do you want to put that as an answer or should I explain what I did myself (pretty much the same thing). \$\endgroup\$
    – mal
    Jan 26, 2012 at 14:16
  • \$\begingroup\$ Actually I'm pretty sure this question needs to be closed, since those other questions already address the same issue (and maybe those questions should be migrated here), but I don't have nearly enough authority to make that happen... But I'm glad to have been of help! \$\endgroup\$
    – Jannie
    Jan 26, 2012 at 16:28
  • \$\begingroup\$ Maybe so, mods here are pretty on the ball, I'm sure they'll sort it soon enough. \$\endgroup\$
    – mal
    Jan 26, 2012 at 19:08

1 Answer 1

0
\$\begingroup\$

OK, as per Jannie Pieters suggested links, I have resolved the issue. I should have been looking at BufferedImage instead of Image and ImageIcon. Here is my revised class:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;



/**
 *
 * @author mal
 */
public class Sprite2 {


    int _imageCount = 0;
    int _animationStart = 0;
    int _spriteSize;
    int _frameCount;

    BufferedImage[] sprites;


   public Sprite2(String sheetName,int frameCount, int columns, int rows, int frameSize){

       _frameCount = frameCount;

      try{

       BufferedImage spriteSheet = ImageIO.read(new File("Images/" + sheetName + ".png"));
       buildSprites(spriteSheet, columns, rows, frameSize);

      }catch(IOException e){

           System.out.println("Image not found: Sprite2.java line 50");

       }



    }

    public void triggerUpdate(){



        System.out.println("image count is " + _imageCount);

        if(_imageCount < _frameCount-1){

           _imageCount += 1;

       }else{

           _imageCount = 0;

       }

    }

    private void buildSprites(BufferedImage spriteSheet, int columns, int rows, int frameSize){

        sprites = new BufferedImage[25];

        for(int x = 0; x < columns; x++){

            for(int y = 0; y < rows; y++){

                sprites[(x * 5) + y] = spriteSheet.getSubimage(
                x * (frameSize + 1),  // my sprite software adds 1 pixel border to each frame
                y * (frameSize + 1),
                frameSize,
                frameSize
                );
            }

        }


    }

 public BufferedImage getSprite(int imageNumber){

    return sprites[imageNumber];

 }

  public BufferedImage getSprite(){

    return sprites[_imageCount];

 }
}

This works perfectly, now all I need to do is re-design the rest of the game, port it to android, market it well and make millions!

sorted!

Lots of new concepts for me to study here.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .