Here you can find the source of tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height)
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. |
public static void tileImage(Image img, Component comp, Graphics g, int x, int y, int width, int height)
//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); } }