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

The following examples show how to use java.awt.Component#createImage() . 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 File: ScaleFx.java    From netbeans with 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().
            getDefaultScreenDevice().getDefaultConfiguration().
            createCompatibleImage(comp.getWidth(), comp.getHeight());*/
    //BufferedImage image = new BufferedImage (targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
    
    Graphics2D gr2d = (Graphics2D)image.getGraphics();
    
    comp.paint(gr2d);
    
    gr2d.dispose();
    
    return image;
}
 
Example 2
Source File: CreateImage.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: AbstractImageEncoder.java    From tn5250j with GNU General Public License v2.0 5 votes vote down vote up
public static Image snapshot(Component component) {
   Image img = component.createImage(component.getSize().width,
                          component.getSize().height);
   if (img != null) {
   Graphics igc = img.getGraphics();
   //Gotta set the clip, or else paint throws an exception
   igc.setClip(0, 0, component.getSize().width,
      component.getSize().height);
      component.paint(igc);
   }
   return img;
}
 
Example 4
Source File: NavigatorWindow.java    From mars-sim with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 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);
}