home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / TexturePaint.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  150 lines

  1. /*
  2.  * @(#)TexturePaint.java    1.25 98/03/18
  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.BufferedImage;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.NearestNeighborAffineTransformOp;
  22. import java.awt.image.BilinearAffineTransformOp;
  23.  
  24. /**
  25.  * This class provides a way to fill a shape with a given texture.
  26.  * The texture used in the filling process is given as a BufferedImage.
  27.  * The size of the texture BufferedImage object should be small because
  28.  * the BufferedImage data is copied by the TexturePaint object.
  29.  * The texture is anchored at construction time to the upper left corner
  30.  * of a Rectangle2D specified in user space.  Texture is computed for
  31.  * locations in the device space by conceptually replicating the given
  32.  * Rectangle2D infinitely in all directions in user space and mapping
  33.  * the BufferedImage to each replicated Rectangle2D.
  34.  * @see Paint
  35.  * @see Graphics2D#setPaint
  36.  * @version 10 Feb 1997
  37.  */
  38.  
  39. public class TexturePaint implements Paint {
  40.  
  41.     /**
  42.      * Texture colors are generated with the nearest neighbor algorithm.
  43.      */
  44.     public static final int NEAREST_NEIGHBOR    =    0;
  45.  
  46.     /**
  47.      * Texture colors are generated with the bilinear algorithm.
  48.      */
  49.     public static final int BILINEAR        =    1;
  50.  
  51.     int transparencyMode;
  52.     int interpolation;
  53.     BufferedImage bufImg;
  54.     Rectangle2D anchor;
  55.  
  56.     /**
  57.      * Constructs a TexturePaint object.
  58.      * @param txtr The BufferedImage object with the texture used for painting.
  59.      * @param rect2d The rectangle in user space used to anchor and replicate
  60.      * the texture.
  61.      * @param interpolation The type of algorithm with regard to rendering
  62.      * quality used to generate the color pattern. Possible values are
  63.      * NEAREST_NEIGHBOR and BILINEAR 
  64.      * @see #NEAREST_NEIGHBOR
  65.      * @see #BILINEAR
  66.      */
  67.     public TexturePaint(BufferedImage txtr,
  68.             Rectangle2D rect2d,
  69.             int interpolation) {
  70.     if (interpolation != NEAREST_NEIGHBOR &&
  71.         interpolation != BILINEAR)
  72.             {
  73.                 throw new IllegalArgumentException("interpolation must be "+
  74.                                                    "NEAREST_NEIGHBOR or "+
  75.                                                    "BILINEAR");
  76.             }
  77.         this.interpolation = interpolation;
  78.         this.bufImg = txtr;
  79.         this.anchor = rect2d;
  80.     }
  81.  
  82.     /**
  83.      * Returns the interpolation type given when the object is constructed.
  84.      */
  85.     public int getInterpolation() {
  86.         return this.interpolation;
  87.     }
  88.  
  89.     /**
  90.      * Creates and returns a context used to generate the color pattern.
  91.      * @param cm ColorModel in which the caller wishes to receive the
  92.      * paint data. This is used only as a hint.
  93.      * @param deviceBounds The rectangle describing the bounding box
  94.      * in device space of the graphics primitive being rendered.
  95.      * @param userBounds The rectangle describing the bounding box in
  96.      * user space of the graphics primitive being rendered.
  97.      * @param xform The Transform from user space into device space.
  98.      * @return The PaintContext for generating color patterns.
  99.      * @see PaintContext
  100.      */
  101.     public PaintContext createContext(ColorModel cm,
  102.                       Rectangle deviceBounds,
  103.                       Rectangle2D userBounds,
  104.                       AffineTransform xform) {
  105.         
  106.         Rectangle txAnchor = anchor.getBounds();
  107.         
  108.         if (xform != null) {
  109.             double mat[] = new double[6];
  110.             xform.getMatrix(mat);
  111.             // Remove translation (not needed)
  112.             xform.translate(-mat[4], -mat[5]);
  113.             if (!xform.isIdentity()) {
  114.                 switch (interpolation) {
  115.                 case NEAREST_NEIGHBOR:
  116.                     bufImg = (new NearestNeighborAffineTransformOp
  117.                               (xform)).filter(bufImg, null);
  118.                     break;
  119.                 case BILINEAR:
  120.                     bufImg = (new BilinearAffineTransformOp
  121.                               (xform)).filter(bufImg, null);
  122.                     break;
  123.                 }
  124.                 // Transform the anchor Rectangle
  125.                 Shape s = xform.createTransformedShape(anchor);
  126.                 txAnchor = s.getBounds();
  127.             }
  128.         }
  129.  
  130.         // Crop the image to the size of the anchor rectangle
  131.         int cw = Math.min(txAnchor.width, bufImg.getWidth());
  132.         int ch = Math.min(txAnchor.height, bufImg.getHeight());
  133.         if (cw < bufImg.getWidth() || ch < bufImg.getHeight()) {
  134.             bufImg = bufImg.getSubimage(0, 0, cw, ch);
  135.         }
  136.  
  137.     return (new TexturePaintContext(bufImg, txAnchor.x, txAnchor.y));
  138.     }
  139.  
  140.     /**
  141.      * Return the transparency mode for this TexturePaint.
  142.      * @see Transparency
  143.      */
  144.     public int getTransparency() {
  145.         return (bufImg.getColorModel()).getTransparency();
  146.     }
  147.  
  148. }
  149.  
  150.