Example usage for java.awt Component print

List of usage examples for java.awt Component print

Introduction

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

Prototype

public void print(Graphics g) 

Source Link

Document

Prints this component.

Usage

From source file:Main.java

/**
 * Takes a snapshot of the target component.
 *
 * @param component the component to draw
 * @param usePrint  whether <tt>print()</tt> or <tt>paint()</tt> is used to grab the snapshot
 * @return a Graphics compatible image of the component
 *//* w  ww.  j  a  va 2 s . co m*/
public static Image takeSnapshot(Component component, boolean usePrint) {
    BufferedImage image = null;
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = genv.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    if (gc.getColorModel().hasAlpha()) {
        image = gc.createCompatibleImage((int) component.getSize().getWidth(),
                (int) component.getSize().getHeight());
    } else {
        image = new BufferedImage((int) component.getSize().getWidth(), (int) component.getSize().getHeight(),
                BufferedImage.TYPE_INT_ARGB);
    }

    Graphics g = image.getGraphics();
    if (usePrint) {
        component.print(g);
    } else {
        component.paint(g);
    }
    g.dispose();

    return image;
}

From source file:com.simiacryptus.util.Util.java

/**
 * To image buffered image./*from w  ww .j a  v  a2 s.c om*/
 *
 * @param component the component
 * @return the buffered image
 */
public static BufferedImage toImage(@javax.annotation.Nonnull final Component component) {
    try {
        com.simiacryptus.util.Util.layout(component);
        @javax.annotation.Nonnull
        final BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(),
                BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics2D g = img.createGraphics();
        g.setColor(component.getForeground());
        g.setFont(component.getFont());
        component.print(g);
        return img;
    } catch (@javax.annotation.Nonnull final Exception e) {
        return null;
    }
}