Tiles an image across the defined area. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Tiles an image across the defined area.

Demo Code


//package com.java2s;

import java.awt.Graphics;
import java.awt.Image;

import java.awt.image.ImageObserver;

public class Main {
    /**//  w w w. j  a  v  a 2s.c o  m
     * Tiles an image across the defined area.
     */
    public static void tileImage(Graphics g, int x, int y, int width,
            int height, Image image, ImageObserver obs) {
        int mWidth = image.getWidth(obs);
        int mHeight = image.getHeight(obs);
        if (mWidth > 0 && mHeight > 0) {
            for (int i = x; i < x + width; i += mWidth) {
                for (int j = y; j < y + height; j += mHeight) {
                    g.drawImage(image, i, j, obs);
                }
            }
        }
    }
}

Related Tutorials