Capture ActiveWindow

A

aidy

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
 
A

Andrew Thompson

aidy said:
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)

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, ..

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

aidy

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
 
J

Joan

Andrew Thompson said:
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


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, ..

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

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

IchBin

aidy said:
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.
--

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

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top