Domanda

I'm trying to make a simple game in Java and I have a sprite sheet. I can now display a single part of the sprite sheet, but now how would I enlarge each sprite? (each sprite is only 16 by 16 pixels)

Here is my code for the Sprite Sheet:

public class SpriteSheet {

    public BufferedImage[] getTilesToArray(String filePath, int tileWidth,
            int tileHeight) {

        BufferedImage tileSheet;

        try {
            tileSheet = ImageIO.read(SpriteSheet.class
                    .getResourceAsStream(filePath));

        } catch (IOException e) {
            System.out.println("Error loading sprite sheet");
            return null;

        }

        int yTiles = tileSheet.getHeight() / tileHeight;

        BufferedImage[] images = new BufferedImage[yTiles];

        for (int index = 0; index < yTiles; index++) {

            BufferedImage img = tileSheet.getSubimage(0, index * tileHeight,
                    tileWidth, tileHeight);

            Graphics g = img.getGraphics();
            g.drawImage(img, 100, 100, 109, 109, null);
            g.dispose();

            images[index] = img;

        }
        return images;
    }
}

And my main class:

 public class Main extends JFrame {

    private SpriteSheet s = new SpriteSheet();

    private BufferedImage[] sprites = s.getTilesToArray("/spritesheettest.png",
            16, 16);

    public Main() {
        super("Game Compnents");
        setSize(500, 600);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        Main m = new Main();
    }

    public void paint(Graphics g) {
        g.drawImage(sprites[0], 101, 101, null);
    }

}

I've also looked at plenty of game programming tutorial on sprite sheets, but they go so fast that I don't understand what's going on. Should I just pick up a book or is there an easy way to make the sprites bigger?

È stato utile?

Soluzione

You can use Image.getScaledInstance().

Of course you will get pixilation the bigger you scale it. It is better to start with a high res image and scale it down.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top