Java Swing: Graphics to Image?

N

Noddie

Hi.
I'm trying to get some sort of "screenshot" from a´Graphics object to an
Image object. In detail I want to copy a tiny area from Graphics to an
Image, so that can draw this part of the Graphics representation back to
Graphics after something else was drawn there. (I have to implement a
marquee which runs over an background image)

Thanks for any hint, Noddie
 
I

IchBin

Noddie said:
Hi.
I'm trying to get some sort of "screenshot" from a´Graphics object to an
Image object. In detail I want to copy a tiny area from Graphics to an
Image, so that can draw this part of the Graphics representation back to
Graphics after something else was drawn there. (I have to implement a
marquee which runs over an background image)

Thanks for any hint, Noddie
public class ScreenShot
{
public static void main(String[] args)
{
try
{
// Get the screen size
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(0, 0, screenSize.width,
screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);
File file;

// Save the screenshot as a png
file = new File("screen.png");
ImageIO.write(image, "png", file);

} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}


--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
O

Oliver Wong

Noddie said:
Hi.
I'm trying to get some sort of "screenshot" from a´Graphics object to an
Image object. In detail I want to copy a tiny area from Graphics to an
Image, so that can draw this part of the Graphics representation back to
Graphics after something else was drawn there. (I have to implement a
marquee which runs over an background image)

Thanks for any hint, Noddie

The "Graphics" object is like a pen, and a "Image" object is like a
sheet of paper. That is, the drawing is stored in the "Image" object, and
not in the "Graphics" object, just like the drawing is stored on the paper,
and not in the pen.

Thus it does not make sense to take drawing data out of a Graphic object
to put into an Image object, since the Graphic object doesn't contain any
drawing data.

You need to get the surface onto which the Graphic draws upon, and get
the drawing data from that.

- Oliver
 
T

Thomas Weidenfeller

Noddie said:
I'm trying to get some sort of "screenshot" from a´Graphics object

Since Graphics objects as such don't hold any image or drawing data,
except from some state, this is not going to happen. A Graphics object
just provides a drawing context to "something". You need to get that
"something" and check if/how you can get some image data out of it.
Image object. In detail I want to copy a tiny area from Graphics

Graphics doesn't hold image data.

/Thomas
 
Joined
Jun 1, 2007
Messages
1
Reaction score
0
Buffer it

Noddie said:
Hi.
I'm trying to get some sort of "screenshot" from a´Graphics object to an
Image object.

Use double buffering, that is, instead of drawing direct to the screen, draw to an Image first, call theImage.getGraphics() then draw to that, dump the image to the screen/panel/whatever when it is drawn. You can then create a new image from any rectangle of your buffer image.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top