<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * @(#)TexturePaint.java	1.36 00/02/02
 *
 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */

package java.awt;

import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

/**
 * The &lt;code&gt;TexturePaint&lt;/code&gt; class provides a way to fill a
 * {@link Shape} with a texture that is specified as
 * a {@link BufferedImage}. The size of the &lt;code&gt;BufferedImage&lt;/code&gt;
 * object should be small because the &lt;code&gt;BufferedImage&lt;/code&gt; data
 * is copied by the &lt;code&gt;TexturePaint&lt;/code&gt; object.
 * At construction time, the texture is anchored to the upper
 * left corner of a {@link Rectangle2D} that is
 * specified in user space.  Texture is computed for
 * locations in the device space by conceptually replicating the   
 * specified &lt;code&gt;Rectangle2D&lt;/code&gt; infinitely in all directions
 * in user space and mapping the &lt;code&gt;BufferedImage&lt;/code&gt; to each
 * replicated &lt;code&gt;Rectangle2D&lt;/code&gt;.
 * @see Paint
 * @see Graphics2D#setPaint
 * @version 1.36, 02/02/00
 */

public class TexturePaint implements Paint {

    BufferedImage bufImg;
    double tx;
    double ty;
    double sx;
    double sy;

    /**
     * Constructs a &lt;code&gt;TexturePaint&lt;/code&gt; object.
     * @param txtr the &lt;code&gt;BufferedImage&lt;/code&gt; object with the texture
     * used for painting
     * @param anchor the &lt;code&gt;Rectangle2D&lt;/code&gt; in user space used to
     * anchor and replicate the texture
     */
    public TexturePaint(BufferedImage txtr,
			Rectangle2D anchor) {
        this.bufImg = txtr;
	this.tx = anchor.getX();
	this.ty = anchor.getY();
	this.sx = anchor.getWidth() / bufImg.getWidth();
	this.sy = anchor.getHeight() / bufImg.getHeight();
    }

    /**
     * Returns the &lt;code&gt;BufferedImage&lt;/code&gt; texture used to 
     * fill the shapes.
     * @return a &lt;code&gt;BufferedImage&lt;/code&gt;.
     */
    public BufferedImage getImage() {
	return bufImg;
    }

    /**
     * Returns a copy of the anchor rectangle which positions and
     * sizes the textured image.
     * @return the &lt;code&gt;Rectangle2D&lt;/code&gt; used to anchor and
     * size this &lt;code&gt;TexturePaint&lt;/code&gt;.
     */
    public Rectangle2D getAnchorRect() {
	return new Rectangle2D.Double(tx, ty,
				      sx * bufImg.getWidth(),
				      sy * bufImg.getHeight());
    }

    /**
     * Creates and returns a context used to generate the color pattern.
     * @param cm the {@link ColorModel} that receives the
     * &lt;code&gt;Paint&lt;/code&gt; data. This is used only as a hint.
     * @param deviceBounds the device space bounding box of the graphics
     * primitive being rendered
     * @param userBounds the user space bounding box of the graphics
     * primitive being rendered
     * @param xform the {@link AffineTransform} from user space
     *          into device space
     * @param hints a {@link RenderingHints} object that can be used to
     *          specify how the pattern is ultimately rendered
     * @return the {@link PaintContext} used for generating color
     *          patterns.
     * @see PaintContext
     */
    public PaintContext createContext(ColorModel cm,
				      Rectangle deviceBounds,
				      Rectangle2D userBounds,
				      AffineTransform xform,
                                      RenderingHints hints) {
	if (xform == null) {
	    xform = new AffineTransform();
	} else {
	    xform = (AffineTransform) xform.clone();
	}
	xform.translate(tx, ty);
	xform.scale(sx, sy);

	return TexturePaintContext.getContext(bufImg, xform, hints,
					      deviceBounds);
    }

    /**
     * Returns the transparency mode for this &lt;code&gt;TexturePaint&lt;/code&gt;.
     * @return the transparency mode for this &lt;code&gt;TexturePaint&lt;/code&gt;
     * as an integer value. 
     * @see Transparency
     */
    public int getTransparency() {
        return (bufImg.getColorModel()).getTransparency();
    }

}

</pre></body></html>