Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Swing: Graphics to Image?

Reply
Thread Tools

Java Swing: Graphics to Image?

 
 
Noddie
Guest
Posts: n/a
 
      03-01-2006
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
 
Reply With Quote
 
 
 
 
IchBin
Guest
Posts: n/a
 
      03-01-2006
Noddie wrote:
> 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-)
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-01-2006

"Noddie" <(E-Mail Removed)> wrote in message
news:4405e001$0$12642$(E-Mail Removed) t...
> 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

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      03-02-2006
Noddie wrote:
> 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
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
johndude johndude is offline
Junior Member
Join Date: Jun 2007
Posts: 1
 
      06-01-2007
Quote:
Originally Posted by Noddie
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Intel HD Graphics 3000 and Intel HD Graphics 2000 Review Ian Front Page News 0 02-24-2011 03:07 PM
Best graphics program to lecture students with graphics tablet Mrs. Chickpea Digital Photography 8 05-26-2008 04:54 AM
Upgrading graphics on a computer with Integrated Graphics sharonf Computer Support 3 03-27-2007 06:21 AM
Gizmodo: NVIDIA Ships 128-Core Graphics Cards for High-End Film Editors Graphics Pros Silverstrand Front Page News 0 03-05-2007 03:32 PM
Intel Graphics Media Accelerator 900 Graphics Harvey Gratt Computer Support 3 10-09-2005 02:40 AM



Advertisments