0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.image.ColorModel;
0N/A
0N/A/**
0N/A * The <code>TexturePaint</code> class provides a way to fill a
0N/A * {@link Shape} with a texture that is specified as
0N/A * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
0N/A * object should be small because the <code>BufferedImage</code> data
0N/A * is copied by the <code>TexturePaint</code> object.
0N/A * At construction time, the texture is anchored to the upper
0N/A * left corner of a {@link Rectangle2D} that is
0N/A * specified in user space. Texture is computed for
0N/A * locations in the device space by conceptually replicating the
0N/A * specified <code>Rectangle2D</code> infinitely in all directions
0N/A * in user space and mapping the <code>BufferedImage</code> to each
0N/A * replicated <code>Rectangle2D</code>.
0N/A * @see Paint
0N/A * @see Graphics2D#setPaint
213N/A * @version 1.48, 06/05/07
0N/A */
0N/A
0N/Apublic class TexturePaint implements Paint {
0N/A
0N/A BufferedImage bufImg;
0N/A double tx;
0N/A double ty;
0N/A double sx;
0N/A double sy;
0N/A
0N/A /**
0N/A * Constructs a <code>TexturePaint</code> object.
0N/A * @param txtr the <code>BufferedImage</code> object with the texture
0N/A * used for painting
0N/A * @param anchor the <code>Rectangle2D</code> in user space used to
0N/A * anchor and replicate the texture
0N/A */
0N/A public TexturePaint(BufferedImage txtr,
0N/A Rectangle2D anchor) {
0N/A this.bufImg = txtr;
0N/A this.tx = anchor.getX();
0N/A this.ty = anchor.getY();
0N/A this.sx = anchor.getWidth() / bufImg.getWidth();
0N/A this.sy = anchor.getHeight() / bufImg.getHeight();
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>BufferedImage</code> texture used to
0N/A * fill the shapes.
0N/A * @return a <code>BufferedImage</code>.
0N/A */
0N/A public BufferedImage getImage() {
0N/A return bufImg;
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of the anchor rectangle which positions and
0N/A * sizes the textured image.
0N/A * @return the <code>Rectangle2D</code> used to anchor and
0N/A * size this <code>TexturePaint</code>.
0N/A */
0N/A public Rectangle2D getAnchorRect() {
0N/A return new Rectangle2D.Double(tx, ty,
0N/A sx * bufImg.getWidth(),
0N/A sy * bufImg.getHeight());
0N/A }
0N/A
0N/A /**
213N/A * Creates and returns a {@link PaintContext} used to
213N/A * generate a tiled image pattern.
213N/A * See the {@link Paint#createContext specification} of the
213N/A * method in the {@link Paint} interface for information
213N/A * on null parameter handling.
213N/A *
213N/A * @param cm the preferred {@link ColorModel} which represents the most convenient
213N/A * format for the caller to receive the pixel data, or {@code null}
213N/A * if there is no preference.
213N/A * @param deviceBounds the device space bounding box
213N/A * of the graphics primitive being rendered.
213N/A * @param userBounds the user space bounding box
213N/A * of the graphics primitive being rendered.
213N/A * @param xform the {@link AffineTransform} from user
213N/A * space into device space.
213N/A * @param hints the set of hints that the context object can use to
213N/A * choose between rendering alternatives.
213N/A * @return the {@code PaintContext} for
213N/A * generating color patterns.
213N/A * @see Paint
0N/A * @see PaintContext
213N/A * @see ColorModel
213N/A * @see Rectangle
213N/A * @see Rectangle2D
213N/A * @see AffineTransform
213N/A * @see RenderingHints
0N/A */
0N/A public PaintContext createContext(ColorModel cm,
0N/A Rectangle deviceBounds,
0N/A Rectangle2D userBounds,
0N/A AffineTransform xform,
0N/A RenderingHints hints) {
0N/A if (xform == null) {
0N/A xform = new AffineTransform();
0N/A } else {
0N/A xform = (AffineTransform) xform.clone();
0N/A }
0N/A xform.translate(tx, ty);
0N/A xform.scale(sx, sy);
0N/A
0N/A return TexturePaintContext.getContext(bufImg, xform, hints,
0N/A deviceBounds);
0N/A }
0N/A
0N/A /**
0N/A * Returns the transparency mode for this <code>TexturePaint</code>.
0N/A * @return the transparency mode for this <code>TexturePaint</code>
0N/A * as an integer value.
0N/A * @see Transparency
0N/A */
0N/A public int getTransparency() {
0N/A return (bufImg.getColorModel()).getTransparency();
0N/A }
0N/A
0N/A}