Here you can find the source of captureAsScreenshot(final Frame frame)
Parameter | Description |
---|---|
frame | JComponent to capture screenshot |
public static BufferedImage captureAsScreenshot(final Frame frame)
//package com.java2s; /**/*from w w w. jav a 2 s . co m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.AWTException; import java.awt.Component; import java.awt.Dialog; import java.awt.Frame; import java.awt.Point; import java.awt.Rectangle; import java.io.File; import java.io.IOException; import javax.swing.JComponent; import java.awt.Robot; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class Main { /** * Captures screenshot of component and saves to a file. * @param component JComponent to capture screenshot * @param saveAs file name */ public static void captureAsScreenshot(final JComponent component, final File saveAs) { try { Robot robot = new Robot(); // Create Rectangle around component Point locOnScreen = component.getLocationOnScreen(); Rectangle bounds = component.getBounds(); bounds.setLocation(locOnScreen); BufferedImage bi = robot.createScreenCapture(bounds); ImageIO.write(bi, "png", saveAs); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Captures screenshot of component and saves to a file. * @param frame JComponent to capture screenshot * @param saveAs file name */ public static void captureAsScreenshot(final Frame frame, final File saveAs) { try { Robot robot = new Robot(); Rectangle bounds = getInternalRectangle(frame); BufferedImage bi = robot.createScreenCapture(bounds); ImageIO.write(bi, "png", saveAs); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Captures screenshot of component and saves to a file. * @param dialog JComponent to capture screenshot * @param saveAs file name */ public static void captureAsScreenshot(final Dialog dialog, final File saveAs) { try { Robot robot = new Robot(); Rectangle bounds = getInternalRectangle(dialog); BufferedImage bi = robot.createScreenCapture(bounds); ImageIO.write(bi, "png", saveAs); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Captures screenshot of component and returns bufferedImage. * @param dialog JComponent to capture screenshot * @return BufferedImage of screenshot */ public static BufferedImage captureAsScreenshot(final Dialog dialog) { BufferedImage bi = null; try { Robot robot = new Robot(); Rectangle bounds = getInternalRectangle(dialog); bi = robot.createScreenCapture(bounds); } catch (AWTException e) { e.printStackTrace(); } return bi; } /** * Captures screenshot of component nd returns as BufferedImage. * @param component JComponent to capture screenshot * @return BufferedImage screenshot of component */ public static BufferedImage captureAsScreenshot(final Component component) { BufferedImage bi = null; try { Robot robot = new Robot(); // Create Rectangle around component Point locOnScreen = component.getLocationOnScreen(); Rectangle bounds = component.getBounds(); bounds.setLocation(locOnScreen); bi = robot.createScreenCapture(bounds); } catch (AWTException e) { e.printStackTrace(); } return bi; } /** * Captures screenshot of component nd returns as BufferedImage. * @param frame JComponent to capture screenshot * @return BufferedImage screenshot of component */ public static BufferedImage captureAsScreenshot(final Frame frame) { BufferedImage bi = null; try { Robot robot = new Robot(); // Create Rectangle around component Rectangle bounds = getInternalRectangle(frame); bi = robot.createScreenCapture(bounds); } catch (AWTException e) { e.printStackTrace(); } return bi; } /** * Returns a rectangle of the inside of the frame i.e. excluding borders. * @param frame frame to get rectangle from * @return frame Rectangle without borders */ public static Rectangle getInternalRectangle(final Frame frame) { // Create Rectangle around component Point locOnScreen = frame.getLocationOnScreen(); Rectangle bounds = frame.getBounds(); // Compensate for frame boundary locOnScreen.setLocation(locOnScreen.x + frame.getInsets().left, locOnScreen.y + frame.getInsets().top); bounds.setRect(0, 0, bounds.getWidth() - frame.getInsets().left - frame.getInsets().right, bounds.getHeight() - frame.getInsets().top - frame.getInsets().bottom); bounds.setLocation(locOnScreen); return bounds; } /** * Returns a rectangle of the inside of the dialog i.e. excluding borders. * @param dialog dialog to get rectangle from * @return dialog Rectangle without borders */ public static Rectangle getInternalRectangle(final Dialog dialog) { // Create Rectangle around component Point locOnScreen = dialog.getLocationOnScreen(); Rectangle bounds = dialog.getBounds(); // Compensate for frame boundary locOnScreen.setLocation(locOnScreen.x + dialog.getInsets().left, locOnScreen.y + dialog.getInsets().top); bounds.setRect(0, 0, bounds.getWidth() - dialog.getInsets().left - dialog.getInsets().right, bounds.getHeight() - dialog.getInsets().top - dialog.getInsets().bottom); bounds.setLocation(locOnScreen); return bounds; } }