home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 5.2 KB | 150 lines |
- /*
- * @(#)TexturePaint.java 1.25 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt;
-
- import java.awt.geom.Rectangle2D;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.NearestNeighborAffineTransformOp;
- import java.awt.image.BilinearAffineTransformOp;
-
- /**
- * This class provides a way to fill a shape with a given texture.
- * The texture used in the filling process is given as a BufferedImage.
- * The size of the texture BufferedImage object should be small because
- * the BufferedImage data is copied by the TexturePaint object.
- * The texture is anchored at construction time to the upper left corner
- * of a Rectangle2D specified in user space. Texture is computed for
- * locations in the device space by conceptually replicating the given
- * Rectangle2D infinitely in all directions in user space and mapping
- * the BufferedImage to each replicated Rectangle2D.
- * @see Paint
- * @see Graphics2D#setPaint
- * @version 10 Feb 1997
- */
-
- public class TexturePaint implements Paint {
-
- /**
- * Texture colors are generated with the nearest neighbor algorithm.
- */
- public static final int NEAREST_NEIGHBOR = 0;
-
- /**
- * Texture colors are generated with the bilinear algorithm.
- */
- public static final int BILINEAR = 1;
-
- int transparencyMode;
- int interpolation;
- BufferedImage bufImg;
- Rectangle2D anchor;
-
- /**
- * Constructs a TexturePaint object.
- * @param txtr The BufferedImage object with the texture used for painting.
- * @param rect2d The rectangle in user space used to anchor and replicate
- * the texture.
- * @param interpolation The type of algorithm with regard to rendering
- * quality used to generate the color pattern. Possible values are
- * NEAREST_NEIGHBOR and BILINEAR
- * @see #NEAREST_NEIGHBOR
- * @see #BILINEAR
- */
- public TexturePaint(BufferedImage txtr,
- Rectangle2D rect2d,
- int interpolation) {
- if (interpolation != NEAREST_NEIGHBOR &&
- interpolation != BILINEAR)
- {
- throw new IllegalArgumentException("interpolation must be "+
- "NEAREST_NEIGHBOR or "+
- "BILINEAR");
- }
- this.interpolation = interpolation;
- this.bufImg = txtr;
- this.anchor = rect2d;
- }
-
- /**
- * Returns the interpolation type given when the object is constructed.
- */
- public int getInterpolation() {
- return this.interpolation;
- }
-
- /**
- * Creates and returns a context used to generate the color pattern.
- * @param cm ColorModel in which the caller wishes to receive the
- * paint data. This is used only as a hint.
- * @param deviceBounds The rectangle describing the bounding box
- * in device space of the graphics primitive being rendered.
- * @param userBounds The rectangle describing the bounding box in
- * user space of the graphics primitive being rendered.
- * @param xform The Transform from user space into device space.
- * @return The PaintContext for generating color patterns.
- * @see PaintContext
- */
- public PaintContext createContext(ColorModel cm,
- Rectangle deviceBounds,
- Rectangle2D userBounds,
- AffineTransform xform) {
-
- Rectangle txAnchor = anchor.getBounds();
-
- if (xform != null) {
- double mat[] = new double[6];
- xform.getMatrix(mat);
- // Remove translation (not needed)
- xform.translate(-mat[4], -mat[5]);
- if (!xform.isIdentity()) {
- switch (interpolation) {
- case NEAREST_NEIGHBOR:
- bufImg = (new NearestNeighborAffineTransformOp
- (xform)).filter(bufImg, null);
- break;
- case BILINEAR:
- bufImg = (new BilinearAffineTransformOp
- (xform)).filter(bufImg, null);
- break;
- }
- // Transform the anchor Rectangle
- Shape s = xform.createTransformedShape(anchor);
- txAnchor = s.getBounds();
- }
- }
-
- // Crop the image to the size of the anchor rectangle
- int cw = Math.min(txAnchor.width, bufImg.getWidth());
- int ch = Math.min(txAnchor.height, bufImg.getHeight());
- if (cw < bufImg.getWidth() || ch < bufImg.getHeight()) {
- bufImg = bufImg.getSubimage(0, 0, cw, ch);
- }
-
- return (new TexturePaintContext(bufImg, txAnchor.x, txAnchor.y));
- }
-
- /**
- * Return the transparency mode for this TexturePaint.
- * @see Transparency
- */
- public int getTransparency() {
- return (bufImg.getColorModel()).getTransparency();
- }
-
- }
-
-