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

  1. /*
  2.  * @(#)GradientPaint.java    1.22 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.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.  * This class provides a way to fill a shape with a linear color gradient
  25.  * pattern.
  26.  * Given two points in user space, P1 with the color C1 and P2 with the
  27.  * color C2, the color
  28.  * on the P1, P2 connecting line is changed from C1 to C2 proportionally to
  29.  * the distance to P1.
  30.  * Any point P not on the extended P1, P2 connecting line has the color of
  31.  * the point P' that is the perpendicular projection of P on the extended
  32.  * P1, P2 connecting line.
  33.  * Points on the extended line outside of the P1, P2 segment can be colored
  34.  * in one of two ways.
  35.  * If the gradient is cyclic, then the points on the extended P1, P2
  36.  * connecting line cycle back and forth through the colors from C1 to C2
  37.  * and back to C1 again continuously at an even rate.
  38.  * If the gradient is acyclic, then points on the P1 side of the segment
  39.  * have the constant color C1 while points on the P2 side have the contsant
  40.  * color C2.
  41.  *
  42.  * @see Paint
  43.  * @see Graphics2D#setPaint
  44.  * @version 10 Feb 1997
  45.  */
  46.  
  47. public class GradientPaint implements Paint {
  48.     Point2D p1;
  49.     Point2D p2;
  50.     Color color1;
  51.     Color color2;
  52.     boolean cyclic;
  53.  
  54.     /**
  55.      * Constructs a simple acyclic GradientPaint object.
  56.      * @param x1,y1 Coordinates of the 1st point in user space.
  57.      * @param color1 Color at the 1st point.
  58.      * @param x2,y2 Coordinates of the 2nd point in user space.
  59.      * @param color2 Color at the 2nd point.
  60.      */
  61.     public GradientPaint(float x1,
  62.              float y1,
  63.              Color color1,
  64.              float x2,
  65.              float y2,
  66.              Color color2) {
  67.         p1 = new Point2D.Float(x1, y1);
  68.         p2 = new Point2D.Float(x2, y2);
  69.         this.color1 = color1;
  70.         this.color2 = color2;
  71.     }
  72.  
  73.     /**
  74.      * Constructs a simple acyclic GradientPaint object.
  75.      * @param pt1 Coordinates of the 1st point in user space.
  76.      * @param color1 Color at the 1st point.
  77.      * @param pt2 Coordinates of the 2nd point in user space.
  78.      * @param color2 Color at the 2nd point.
  79.      */
  80.     public GradientPaint(Point2D pt1,
  81.              Color color1,
  82.              Point2D pt2,
  83.              Color color2) {
  84.         p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());
  85.         p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());
  86.         this.color1 = color1;
  87.         this.color2 = color2;
  88.     }
  89.  
  90.     /**
  91.      * Constructs either a cyclic or acyclic GradientPaint object
  92.      * depending on the boolean parameter.
  93.      * @param x1,y1 Coordinates of the 1st point in user space.
  94.      * @param color1 Color at the 1st point.
  95.      * @param x2,y2 Coordinates of the 2nd point in user space.
  96.      * @param color2 Color at the 2nd point.
  97.      * @param cyclic true if the gradient pattern should cycle
  98.      * repeatedly between the two colors.
  99.      */
  100.     public GradientPaint(float x1,
  101.              float y1,
  102.              Color color1,
  103.              float x2,
  104.              float y2,
  105.              Color color2,
  106.              boolean cyclic) {
  107.     this (x1, y1, color1, x2, y2, color2);
  108.     this.cyclic = cyclic;
  109.     }
  110.  
  111.     /**
  112.      * Constructs either a cyclic or acyclic GradientPaint object
  113.      * depending on the boolean parameter.
  114.      * @param pt1 Coordinates of the 1st point in user space.
  115.      * @param color1 Color at the 1st point.
  116.      * @param pt2 Coordinates of the 2nd point in user space.
  117.      * @param color2 Color at the 2nd point.
  118.      * @param cyclic true if the gradient pattern should cycle
  119.      * repeatedly between the two colors.
  120.      */
  121.     public GradientPaint(Point2D pt1,
  122.              Color color1,
  123.              Point2D pt2,
  124.              Color color2,
  125.              boolean cyclic) {
  126.     this (pt1, color1, pt2, color2);
  127.     this.cyclic = cyclic;
  128.     }
  129.  
  130.     /**
  131.      * Creates and returns a context used to generate the color pattern.
  132.      * @param cm ColorModel in which the caller wishes to receive the
  133.      * paint data. This is used only as a hint.
  134.      * @param deviceBounds The rectangle describing the bounding box in
  135.      * device space of the graphics primitive being rendered.
  136.      * @param userBounds The rectangle describing the bounding box in
  137.      * user space of the graphics primitive being rendered.
  138.      * @param xform The Transform from user space into device space.
  139.      * @return The PaintContext for generating color patterns.
  140.      * @see PaintContext
  141.      */
  142.     public PaintContext createContext(ColorModel cm,
  143.                       Rectangle deviceBounds,
  144.                       Rectangle2D userBounds,
  145.                       AffineTransform xform) {
  146.  
  147.         Point2D t1 = xform.transform(p1, null);
  148.         Point2D t2 = xform.transform(p2, null);
  149.         return (new GradientPaintContext(t1, t2, color1, color2, cyclic));
  150.     }
  151.  
  152.     /**
  153.      * Return the transparency mode for this GradientPaint.
  154.      * @see Transparency
  155.      */
  156.     public int getTransparency() {
  157.     int a1 = color1.getAlpha();
  158.     int a2 = color2.getAlpha();
  159.     return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
  160.     }
  161.  
  162. }
  163.