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 / November 2005

Tip: Looking for answers? Try searching our database.

Capture ActiveWindow

Thread view: 
aidy - 18 Nov 2005 14:04 GMT
Hi,

I am trying to take a screenshot of an active window and save it to a
file. I've used some code from the SUN site

<code>
import java.awt.Rectangle;
import java.awt.Robot;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;

public class Main {
    try{
//        Get the screen size
        Rectangle rectangle = new Rectangle(this.getBounds());
        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);

//        Save the screenshot as a jpg
        file = new File("screen.jpg");
        ImageIO.write(image, "jpg", file);
    }
        catch (Exception e)
        {
        System.out.println(e.getMessage());
        }
 }
<code>

As you may guess I am new to Java.

q1. I am having problems with what to put in the above classes getbound
method.
q2. I am getting compile errors on the main braces for some reason.

Any pointers would be greatly appreciated.

Aidy
Andrew Thompson - 18 Nov 2005 14:34 GMT
> I am trying to take a screenshot of an active window and save it to a
> file. I've used some code from the SUN

That name is 'Sun' (it is not an acronym)

>..site

Where?  Sun has a big site with a rather poor search engine,
what specific page/URL did you find this code on?

> <code>
> import java.awt.Rectangle;
...

> public class Main {

If the code on Sun calls this class 'Main', toss it away
as simply being a bad example of *any* Java code, let alone
tutorial code.

OTOH, ..

>     try{

This line immediately after it suggests that this was
originally in a ..

  static void main(String[] args) {
     // ....
  }

..method, or another method, which is where it needs to be.

If not, then perhaps you'd better go back to the original
code from the Sun site (which we might hope actually compiles
and works) before attempting to change it to your needs.

> //        Get the screen size
>         Rectangle rectangle = new Rectangle(this.getBounds());

While 'this' might suggest that the line was in a constructor,
or a non-static method.
...
> As you may guess I am new to Java.

Yes, it is pretty obvious from your code.
Which leads me to two points,
1) There is a better groups for those new to Java
<http://www.physci.org/codes/javafaq.jsp#cljh>
2) You should not be programming applications to do with GUI's
or images (tricky, in Java) just yet.
<http://www.physci.org/codes/javafaq.jsp#appfirst>

> q1. I am having problems with what to put in the above classes getbound
> method.

You can use the screensize.

> q2. I am getting compile errors on the main braces for some reason.

No, not for 'some reason', but for a very specific reason..
F:\...\Main.java:10: illegal start of type
    try{

Please be specific when quoting errors, as your readers
do not have a crystal ball, and while *that* is the error
that *I* am getting when compiling your code, it might not
be the same error that you are getting (though it probably
is).

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Currently accepting short and long term contracts - on Earth.

Roedy Green - 18 Nov 2005 15:58 GMT
On Fri, 18 Nov 2005 14:34:59 GMT, Andrew Thompson
<seemysites@www.invalid> wrote, quoted or indirectly quoted someone
who said :

> Sun has a big site with a rather poor search engine,

no way around that is to use google with a "site:sun.com" modifier
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

aidy - 18 Nov 2005 16:11 GMT
Hi,

I've compiled the below code without errors.

<code>
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class ScreenShot {

    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());
        }
    }
}
<code>

The code is from
http://www.codebeach.com/tutorials/screenshots-java.asp

The code grabs the entire screen, but I thought that if I changed the
screenSize reference, I could attempt to retrieve the active window.

I am using the Eclipse IDE and I have set the classpath to the default
package. In that package is the above Java class.

I attempt to run the app but I am getting this error msg.

"java.lang.NoClassDefFoundError:"

Now, I have googled, and the info I get is that the JVM cannot see the
class. But I cannot see why if I have set the classpath.

Thanks for you help

Aidy
IchBin - 18 Nov 2005 18:40 GMT
> Hi,
>
> I've compiled the below code without errors.
>
> <code>
 [deleted code]
> Aidy

I ran under Eclipse 3.1.1 with no problem. Well, I had to add public to
the main method so Eclipse would run it as an application.

Check and make sure that your project has the JRE System Library in it's
'Java Build Path'. Outside of that... should work.
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-)

Roedy Green - 18 Nov 2005 20:13 GMT
>NoClassDefFoundError

see http://mindprod.com/jgloss/caq.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Joan - 18 Nov 2005 18:40 GMT
>> I am trying to take a screenshot of an active window and save
>> it to a
>> file. I've used some code from the SUN
>
> That name is 'Sun' (it is not an acronym)

See this page about McNealy
http://www.referenceforbusiness.com/biography/M-R/McNealy-Scott-G-1954.html
...He [McNealy] met Andy Bechtolsheim, a PhD student who was
working on a project called the Stanford University Network, or
SUN

>>..site
>
[quoted text clipped - 62 lines]
> be the same error that you are getting (though it probably
> is).


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



©2009 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.