Here you can find the source of createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname)
static File createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname) throws IOException
//package com.java2s; //License from project: Open Source License import javax.imageio.ImageIO; import javax.imageio.ImageTypeSpecifier; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.*; public class Main { static File createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname) throws IOException { int w = c.getWidth(); int h = c.getHeight(); BufferedImage cHistImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = cHistImage.createGraphics(); g2.setColor(bgColor);/*from w w w.ja va 2 s . c o m*/ g2.fillRect(0, 0, w, h); hd.paintIcon(c, g2, 0, 0); File f = new File(dir, fname); writeImage(cHistImage, f); return f; } static void writeImage(BufferedImage image, File f) throws IOException { OutputStream chistOut = new BufferedOutputStream(new FileOutputStream(f), 4096); Iterator writers = ImageIO.getImageWritersByFormatName("png"); ImageWriter writer = (ImageWriter) writers.next(); ImageOutputStream ios = ImageIO.createImageOutputStream(chistOut); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); param.setDestinationType(new ImageTypeSpecifier(image)); writer.write(image); } }