Here you can find the source of saveComponentAsImage(final String path, final Component target, final String format)
Parameter | Description |
---|---|
path | the file to save |
target | the component to save as image |
format | a string describing the format. See http://docs.oracle.com/javase /7/docs/api/javax/imageio/package- summary.html#package_description for a list of available formats. |
Parameter | Description |
---|---|
IOException | in case of I/O Errors |
public static void saveComponentAsImage(final String path, final Component target, final String format) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (C) 2009, 2015, Danilo Pianini and contributors * listed in the project's build.gradle or pom.xml file. * * This file is distributed under the terms of the Apache License, version 2.0 *******************************************************************************/ import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main { /**/* ww w . ja va 2 s . c om*/ * Given a component, saves it on disk as image. * * @param path * the file to save * @param target * the component to save as image * @param format * a string describing the format. See * http://docs.oracle.com/javase * /7/docs/api/javax/imageio/package- * summary.html#package_description for a list of available * formats. * @throws IOException * in case of I/O Errors */ public static void saveComponentAsImage(final String path, final Component target, final String format) throws IOException { final JFrame win = (JFrame) SwingUtilities.getWindowAncestor(target); final Dimension size = win.getSize(); final BufferedImage image = (BufferedImage) win.createImage(size.width, size.height); final Graphics g = image.getGraphics(); win.paint(g); g.dispose(); ImageIO.write(image, format, new File(path)); } }