Jump to content
  • Advertisement
Sign in to follow this  
Scribbl3r

[java] Drawing 2D sprites with BufferStrategy

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using BufferStrategy with two buffers to draw on a awt.Canvas that is inside of a JFrame. So I am trying to get this animated sprite to appear correctly on the screen, but I ran into trouble. Unless the background is cleared somehow after each paint(), the last frame remains on the canvas until a blob remains where my animated sprite was. I figured out how to fix it by drawing a rectangle of white ever time at the beginning of the paint() function, like so: (code tags don't fail me now!)
public void paint(){
        Graphics2D g = (Graphics2D) canvas.getBufferStrategy().getDrawGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,640,480);

        *draw stuff to g*

        g.dispose();
        canvas.getBufferStrategy().show();
    }
However, I imagine once I start using background images, this will become very innefficient. How do I selectively manipulate the background of my canvas?

Share this post


Link to post
Share on other sites
Advertisement
I think the answer to this question depends on whether the background moves along with the sprite or not. For example, if you have a player sprite in the middle of the screen and you walk to the right, does the background scroll? If so, then you have to render the entire background again anyway, so just render the whole thing.

Otherwise it is a valid question and I am eager to hear the replies.

Share this post


Link to post
Share on other sites
As Kev said, it depends on the scrolling of the background. To scroll, you calculate the offset of the player's position, paint() the background image based on it, then your tiles, and then you iterate over your sprites to draw them - it will appear to you that they have moved, but in fact, they never leave the center of the screen, the terrain moves, not the sprites.

Share this post


Link to post
Share on other sites
I see your point about having to update the background anyways if the BG is scrolling, which is problably what most games will do, including mine.

I guess my question is answered then, but I am still curious because I have limited experience with other libraries like Simple Direct Media Layer, and they use "blitting," where you can copy a rectangular area of any given image to a rectangular area of a destination image. Are there any comparable operations possible in Java?

Thanks

Share this post


Link to post
Share on other sites
I don't know the Java 2D API well enough to answer that, but I guess you can do that easily. In OpenGL, you can use glReadPixels(), so using a Java OGL binding might do the trick. In pure Java 2D, there are means to access the byte array corresponding to the pixels, though I don't know the methods to name them for you. :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!