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 / GradientPaint.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.7 KB  |  233 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)GradientPaint.java    1.27 98/06/24
  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.Point2D;
  18. import java.awt.geom.Rectangle2D;
  19. import java.awt.geom.AffineTransform;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.BufferedImage;
  22.  
  23. /**
  24.  * The <code>GradientPaint</code> class provides a way to fill 
  25.  * a {@link Shape} with a linear color gradient pattern.
  26.  * If {@link Point} P1 with {@link Color} C1 and <code>Point</code> P2 with
  27.  * <code>Color</code> C2 are specified in user space, the
  28.  * <code>Color</code> on the P1, P2 connecting line is proportionally
  29.  * changed from C1 to C2.  Any point P not on the extended P1, P2
  30.  * connecting line has the color of the point P' that is the perpendicular
  31.  * projection of P on the extended P1, P2 connecting line.
  32.  * Points on the extended line outside of the P1, P2 segment can be colored
  33.  * in one of two ways.
  34.  * <ul>
  35.  * <li>
  36.  * If the gradient is cyclic then the points on the extended P1, P2
  37.  * connecting line cycle back and forth between the colors C1 and C2.
  38.  * <li>
  39.  * If the gradient is acyclic then points on the P1 side of the segment
  40.  * have the constant <code>Color</code> C1 while points on the P2 side
  41.  * have the constant <code>Color</code> C2.
  42.  * </ul>
  43.  *
  44.  * @see Paint
  45.  * @see Graphics2D#setPaint
  46.  * @version 10 Feb 1997
  47.  */
  48.  
  49. public class GradientPaint implements Paint {
  50.     Point2D.Float p1;
  51.     Point2D.Float p2;
  52.     Color color1;
  53.     Color color2;
  54.     boolean cyclic;
  55.  
  56.     /**
  57.      * Constructs a simple acyclic <code>GradientPaint</code> object.
  58.      * @param x1, y1 coordinates of the first specified
  59.      * <code>Point</code> in user space
  60.      * @param color1 <code>Color</code> at the first specified 
  61.      * <code>Point</code>
  62.      * @param x2, y2 coordinates of the second specified
  63.      * <code>Point</code> in user space
  64.      * @param color2 <code>Color</code> at the second specified 
  65.      * <code>Point</code>
  66.      */
  67.     public GradientPaint(float x1,
  68.              float y1,
  69.              Color color1,
  70.              float x2,
  71.              float y2,
  72.              Color color2) {
  73.         p1 = new Point2D.Float(x1, y1);
  74.         p2 = new Point2D.Float(x2, y2);
  75.         this.color1 = color1;
  76.         this.color2 = color2;
  77.     }
  78.  
  79.     /**
  80.      * Constructs a simple acyclic <code>GradientPaint</code> object.
  81.      * @param pt1 the first specified <code>Point</code> in user space
  82.      * @param color1 <code>Color</code> at the first specified 
  83.      * <code>Point</code>
  84.      * @param pt2 the second specified <code>Point</code> in user space
  85.      * @param color2 <code>Color</code> at the second specified 
  86.      * <code>Point</code>
  87.      */
  88.     public GradientPaint(Point2D pt1,
  89.              Color color1,
  90.              Point2D pt2,
  91.              Color color2) {
  92.         p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());
  93.         p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());
  94.         this.color1 = color1;
  95.         this.color2 = color2;
  96.     }
  97.  
  98.     /**
  99.      * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  100.      * object depending on the <code>boolean</code> parameter.
  101.      * @param x1, y1 coordinates of the first specified
  102.      * <code>Point</code> in user space
  103.      * @param color1 <code>Color</code> at the first specified 
  104.      * <code>Point</code>
  105.      * @param x2, y2 coordinates of the second specified
  106.      * <code>Point</code> in user space
  107.      * @param color2 <code>Color</code> at the second specified 
  108.      * <code>Point</code>
  109.      * @param cyclic <code>true</code> if the gradient pattern should cycle
  110.      * repeatedly between the two colors; <code>false</code> otherwise
  111.      */
  112.     public GradientPaint(float x1,
  113.              float y1,
  114.              Color color1,
  115.              float x2,
  116.              float y2,
  117.              Color color2,
  118.              boolean cyclic) {
  119.     this (x1, y1, color1, x2, y2, color2);
  120.     this.cyclic = cyclic;
  121.     }
  122.  
  123.     /**
  124.      * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  125.      * object depending on the <code>boolean</code> parameter.
  126.      * @param pt1 the first specified <code>Point</code> 
  127.      * in user space
  128.      * @param color1 <code>Color</code> at the first specified 
  129.      * <code>Point</code>
  130.      * @param pt2 the second specified <code>Point</code> 
  131.      * in user space
  132.      * @param color2 <code>Color</code> at the second specified 
  133.      * <code>Point</code>
  134.      * @param cyclic <code>true</code> if the gradient pattern should cycle
  135.      * repeatedly between the two colors; <code>false</code> otherwise
  136.      */
  137.     public GradientPaint(Point2D pt1,
  138.              Color color1,
  139.              Point2D pt2,
  140.              Color color2,
  141.              boolean cyclic) {
  142.     this (pt1, color1, pt2, color2);
  143.     this.cyclic = cyclic;
  144.     }
  145.  
  146.     /**
  147.      * Returns a copy of the point P1 that anchors the first color.
  148.      * @return a {@link Point2D} object that is a copy of the point
  149.      * that anchors the first color of this 
  150.      * <code>GradientPaint</code>.  
  151.      */
  152.     public Point2D getPoint1() {
  153.     return new Point2D.Float(p1.x, p1.y);
  154.     }
  155.  
  156.     /**
  157.      * Returns the color C1 anchored by the point P1.
  158.      * @return a <code>Color</code> object that is the color
  159.      * anchored by P1.
  160.      */
  161.     public Color getColor1() {
  162.     return color1;
  163.     }
  164.  
  165.     /**
  166.      * Returns a copy of the point P2 which anchors the second color.
  167.      * @return a {@link Point2D} object that is a copy of the point
  168.      * that anchors the second color of this
  169.      * <code>GradientPaint</code>.
  170.      */
  171.     public Point2D getPoint2() {
  172.     return new Point2D.Float(p2.x, p2.y);
  173.     }
  174.  
  175.     /**
  176.      * Returns the color C2 anchored by the point P2.
  177.      * @return a <code>Color</code> object that is the color
  178.      * anchored by P2.
  179.      */
  180.     public Color getColor2() {
  181.     return color2;
  182.     }
  183.  
  184.     /**
  185.      * Returns <code>true</code> if the gradient cycles repeatedly
  186.      * between the two colors C1 and C2.
  187.      * @return <code>true</code> if the gradient cycles repeatedly
  188.      * between the two colors; <code>false</code> otherwise.
  189.      */
  190.     public boolean isCyclic() {
  191.     return cyclic;
  192.     }
  193.  
  194.     /**
  195.      * Creates and returns a context used to generate the color pattern.
  196.      * @param cm {@link ColorModel} that receives
  197.      * the <code>Paint</code> data. This is used only as a hint.
  198.      * @param deviceBounds the device space bounding box of the 
  199.      * graphics primitive being rendered
  200.      * @param userBounds the user space bounding box of the 
  201.      * graphics primitive being rendered
  202.      * @param xform the {@link AffineTransform} from user
  203.      *     space into device space
  204.      * @param hints the hints that the context object uses to choose
  205.      * between rendering alternatives
  206.      * @return the {@link PaintContext} that generates color patterns.
  207.      * @see PaintContext
  208.      */
  209.     public PaintContext createContext(ColorModel cm,
  210.                       Rectangle deviceBounds,
  211.                       Rectangle2D userBounds,
  212.                       AffineTransform xform,
  213.                                       RenderingHints hints) {
  214.  
  215.         Point2D t1 = xform.transform(p1, null);
  216.         Point2D t2 = xform.transform(p2, null);
  217.         return (new GradientPaintContext(t1, t2, color1, color2, cyclic));
  218.     }
  219.  
  220.     /**
  221.      * Returns the transparency mode for this <code>GradientPaint</code>.
  222.      * @return an integer value representing this <code>GradientPaint</code>
  223.      * object's transparency mode.
  224.      * @see Transparency
  225.      */
  226.     public int getTransparency() {
  227.     int a1 = color1.getAlpha();
  228.     int a2 = color2.getAlpha();
  229.     return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
  230.     }
  231.  
  232. }
  233.