draw Icon To Icon - Java 2D Graphics

Java examples for 2D Graphics:Icon

Description

draw Icon 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 drawIconToIcon(ImageIcon canvas,
            ImageIcon paint, Point origin) {
        // Convert the "paint" ImageIcon to a BufferedImage
        BufferedImage img = new BufferedImage(tileWidth, tileHeight,
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.createGraphics();
        g.drawImage(paint.getImage(), 0, 0, null);

        // Now that the Icon is an image, call the drawImageToIcon method
        return drawImageToIcon(canvas, img, origin);
    }/*  w ww.  java  2 s .co  m*/

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

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

Related Tutorials