Here you can find the source of componentToImageIcon(Component c, String description, boolean paintBorder)
public static ImageIcon componentToImageIcon(Component c, String description, boolean paintBorder)
//package com.java2s; /*/*from w w w. ja v a 2s .c o m*/ * Copyright (C) 2006 The Concord Consortium, Inc., * 25 Love Lane, Concord, MA 01742 * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * END LICENSE */ import java.awt.AWTException; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JComponent; public class Main { private static Robot robot; /** create an ImageIcon delegate of a component */ public static ImageIcon componentToImageIcon(Component c, String description, boolean paintBorder) { return componentToImageIcon(c, description, paintBorder, 1, Image.SCALE_SMOOTH); } /** create an ImageIcon delegate of a component with the specified scale */ public static ImageIcon componentToImageIcon(Component c, String description, boolean paintBorder, float scale, int hints) { if (c == null) return null; if (!c.isShowing()) return null; if (scale > 1.0f || scale <= 0.0f) throw new IllegalArgumentException("scale must be in (0, 1]."); Dimension size = c.getSize(); BufferedImage bi = null; if (c instanceof JComponent) { bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); c.paint(g2); // FIXME: if c is a JEditorPane, calling the paint method forces the images to be reloaded incorrectly. g2.dispose(); // This caused empty image before. Was it fixed? } else { if (robot == null) { try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); robot = null; } } if (robot != null) { bi = robot.createScreenCapture(new Rectangle(c.getLocationOnScreen().x, c.getLocationOnScreen().y, c.getWidth(), c.getHeight())); } else { bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); c.paint(g2); g2.dispose(); } } ImageIcon icon = null; if (scale < 1.0f) { icon = new ImageIcon( bi.getScaledInstance((int) (bi.getWidth() * scale), (int) (bi.getHeight() * scale), hints)); } else { if (paintBorder) { int x = bi.getMinX(); int y = bi.getMinY(); int w = bi.getWidth(); int h = bi.getHeight(); for (int i = x; i < x + w; i++) { bi.setRGB(i, y, 0x000000); bi.setRGB(i, y + h - 1, 0x000000); } for (int i = y; i < y + h; i++) { bi.setRGB(x, i, 0x000000); bi.setRGB(x + w - 1, i, 0x000000); } } icon = new ImageIcon(bi); } icon.setDescription(description); return icon; } }