Java JComponent to Image buildImage(Component c)

Here you can find the source of buildImage(Component c)

Description

create an image of the component

License

Apache License

Parameter

Parameter Description
c non-null component

Declaration

public static Image buildImage(Component c) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.*;

public class Main {
    /**/* w w w  . j a va2 s.c  o m*/
     * create an image of the component
     *
     * @param c non-null component
     * @return
     */
    public static Image buildImage(Component c) {
        Dimension size = c.getSize();
        int width = (int) size.getWidth();
        int height = (int) size.getHeight();
        Image ret = c.createImage(width, height);
        if (ret != null) {
            Graphics g = ret.getGraphics();
            Color background = c.getBackground();
            if (background == null)
                background = Color.white;
            g.setColor(background);
            g.fillRect(0, 0, (int) size.getWidth(), (int) size.getHeight());
            c.paint(g);
        }
        return ret;
    }
}

Related

  1. componentToImage(Component c)
  2. componentToImage(Component c)
  3. componentToImage(Component component, int resolution)
  4. doScreenshotToFile(final Component container, final Path filePath, final String imageType)