1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/* * @(#)TexturePaint.java 1.40 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. 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 <code>TexturePaint</code> class provides a way to fill a * {@link Shape} with a texture that is specified as * a {@link BufferedImage}. The size of the <code>BufferedImage</code> * object should be small because the <code>BufferedImage</code> data * is copied by the <code>TexturePaint</code> 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 <code>Rectangle2D</code> infinitely in all directions * in user space and mapping the <code>BufferedImage</code> to each * replicated <code>Rectangle2D</code>. * @see Paint * @see Graphics2D#setPaint * @version 1.40, 11/17/05 */ public class TexturePaint implements Paint { BufferedImage bufImg; double tx; double ty; double sx; double sy; /** * Constructs a <code>TexturePaint</code> object. * @param txtr the <code>BufferedImage</code> object with the texture * used for painting * @param anchor the <code>Rectangle2D</code> 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 <code>BufferedImage</code> texture used to * fill the shapes. * @return a <code>BufferedImage</code>. */ public BufferedImage getImage() { return bufImg; } /** * Returns a copy of the anchor rectangle which positions and * sizes the textured image. * @return the <code>Rectangle2D</code> used to anchor and * size this <code>TexturePaint</code>. */ 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 * <code>Paint</code> 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 <code>TexturePaint</code>. * @return the transparency mode for this <code>TexturePaint</code> * as an integer value. * @see Transparency */ public int getTransparency() { return (bufImg.getColorModel()).getTransparency(); } }