• Advertisement
Sign in to follow this  

Java Applet and taking a screenshot?

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, first of all i must say that i am pretty much a beginner in Java and i have no experience with throw and catch in c(++) either. For some project for college (no, it's no 'help me with my homework' question) i need to take a screenshot of the desktop or at least the contents of the window the applet is in. I found a way of doing this in an application using the Robot class but i got a problem: The samples i found use the main function that throws an exception ('throws Exception'). I am using an applet and i want to make a screenshot whenever i want but i get this error: "unreported exception java.awt.AWTException; must be caught or declared to be thrown" I tried just copying the class out of the examples but i still get the error whenever i call the main function. Can anyone help me? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I need the applet to take a screenshot and draw that image in a particular way inside the applet, so i need the applet to take a screenshot on command and store it in an Image or BufferedImage object.

Share this post


Link to post
Share on other sites
Java doesn't like it when an exception could happen at runtime and you don't account for it. Somewhere in your code there is a method call or something that could trigger an AWTException. Locate it and wrap a try block around it and catch the exception (if the exception is impossible given your circumstances, you can just write an empty catch block). Example:


public void myMethod() {

//some code that could throw an AWTException

}


The compiler doesn't like that, because I don't do anything to ensure that the program or applet won't crash at runtime.

I should change it to either:

public void myMethod() throws AWTException {

//some code that could throw an AWTException

}


And handle the exception in the calling method, or:

public void myMethod() {

try {

//some code that could throw an AWTException

}

catch(AWTException e) {

//stuff to do if an AWTException is thrown

}

}


(Catch the exception right here so the calling method doesn't have to worry about it.)

Share this post


Link to post
Share on other sites
Thanks, that solved it, i do have a new problem and that is that i get this runtime error:

java.security.AccessControlException: access denied (java.awt.AWTPermission crea
teRobot)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:269)
at java.security.AccessController.checkPermission(AccessController.java:
401)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
at java.awt.Robot.checkRobotAllowed(Robot.java:122)
at java.awt.Robot.init(Robot.java:111)
at java.awt.Robot.<init>(Robot.java:73)
(...)

So if i'm not allowed to create a Robot object in an applet, is it possible to make a screenshot (either of the full screen or the internet browser window the applet is in) in another way?
Thanks.

Share this post


Link to post
Share on other sites
That security policy is in place for a good reason - imagine if you published that applet on a web page, and users could run your applet, and it was allowed to take a screen-shot of their desktop and silently send it back to your server! That's the sort of thing that makes people start to think "project for college eh? mm hmm... [rolleyes]" [wink]

Anyway. You either need to arrange the appropriate permissions for the applet (don't know any details, sorry), or write a proper application instead. Do you really need to see the other window contents, or just the stuff actually generated by the Applet? o_O

Share this post


Link to post
Share on other sites
Ok, first of all, what's the point with sending screenshots of someone's desktop??? I can imagine how irritating an applet that randomly moves your mousecursor would be so i take that as the reason robots aren't allowed in web pages.

Anyway i will explain my idea a bit further:

As a project for college we must make a website about our college with some general info and information that is of interest for a certain group of people (students / companies / whatever).
One of the things we must add is a so-called 'eye-catcher' which should be some interactive java applet that is placed in the website and i had this fun idea:

When the user left-clicks the eye-catcher (a small square somewhere in a corner of the browser window) the applet takes a screenshot, resizes to the full size of the browser window and draws the offsetted screenshot (just the window contents) again as background so the user sees no difference. This image will be the playground for (whatever the user chooses) a breakout, space invaders or pacman clone.
I think it's pretty funny to just convert the exact website layout to a game and letting the user completely destroy it.

So that's why :).

So i need some way to read pixels off the screen, even if it's only inside the browser window it's fine.
If there's no way of doing that i think i will just have to make the screenies myself.

Share this post


Link to post
Share on other sites
omg, man.
With full screen copying, it's huge no-no with java, or web in general. It woulda be almost as huge security hole as using microphone without permission. If that woulda be possible, i woulda instantly swich to browser that doesn't make it possible. There's nothing wrong on my screen, but anyway, i have some program sources(my and not my) that absolutely must *not* be distributed, and if that woulda be possible i woulda expect it to also give at least read-only access to my hard drive, etc.

In summary: probably there's no way unless
1: you'll hack browsers (another no-no)
2: every user will have low security for your site, basically will trust your site VERY much (Hey, i want full access to your system, it's to play that game).

If you want to copy only inside part of window , i don't know how to do it... it might be possible.

Share this post


Link to post
Share on other sites
I see. Would it be possible to resize the applet to the full window without redrawing the webpage and then using basic Graphics functions to get the contents of the window? First of all setBounds doesn't seem to work inside webpages, then there is the ugly background of the applet which might be automatically drawn when resizing the applet (even when i set ignoreRepaint(true)) and then there is the chance that the getPixel method will read the pixel inside the applet graphics context which might not contain the window contents but clear pixels.

Has anyone tried this before?

Share this post


Link to post
Share on other sites
Hi,

You could look into WebStart to get past security. This will let you use all the features of Java you like, but users of your site will have to agree to let this slide (they're presented with a 'Security Certificate' whenever they visit the site). Also, it's a bit of a bulky download (about 5 MB) and, to my knowldge, doesn't come pre-installed with Windows.

Cheers,
--Brian

Share this post


Link to post
Share on other sites
Thanks, i will look it up, together with the posibilities to download it from a cd (the pc we will be presenting it on probably won't be connected to the internet).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement