简体   繁体   中英

Isolating sprite sheet subimages for animation and display

I have been looking around online to try to find a complete solution but as of now, I can only find pieces that don't fit together.

I'm looking for a program that can look at an image file, loop through the file and isolate the sprites and then save them. After that I need a way to re-access that sprite info so I can display it, but I would like to be able to display the tiles based on my own formulas. Think of a game like advanced wars with little terrain tiles. I want to be able to display those tiles semi-randomly, but within my own parameters.

I would also like to be able to load up a different image file that has the same size sprites as the above image but use these images for animations.

So I have 2 sprite sheets that have a bunch of 64x64 pixel sprites. One of the image files is all my terrain tiles. The other is my unit tiles. I want to be able to read in the sprites and display them any way I please.

I cant for the life of me figure out which way to do this. I've looked into Subimaging and drawImage, but I can't get them to store or redisplay information properly.

Thanks.

EDIT: So ive simplified my question for my own sake and for the sake of everyone else.

Why doesnt the following code work?

package animation;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnimTest
{
public static void main(String[] args)
{
    AnimTest test = new AnimTest();
    test.go();
}

public void go()
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyDrawP drawP = new MyDrawP();
    frame.getContentPane().add(drawP);
    frame.setSize(500,500);
    frame.setVisible(true);
}
    }

   class MyDrawP extends JPanel
    {

public void drawSprite(Graphics g)
{
    try {
        BufferedImage bigImg = ImageIO.read(new File("C:/Users/scott/Desktop/Personal Work/Pixel Art/terrain.png"));

        final int width = 64;
        final int height = 64;

        int x = 0;
        int y = 0;

        bigImg.getSubimage(x, y, width, height);
        g.drawImage(bigImg, 5, 5, this);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
    }

Sorry for the mess, i have NO idea how to format a code block properly.

Now with that code, i want the MyDrawP class to look at my file, grab a 64x64 piece of it starting at 0,0 and then save it and display it in the frame when its added to it. Im pretty sure the problem is that the method drawSprite is never called, but im not sure when to call it and im not sure if anything else is missing.

Again, why doesnt the above code work?

Thanks

You should override the paintComponent(Graphics) method in your MyDrawP class and call your drawSprite from there.

Then, the way you "extract" sub-images and draw them on your JPanel should be reworked:

bigImg.getSubimage(x, y, width, height)

will return a subimage (actually always the one at 0,0 in the code you provide), so you should have something like:

Image subImage = bigImg.getSubimage(x,y,width,height)

Of course, the next line should use subImage instead of bigImg...

And you finally have to implement the way you "randomly" choose which subimage you want to draw, and where you want to draw it on your JPanel.

See http://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html

When I was tutoring a student we used sprite sheets to create animations for games. There are some sprite cutting programs that can help, but none did what I wanted so I wrote my own. In Java you can do some low-level pixel processing to figure out where sprites start and end, then 'cut' them out.

Basically turn your sprite sheet into a BufferedImage and use getPixel to find out where sprites start and end. Then you can write each sprite to a separate image using getSubimage . However, the hard part is aligning the sprites so your animation doesn't jump all over the place. I created a GUI so you can see the animation and do the alignment by hand. If you give me your email I can send the source or I can upload it somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM