Example usage for java.awt Component printAll

List of usage examples for java.awt Component printAll

Introduction

In this page you can find the example usage for java.awt Component printAll.

Prototype

public void printAll(Graphics g) 

Source Link

Document

Prints this component and all of its subcomponents.

Usage

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Write AWT/Swing component to GIF file.
 * /*  ww w. j  a v a  2  s . c  o  m*/
 * @param image  Image to write.
 * @param file   File to erite to.
 */
public static void writeFile(Component component, File file) throws AWTException, IOException {
    Image image = component.createImage(component.getWidth(), component.getHeight());
    Graphics graphics = image.getGraphics();
    component.printAll(graphics);

    GifEncoder.writeFile(image, file);
}

From source file:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

/**
 * @return the {@link java.awt.Image} of given {@link Component}. Must be called from AWT disp
 *         thread./*from w  w  w.j a  v a 2 s  . com*/
 */
static java.awt.Image createComponentShotAWT(final Component component) throws Exception {
    Assert.isNotNull(component);
    // prepare sizes
    final int componentWidth = component.getWidth();
    final int componentHeight = component.getHeight();
    final int imageWidth = Math.max(1, componentWidth);
    final int imageHeight = Math.max(1, componentHeight);
    // prepare empty image
    final BufferedImage componentImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    // If actual size on component is zero, then we are done.
    if (componentWidth == 0 || componentHeight == 0) {
        return componentImage;
    }
    // some components like JLabel are transparent by default and printing it separately gives
    // bad results like invalid background color. The workaround is to set opacity property and
    // restore old value back when all done.
    ComponentShotConfigurator shotConfigurator = new ComponentShotConfigurator(component);
    try {
        // Linux only: it seems that printAll() should be invoked in AWT dispatch thread 
        // to prevent deadlocks between main thread and AWT event queue.
        // See also SwingUtils.invokeLaterAndWait().
        runInDispatchThread(new Runnable() {
            public void run() {
                component.printAll(componentImage.getGraphics());
            }
        });
    } finally {
        shotConfigurator.dispose();
    }
    // convert into SWT image
    return componentImage;
}