Java JComponent Padding paint(JComponent jc, int padding)

Here you can find the source of paint(JComponent jc, int padding)

Description

Paint a JComponent as a BufferedImage

License

Open Source License

Parameter

Parameter Description
jc the component to paint
padding a uniform padding

Return

an image rendering of this component

Declaration

protected static BufferedImage paint(JComponent jc, int padding) 

Method Source Code

//package com.java2s;
/*//from w ww  .j  av a2  s.co m
 * @(#)FilledButtonUIDemoHelper.java
 *
 * $Date: 2014-06-06 20:04:49 +0200 (Fr, 06 Jun 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.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

public class Main {
    /** Paint a JComponent as a BufferedImage
     * 
     * @param jc the component to paint
     * @param padding a uniform padding
     * @return an image rendering of this component
     */
    protected static BufferedImage paint(JComponent jc, int padding) {
        Dimension d = jc.getSize();
        BufferedImage bi = new BufferedImage(d.width + 2 * padding,
                d.height + 2 * padding, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
        g.translate(padding, padding);
        jc.paint(g);
        g.dispose();
        return bi;
    }
}

Related

  1. addPadding(JComponent comp, int topbottom, int leftright)
  2. makePaddedPanel(JComponent aComponent, Insets aInsets)
  3. pad(JComponent component, int w, int h)
  4. setComponentHeight(JComponent setme, JComponent getme, int padding)
  5. setLeftPadding(JComponent component, int padding)