jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Getting pixels from a  Graphics instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting pixels from a  Graphics instance" Watch "Getting pixels from a  Graphics instance" New topic

Getting pixels from a Graphics instance

Neville Campbell

Joined: Mar 31, 2001
Posts: 6
Is there any way to read pixel data from a Graphics instance?
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

No... you use Graphics to draw to components... you can't query the Graphics object to see what was drawn... though you can create an Image (like BufferedImage), draw to that, and ask the image what is drawn on it...

What exactly are you trying to do?

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Neville Campbell

Joined: Mar 31, 2001
Posts: 6
My question is motivated by animation in full-screen exclusive mode, typically with page flipping. If an animation involves relatively few scene changes between frames, I might render the pages incrementally. Each page would receive only the necessary updates before being displayed.
Now consider the implementation of a sprite under these circumstances. Just before a sprite is rendered, it would be useful to have the sprite save the portion of the scene it is about to cover. The sprite would store a sprite-sized overwritten image for each page. The appropriate image would be restored to the volatile buffer immediately after each invocation of bufferStrategy.getDrawGraphics().
Unfortunately I know of no way to retrieve the portion of the image that is about to be overwritten by the sprite. I don't know how to access the underlying VolatileImage.
Any suggestions? Or am I out of luck?
I agree. Here's the link: http://aspose.com/file-tools
subject: Getting pixels from a Graphics instance
It's not a secret anymore!