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 / May 2007

Tip: Looking for answers? Try searching our database.

Taking a Screenshot in Java

Thread view: 
Hal Vaughan - 03 May 2007 22:25 GMT
I've been trying to Google for this, but considering one of the terms
is "screenshot," it's almost impossible to get past the noise on this.  I
want to have my program take a screenshot of the entire screen when a
button in a Swing window is pressed.  I do mean I need a full screenshot,
as in not just the Java windows, but everything on the screen at that time.

I admit I don't know the graphics API too well, but I can't find a reference
to any thing in there.  If I've missed the obvious, it would certainly make
things a lot easier.  I figure, though, I'll need to find an open source
class somewhere that does this.

Thanks for any help or suggestions!

Hal
Andrew Thompson - 04 May 2007 02:06 GMT
>I've been trying to Google for this, but considering one of the terms
>is "screenshot,"

Mine was "java screenshot" and the top link was
to Marco Schmidt's site, and linked to a 50ish line
source that mentioned the class you need.

>Thanks for any help or suggestions!

Add 'java' to any searches.

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Hal Vaughan - 04 May 2007 03:48 GMT
>>I've been trying to Google for this, but considering one of the terms
>>is "screenshot,"
>
> Mine was "java screenshot" and the top link was
> to Marco Schmidt's site, and linked to a 50ish line
> source that mentioned the class you need.

I used several different variations.  Looking back, I also see I
used "screenshots" instead of the singular.  I've also found that one's
Google results can vary depending on where they are and which Google server
it goes to.

I just searched for Marco Schmidt and Java and got his site and fount it
from there.

>>Thanks for any help or suggestions!
>
> Add 'java' to any searches.

It was in there.  Actually, I was using several different sets of search
terms, I just had not used the specific combination you did.  I had tried
adding "how to" and other terms, but had not gotten the site you mentioned
until I added in Macro's name.

I did get a LOT of hits on homepages for different Java programs that
included screenshots of their own windows and features, though.

Hal
Andrew Thompson - 04 May 2007 06:51 GMT
>>>I've been trying to Google for this, but considering one of the terms
>>>is "screenshot,"
[quoted text clipped - 4 lines]
>
>I used several different variations.  

>...  I've also found that one's
>Google results can vary depending on where they are and which Google server
>it goes to.

Really?  I, in turn never fail to *verify* that the
top 4-5 hits are identical for my 'standard search', e.g.
<http://www.google.com.au/search?hl=en&q=java+screenshot&meta=>
& the optimised and internationalised version of that URL, e.g...
<http://www.google.com/search?q=java+screenshot>
(also shorter).

And, like all other times, the first 4-5 hits (all I ever
bother checking) were the same.   The top hit now is the
same as the top hit last time I checked those search terms.
it points here..
<http://schmidt.devlib.org/java/save-screenshot.html>

Where do you Google from?

>I just searched for Marco Schmidt and Java and got his site and fount it
>from there.

So.. you're sorted?

>>>Thanks for any help or suggestions!
>>
[quoted text clipped - 4 lines]
>adding "how to" and other terms, but had not gotten the site you mentioned
>until I added in Macro's name.

That would be 'Marco' and you did not need to add it
to the search term.  His page was (and still is) the top hit
for 'java screenshot'.

Maybe you need to brush up your 'Google foo'.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Hal Vaughan - 05 May 2007 04:50 GMT
>>>>I've been trying to Google for this, but considering one of the terms
>>>>is "screenshot,"
[quoted text clipped - 43 lines]
>
> Maybe you need to brush up your 'Google foo'.

Okay, I really do appreciate your help.  It pointed me to an answer I
needed.  Maybe the results were because I used "screenshots" instead
of "screenshot" or maybe, when I used Java, it was at the end of my search
terms.  I've also found, in the past, that I got different answers
depending on the order of the search terms.  I may have even added extra
terms to all the searches.  I don't remember exactly which searches I did.
To be honest, with my learning disability, it is hard for me to keep
sequences straight all the time.

I did try, I didn't find, and I appreciate your help.  I may have added
terms related to graphics and, as the result shows, the answer is not in a
Swing or AWT class, but in a class I had never heard of before.  (Yes, I'm
self taught.  I've learned all my programming except a class in BASIC in
high school and Assembler in college years ago.  I've taught myself and I
have a learning disability that sometimes makes identifying symbols, i.e.
words and letters, a bit confusing.)

Hal
IchBin - 04 May 2007 14:06 GMT
> I've been trying to Google for this, but considering one of the terms
> is "screenshot," it's almost impossible to get past the noise on this.  I
[quoted text clipped - 10 lines]
>
> Hal

OK, here is an example:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

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...           http://weconsultants.prophp.org
IchBin, Philadelphia, Pa, USA  http://ichbinquotations.awardspace.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Hal Vaughan - 05 May 2007 04:52 GMT
>> I've been trying to Google for this, but considering one of the terms
>> is "screenshot," it's almost impossible to get past the noise on this.  I
[quoted text clipped - 50 lines]
>      }
> }

The big key that confused me is that I had looked in some Swing classes and
had never heard of the Robot class.  I don't know if it's a common class or
one used very often, but it is not the place I would have expected to find
a screen capture method.

Thank you for a simple, direct answer that would clear up any questions
quickly and provide the help I needed.

Hal


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.