Skip to content

Instantly share code, notes, and snippets.

@DV8FromTheWorld
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save DV8FromTheWorld/9b76d5fc8c664129ade2 to your computer and use it in GitHub Desktop.

Select an option

Save DV8FromTheWorld/9b76d5fc8c664129ade2 to your computer and use it in GitHub Desktop.
/**
* Captures the currently selected area of the screen and sends it to the
* System Clipboard as an image.
*
* Sets the capture button non-visible so it is not captured in the screenshot.
*/
private void captureArea()
{
btnCapture.setVisible(false);
this.repaint();
//We run the screen capture in a different thread so that we can wait while
//the button is becoming invisible. (We don't want the button in the image).
new Thread(new Runnable()
{
@Override
public void run()
{
Robot r;
Point loc = pnlCapture.getLocationOnScreen();
Rectangle imageArea = new Rectangle(
loc.x, loc.y, pnlCapture.getWidth(), pnlCapture.getHeight());
try
{
Thread.sleep(20);
r = new Robot();
BufferedImage i = r.createScreenCapture(imageArea);
UploaderFrame.CLIPBOARD.setContents(new ClipboardImage(i), null);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (AWTException e)
{
e.printStackTrace();
}
btnCapture.setVisible(true);
}
}).start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment