wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Most efficient way to use a spritesheet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Most efficient way to use a spritesheet?" Watch "Most efficient way to use a spritesheet?" New topic
Author

Most efficient way to use a spritesheet?

Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
What would be the most efficient way to take images from a sprite sheet and randomly place them to make a background map?

Right now i have a these 4 methods that do what i want, but theyre super slow when i try to scroll around my image.



Is there a way for my to add all the sprites (the final map image) into a Swing Component and then just display that component rather then draw out the entire thing? If so, which one would be the best at handling it? I really dont need the entire thing to be redrawn, i just need it made once for now.
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

Scott Kimberly wrote:
Is there a way for my to add all the sprites (the final map image) into a Swing Component and then just display that component rather then draw out the entire thing? If so, which one would be the best at handling it? I really dont need the entire thing to be redrawn, i just need it made once for now.


Sounds like what you are looking for is simply a buffer for your background image. You can easily use the BufferedImage class to achieve this. In your spriteRiverSetup method
1. Create a BufferedImage of the appropriate size (same size as your panel will be). Best do this using JComponent#createImage(...);
2. Invoke image.createGraphics to obtain a Graphics2D object.
3. Render all your sprites to the image, pretty much the same way you did in your paintComponent method now
4. Dispose of the Graphics2D instance (graphics.dispose()).
5. In your paintComponent method, simply draw your image to your component (this is very, very fast).

You will, of course, have to rerun the method if your panel's size changes.
Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
Alexander Kober wrote:
Sounds like what you are looking for is simply a buffer for your background image. You can easily use the BufferedImage class to achieve this. In your spriteRiverSetup method
1. Create a BufferedImage of the appropriate size (same size as your panel will be). Best do this using JComponent#createImage(...);
2. Invoke image.createGraphics to obtain a Graphics2D object.
3. Render all your sprites to the image, pretty much the same way you did in your paintComponent method now
4. Dispose of the Graphics2D instance (graphics.dispose()).
5. In your paintComponent method, simply draw your image to your component (this is very, very fast).

You will, of course, have to rerun the method if your panel's size changes.


So how much of that is one method? It sounds like 1-4 is one method and 5 is a second.

Also I want to createImage and use that instead of BufferedImage for reading in my sprites?

Then create a Graphics2d object and render all my sprites to that object.

And when i dispose of my Graphics2D itll still draw it, disposing just dumps it from memory right? Sends it to garbage collection?
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

Scott Kimberly wrote:

So how much of that is one method? It sounds like 1-4 is one method and 5 is a second.

Also I want to createImage and use that instead of BufferedImage for reading in my sprites?

Then create a Graphics2d object and render all my sprites to that object.

And when i dispose of my Graphics2D itll still draw it, disposing just dumps it from memory right? Sends it to garbage collection?


Sorry if that was confusing. The method you'll have to run whenever your panel size changes is of course the one where you create the image buffer and draw your sprites - so yes, points 1 to 4.

The way you're reading your sprite images is fine - JComponent#createImage simply makes sure that the image buffer you create for your sprites is 'compatible'. In simplest terms, this means that the image's color model is one that the graphics card understands and does not have to be converted internally before drawing it to the screen. Since you'll only draw the sprite images onto your image buffer once, their model doesn't really matter.

Disposing a Graphics2D indeed only releases the associated resources - since the Graphics2D instance has some counter parts in native drawing code, it is good practice to do so. Your image will not be affected in any way.

Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
So ive reduced my code to these 2 methods which now do the same thing as the other 4 did.



For all intents and purposes, this is a faster version of the other longer way i was doing it. What im running into now is that when i scroll up and down my image, it gets all streaky and distorts and doesnt remain clean.

This is my GUI set up code, i dont know if im doing something wrong with my scroll bars or not.

Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
I figured out why my scrolling was being dumb.

I had been drawing directly into a panel (Drawing extended Panel) and i switched it to Label, then added the Label into the Panel and it works fine now and doesnt mess up and scrolls fast.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Most efficient way to use a spritesheet?
 
Similar Threads
append multiple jpg images to form multipage jpg Image
Sending an image on network..
Autoscrolling for marquee selection
Adding images in JTable
Moving within a 2 dimensional array