home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / TexturePaint.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.2 KB  |  131 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)TexturePaint.java    1.30 98/08/13
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.geom.Rectangle2D;
  18. import java.awt.geom.AffineTransform;
  19. import java.awt.image.AffineTransformOp;
  20. import java.awt.image.BufferedImage;
  21. import java.awt.image.ColorModel;
  22.  
  23. /**
  24.  * The <code>TexturePaint</code> class provides a way to fill a
  25.  * {@link Shape} with a texture that is specified as
  26.  * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
  27.  * object should be small because the <code>BufferedImage</code> data
  28.  * is copied by the <code>TexturePaint</code> object.
  29.  * At construction time, the texture is anchored to the upper
  30.  * left corner of a {@link Rectangle2D} that is
  31.  * specified in user space.  Texture is computed for
  32.  * locations in the device space by conceptually replicating the   
  33.  * specified <code>Rectangle2D</code> infinitely in all directions
  34.  * in user space and mapping the <code>BufferedImage</code> to each
  35.  * replicated <code>Rectangle2D</code>.
  36.  * @see Paint
  37.  * @see Graphics2D#setPaint
  38.  * @version 10 Feb 1997
  39.  */
  40.  
  41. public class TexturePaint implements Paint {
  42.  
  43.     BufferedImage bufImg;
  44.     double tx;
  45.     double ty;
  46.     double sx;
  47.     double sy;
  48.  
  49.     /**
  50.      * Constructs a <code>TexturePaint</code> object.
  51.      * @param txtr the <code>BufferedImage</code> object with the texture
  52.      * used for painting
  53.      * @param anchor the <code>Rectangle2D</code> in user space used to
  54.      * anchor and replicate the texture
  55.      */
  56.     public TexturePaint(BufferedImage txtr,
  57.             Rectangle2D anchor) {
  58.         this.bufImg = txtr;
  59.     this.tx = anchor.getX();
  60.     this.ty = anchor.getY();
  61.     this.sx = anchor.getWidth() / bufImg.getWidth();
  62.     this.sy = anchor.getHeight() / bufImg.getHeight();
  63.     }
  64.  
  65.     /**
  66.      * Returns the <code>BufferedImage</code> texture used to 
  67.      * fill the shapes.
  68.      * @return a <code>BufferedImage</code>.
  69.      */
  70.     public BufferedImage getImage() {
  71.     return bufImg;
  72.     }
  73.  
  74.     /**
  75.      * Returns a copy of the anchor rectangle which positions and
  76.      * sizes the textured image.
  77.      * @return the <code>Rectangle2D</code> used to anchor and
  78.      * size this <code>TexturePaint</code>.
  79.      */
  80.     public Rectangle2D getAnchorRect() {
  81.     return new Rectangle2D.Double(tx, ty,
  82.                       sx * bufImg.getWidth(),
  83.                       sy * bufImg.getHeight());
  84.     }
  85.  
  86.     /**
  87.      * Creates and returns a context used to generate the color pattern.
  88.      * @param cm the {@link ColorModel} that receives the
  89.      * <code>Paint</code> data. This is used only as a hint.
  90.      * @param deviceBounds the device space bounding box of the graphics
  91.      * primitive being rendered
  92.      * @param userBounds the user space bounding box of the graphics
  93.      * primitive being rendered
  94.      * @param xform the {@link AffineTransform} from user space
  95.      *          into device space
  96.      * @param hints a {@link RenderingHints} object that can be used to
  97.      *          specify how the pattern is ultimately rendered
  98.      * @return the {@link PaintContext} used for generating color
  99.      *          patterns.
  100.      * @see PaintContext
  101.      */
  102.     public PaintContext createContext(ColorModel cm,
  103.                       Rectangle deviceBounds,
  104.                       Rectangle2D userBounds,
  105.                       AffineTransform xform,
  106.                                       RenderingHints hints) {
  107.     if (xform == null) {
  108.         xform = new AffineTransform();
  109.     } else {
  110.         xform = (AffineTransform) xform.clone();
  111.     }
  112.     xform.translate(tx, ty);
  113.     xform.scale(sx, sy);
  114.  
  115.     return TexturePaintContext.getContext(bufImg, xform, hints,
  116.                           deviceBounds);
  117.     }
  118.  
  119.     /**
  120.      * Returns the transparency mode for this <code>TexturePaint</code>.
  121.      * @return the transparency mode for this <code>TexturePaint</code>
  122.      * as an integer value. 
  123.      * @see Transparency
  124.      */
  125.     public int getTransparency() {
  126.         return (bufImg.getColorModel()).getTransparency();
  127.     }
  128.  
  129. }
  130.  
  131.