Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using Graphics2D for all sprites.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 isometrixk   Members   -  Reputation: 176

Like
0Likes
Like

Posted 28 January 2011 - 11:23 AM

I'm tired of searching, and I am not sure of the correct pronounciation or terminology. <BR><BR>I am manually drawing all sprites directly using Graphics2D in Java for a simple web applet. By hardcoding the lines, fillers, pixels, etc. myself.<BR><BR>Drawing the graphics unto a component is no problem. But saving them for re-use is what I am confused about.<BR><BR>How do I go about saving the area I drew upon unto an object of some sort? I do not even know the class to use.<BR><BR>

Sponsor:

#2 isometrixk   Members   -  Reputation: 176

Like
0Likes
Like

Posted 28 January 2011 - 11:38 AM

Ah! Wait a minute. Do I do something like.....
Graphics2D SpriteObject1;
Graphics2D SpriteObject2;

SpriteObject1.drawEverything;
SpriteObject2.drawEverything;

??
I'm still newbish at working with Graphics in Java

#3 Angex   Members   -  Reputation: 884

Like
1Likes
Like

Posted 28 January 2011 - 02:56 PM

That's the right idea, you just pre-render the sprite to an off screen buffer.

When you want to display the sprite, you simply copy it to the back buffer.

This is a basic example of how to do it:

// Setup sprites.
final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

final GraphicsDevice device = env.getDefaultScreenDevice();

final GraphicsConfiguration gc = device.getDefaultConfiguration();

final BufferedImage spriteBI = gc.createCompatibleImage(
	100,				// Width
	100,				// Height
	Transparency.TRANSLUCENT	// Transparency mode
);

final Graphics2D g2dSprite = (Graphics2D) spriteBI.getGraphics();

g2dSprite.fillRect(0, 0, 100, 100);

// ...



Then for you main render method.



public void render(final Graphics2D g2d) {

	g2d.drawImage(	spriteBI,	// Buffer
			0,		// x coordinate
			0,		// y coordinate
			null		// Not used. We know image is loaded.
	);

}



#4 isometrixk   Members   -  Reputation: 176

Like
0Likes
Like

Posted 28 January 2011 - 03:09 PM

Great response, thank you!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS