Sprites, how to?

Hey guys,

So I’ve been wanting to figure out how to use sprites in Java. I know how the concept works and all that, since I’ve used them in html/css for a while now.

But I’m not sure how to implement it in Java? Also, how can i make them so that the sprites are in, say 8 colors, and those colors can be changed on the fly by the game?

As far as changing the pixels of a sprite in java ‘on the fly’, I know no such way.
But for sprites, I could help you out =D.

You’re going to want to create a ImageIcon first for the sprite, than list the path for it, than draw it using java Graphics.

public ImageIcon tree = new ImageIcon("src/server/cache/tree.png"); //<-- that is your path, now stored in "tree".

Next you’re going to want to create a method to actually draw the tree/picture you’re choosing.

public void paint(Graphics g) {
g.drawImage(tree, 0, 0, null); //<-- Simple, "tree" = image, 0=X, 0=Y, null = your observer.
}

Hope i’m not to late, believe everything above should work, but other members here frown upon the way i do things so wait for others opinions if mine doesn’t cut it for drawing sprites.

You can create a BufferedImage as sprite and redraw it when needed.

You can’t directly draw an ImageIcon, you have to get the Image from it first. However, it is much cleaner to get the image using javax.imageio.ImageIO.

A Sprite should basically be a class that stores the X, Y, width, height, image(s), and other variables that might be important.

There is a video link here.

this guy shows how to use sprites in java.

Hrm, that looks easy enough. I’ll get sprites working first and then I’ll figure out how to change their colors on the fly.

Thanks for all the help guys. :slight_smile: