This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Most efficient way to use a spritesheet?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 32
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic