draw Image To Icon - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

draw Image To Icon

Demo Code


//package com.java2s;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;

public class Main {
    private static byte tileWidth = 64;
    private static byte tileHeight = 64;

    public static ImageIcon drawImageToIcon(ImageIcon canvas,
            BufferedImage paint, Point origin) {

        // Convert the icon to a bufferedimage of the same dimensions so you can easily manipulate it
        BufferedImage img = new BufferedImage(canvas.getIconWidth(),
                canvas.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.createGraphics();
        g.drawImage(canvas.getImage(), 0, 0, null);

        // Draw the 'paint' BufferedImage param to the newly created BufferedImage
        g.drawImage(paint, origin.x - (origin.x % tileWidth), origin.y
                - (origin.y % tileHeight), null);

        // Set the modified BufferedImage back to the ImageIcon param
        canvas.setImage(img);/* w  w w.  j  a  v a 2 s  .  c om*/

        // Finally, return the modified ImageIcon
        return canvas;
    }
}

Related Tutorials