Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Java Swing: Graphics to Image?

Thread view: 
Noddie - 01 Mar 2006 17:51 GMT
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
IchBin - 01 Mar 2006 18:36 GMT
> Hi.
> I'm trying to get some sort of "screenshot" from a´Graphics object to an
[quoted text clipped - 4 lines]
>
> 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());
        }
    }
}

Signature

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-)

Oliver Wong - 01 Mar 2006 20:34 GMT
> Hi.
> I'm trying to get some sort of "screenshot" from a´Graphics object to an
[quoted text clipped - 4 lines]
>
> 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
Thomas Weidenfeller - 02 Mar 2006 08:28 GMT
> 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
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.