Java Icon createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname)

Here you can find the source of createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname)

Description

create Hist Image

License

Open Source License

Declaration

static File createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname) throws IOException 

Method Source Code


//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);
    }
}

Related

  1. createColorIcon(Color color)
  2. createColorIcon(final int width, final int height, final Color color)
  3. createRotatedImage(Component c, Icon icon, double rotatedAngle)
  4. extractIconImage(Component component, Icon icon)
  5. genImageResource(Class cls, String icon)
  6. getChatIconImage()