Java JComponent to Image tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height)

Here you can find the source of tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height)

Description

Tiles a rectangular region of an AWT Graphics context with a specified image.

License

Open Source License

Parameter

Parameter Description
img Image which will be tiled in a repeating pattern.
comp AWT Component needed to draw images.
g Graphics into which tiling is done.
x Left coordinate of tiled rectangle.
y Top coordinate of tiled rectangle.
width Width of the tiled rectangle.
height Height of the tiled rectangle.

Declaration

public static void tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Shape;

public class Main {
    /**//from   w  w  w  . j  a  v  a2s  . co  m
     * Tiles a rectangular region of an AWT Graphics context with
     * a specified image. Dead cool this!.
     * @param img Image which will be tiled in a repeating pattern.
     * @param comp AWT Component needed to draw images.
     * @param g Graphics into which tiling is done.
     * @param x Left coordinate of tiled rectangle.
     * @param y Top coordinate of tiled rectangle.
     * @param width Width of the tiled rectangle.
     * @param height Height of the tiled rectangle.
     */
    public static void tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height) {
        // Image and Graphics must not be null
        if (img == null || g == null) {
            throw new NullPointerException("Image/Graphics null");
        }
        // Store the current clip of the Graphics so can restore
        // it later. Then set clip to specified rectangle.
        Shape origclip = g.getClip();
        g.setClip(x, y, width, height);

        // Get the image dimensions.
        int imgwidth = img.getWidth(comp);
        int imgheight = img.getHeight(comp);

        // Tile away!
        int ypos = y;
        while (ypos <= (y + height)) {
            int xpos = x;
            while (xpos <= (x + width)) {
                g.drawImage(img, xpos, ypos, comp);
                xpos += imgwidth;
            }
            ypos += imgheight;
        }
        // Restore original clip to Graphics.
        g.setClip(origclip);
    }
}

Related

  1. ensureImageLoaded(Component owner, Image image)
  2. findConnectedComponents(int[][] image)
  3. generateImageFileFromComponent(Component component, String filename, String Type)
  4. setCustomCursor(Component component, Image cursorImg, String name)
  5. setImage(Image im, Component c)