Java JComponent Size paint(final Component c, Dimension size)

Here you can find the source of paint(final Component c, Dimension size)

Description

Paint a Component as a BufferedImage using its preferred size.

License

Open Source License

Parameter

Parameter Description
c the component to paint
size an optional size to impose on the argument. If null, then the preferred size is used.

Return

a rendering of the component.

Declaration

protected static BufferedImage paint(final Component c, Dimension size) 

Method Source Code

//package com.java2s;
/*//from   w ww. java 2s  . c om
 * @(#)BlogHelper.java
 *
 * $Date: 2014-11-27 07:50:51 +0100 (Do, 27 Nov 2014) $
 *
 * Copyright (c) 2014 by Jeremy Wood.
 * All rights reserved.
 *
 * The copyright of this software is owned by Jeremy Wood. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jeremy Wood. For details see accompanying license terms.
 * 
 * This software is probably, but not necessarily, discussed here:
 * https://javagraphics.java.net/
 * 
 * That site should also contain the most recent official version
 * of this software.  (See the SVN repository for more details.)
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import java.awt.Graphics2D;
import java.awt.LayoutManager;

import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class Main {
    /** Paint a Component as a BufferedImage using its preferred size. 
     * 
     * @param c the component to paint
     * @param size an optional size to impose on the argument. If null,
     * then the preferred size is used. 
     * @return a rendering of the component.
     */
    protected static BufferedImage paint(final Component c, Dimension size) {
        if (size == null)
            size = c.getPreferredSize();
        final BufferedImage bi = new BufferedImage(size.width, size.height,
                BufferedImage.TYPE_INT_ARGB);
        try {
            Runnable runnable1 = new Runnable() {

                private void revalidate(Component c) {
                    if (c instanceof Container) {
                        Container c2 = (Container) c;
                        LayoutManager lm = c2.getLayout();
                        if (lm != null)
                            lm.layoutContainer(c2);
                        for (int a = 0; a < c2.getComponentCount(); a++) {
                            revalidate(c2.getComponent(a));
                        }
                    }
                    c.validate();
                    c.revalidate();
                }

                public void run() {
                    c.setSize(new Dimension(bi.getWidth(), bi.getHeight()));
                    revalidate(c);
                }
            };
            Runnable runnable2 = new Runnable() {
                public void run() {
                    Graphics2D g = bi.createGraphics();
                    c.paint(g);
                    g.dispose();
                }
            };
            if (SwingUtilities.isEventDispatchThread()) {
                runnable1.run();
                runnable2.run();
            } else {
                SwingUtilities.invokeAndWait(runnable1);
                //Ugh. I'm not sure why this is necessary, but this
                //delay lets the PromptSearchDemo demo render
                //correctly.
                Thread.sleep(1000);
                SwingUtilities.invokeAndWait(runnable2);
            }
            return bi;
        } catch (RuntimeException e) {
            throw e;
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }
}

Related

  1. makeEqualSize(JComponent reference, JComponent... others)
  2. makeSameSize(Component... components)
  3. makeSameSize(JComponent... comps)
  4. makeSameSize(JComponent[] comps)
  5. minSize(T comp, int width)
  6. preferredSize(JComponent component)
  7. resize(JComponent component, Dimension size)
  8. resizeComponentWidth(JComponent currentComponent, double maxComponentWidth)
  9. restrictWindowMinimumSize(final Window wnd, final Dimension minSize)