Java Code Examples for java.awt.Component#createImage()

The following examples show how to use java.awt.Component#createImage() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File:    License: Apache License 2.0 6 votes vote down vote up
private Image createCompImage(Component comp, Dimension targetSize) {
    // component won't paint if not showing anyway, so don't create
    // empty image but honestly return null 
    if (!comp.isShowing()) {
        return null;
    Image image = comp.createImage(comp.getWidth(), comp.getHeight());
    /*BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().
            createCompatibleImage(comp.getWidth(), comp.getHeight());*/
    //BufferedImage image = new BufferedImage (targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr2d = (Graphics2D)image.getGraphics();
    return image;
Example 2
private static void checkCreateImage(final Component comp,
                                     final boolean isNull) {
    if ((comp.createImage(10, 10) != null) == isNull) {
        throw new RuntimeException("Image is wrong");
    if ((comp.createVolatileImage(10, 10) != null) == isNull) {
        throw new RuntimeException("Image is wrong");
    try {
        if ((comp.createVolatileImage(10, 10, null) != null) == isNull) {
            throw new RuntimeException("Image is wrong");
    } catch (final AWTException ignored) {
        // this check is not applicable
Example 3
public static Image snapshot(Component component) {
   Image img = component.createImage(component.getSize().width,
   if (img != null) {
   Graphics igc = img.getGraphics();
   //Gotta set the clip, or else paint throws an exception
   igc.setClip(0, 0, component.getSize().width,
   return img;
Example 4
 * Creates an icon representing a color.
 * @param color            the color for the icon.
 * @param displayComponent the component to display the icon on.
 * @return the color icon.
private Icon createColorLegendIcon(Color color, Component displayComponent) {
	int[] imageArray = new int[10 * 10];
	Arrays.fill(imageArray, color.getRGB());
	Image image = displayComponent.createImage(new MemoryImageSource(10, 10, imageArray, 0, 10));
	return new ImageIcon(image);