draw Icon To Icon Repeatedly - Java 2D Graphics

Java examples for 2D Graphics:Icon

Description

draw Icon To Icon Repeatedly

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 drawIconToIconRepeatedly(ImageIcon canvas,
            ImageIcon paint) {//from  www  .  j  a va  2s.  c o m
        // Translate the "paint" param to a BufferedImage
        BufferedImage source = new BufferedImage(paint.getIconWidth(),
                paint.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = source.createGraphics();
        g.drawImage(paint.getImage(), 0, 0, null);

        // Translate the canvas to a buffered img
        BufferedImage destination = new BufferedImage(
                canvas.getIconWidth(), canvas.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        g = destination.createGraphics();
        g.drawImage(canvas.getImage(), 0, 0, null);

        // Draw "paint" over the "canvas" until the canvas is completely covered
        for (int row = 0; row < destination.getWidth() / tileWidth; row++) {
            for (int col = 0; col < destination.getHeight() / tileHeight; col++) {
                g.drawImage(source, row * tileWidth, col * tileHeight, null);
            }
        }

        // Translate the modified canvas back into an ImageIcon
        canvas.setImage(destination);

        // Return the modified ImageIcon
        return canvas;
    }
}

Related Tutorials