Java Utililty Methods JComponent to Image

List of utility methods to do JComponent to Image

Description

The list of methods to do JComponent to Image are organized into topic(s).

Method

ImagebuildImage(Component c)
create an image of the component
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)
...
BufferedImagecomponentToImage(Component c)
Sometimes you want a laid out component as an image.
BufferedImage result = null;
if (c != null) {
    Dimension bounds = c.getPreferredSize();
    result = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = result.createGraphics();
    g2d.setClip(new java.awt.Rectangle(c.getSize()));
    g2d.setComposite(AlphaComposite.Clear);
    g2d.fillRect(0, 0, bounds.width, bounds.height);
...
BufferedImagecomponentToImage(Component c)
component To Image
BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(image.getGraphics());
return image;
BufferedImagecomponentToImage(Component component, int resolution)
component To Image
BufferedImage img = new BufferedImage(component.getWidth() * resolution, component.getHeight() * resolution,
        BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = (Graphics2D) img.getGraphics();
g2.scale(resolution, resolution);
g2.setColor(component.getForeground());
g2.setFont(component.getFont());
component.paintAll(g2);
return img;
...
FiledoScreenshotToFile(final Component container, final Path filePath, final String imageType)
do Screenshot To File
final File imageFile = new File(filePath.toString());
ImageIO.write(getScreenshotImage(container), imageType, imageFile);
return imageFile;
voidensureImageLoaded(Component owner, Image image)
ensure Image Loaded
if (image != null) {
    boolean done = false;
    Toolkit tk = owner.getToolkit();
    if (tk != null) {
        int flags = tk.checkImage(image, -1, -1, owner);
        if ((flags & (ImageObserver.ABORT | ImageObserver.ALLBITS | ImageObserver.ERROR
                | ImageObserver.FRAMEBITS)) != 0) {
            done = true;
...
int[][]findConnectedComponents(int[][] image)
find Connected Components
final int nHeight = image.length;
final int nWidth = image[0].length;
int n = -1;
int[][] cc = new int[nHeight][nWidth];
for (int y = 0; y < nHeight; y++) {
    for (int x = 0; x < nWidth; x++) {
        cc[y][x] = -1;
for (int y = 0; y < nHeight; y++) {
    for (int x = 0; x < nWidth; x++) {
        if (image[y][x] == -1)
            continue;
        if (cc[y][x] != -1)
            continue;
        n = n + 1;
        Queue<Point> q = new LinkedList<Point>();
        q.add(new Point(x, y));
        cc[y][x] = n;
        while (!q.isEmpty()) {
            Point p = q.poll();
            if ((p.y > 0) && (image[p.y - 1][p.x] == image[p.y][p.x]) && (cc[p.y - 1][p.x] == -1)) {
                q.add(new Point(p.x, p.y - 1));
                cc[p.y - 1][p.x] = n;
            if ((p.x > 0) && (image[p.y][p.x - 1] == image[p.y][p.x]) && (cc[p.y][p.x - 1] == -1)) {
                q.add(new Point(p.x - 1, p.y));
                cc[p.y][p.x - 1] = n;
            if ((p.y < nHeight - 1) && (image[p.y + 1][p.x] == image[p.y][p.x])
                    && (cc[p.y + 1][p.x] == -1)) {
                q.add(new Point(p.x, p.y + 1));
                cc[p.y + 1][p.x] = n;
            if ((p.x < nWidth - 1) && (image[p.y][p.x + 1] == image[p.y][p.x])
                    && (cc[p.y][p.x + 1] == -1)) {
                q.add(new Point(p.x + 1, p.y));
                cc[p.y][p.x + 1] = n;
return cc;
voidgenerateImageFileFromComponent(Component component, String filename, String Type)
Erstellt Bilddatei aus einer (Java)Komponente
generateImageFileFromComponent(component, new File(filename), Type);
voidsetCustomCursor(Component component, Image cursorImg, String name)
set Custom Cursor
setCustomCursor(component, cursorImg, new Point(0, 0), name);
booleansetImage(Image im, Component c)
Ensures that Images is completely loaded
boolean b = true;
MediaTracker mt = new MediaTracker(c);
mt.addImage(im, 0);
try {
    mt.waitForID(0);
} catch (InterruptedException e) {
    System.out.println("Error while image loading."); 
    b = false;
...