Create a colored image icon. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

Create a colored image icon.

Demo Code

/*//from  w  w  w.j  a  v a 2s  .  c  o  m
 * Copyright 2008-2014, David Karnok 
 * The file is part of the Open Imperium Galactica project.
 * 
 * The code should be distributed under the LGPL license.
 * See http://www.gnu.org/licenses/lgpl.html for details.
 */
//package com.java2s;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Create a colored image icon.
     * @param w the width
     * @param h the height
     * @param c the fill color
     * @return the image icon
     */
    public static ImageIcon createColorImageIcon(int w, int h, int c) {
        BufferedImage img = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                img.setRGB(j, i, c);
            }
        }
        return new ImageIcon(img);
    }
}

Related Tutorials