home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 5.3 KB | 163 lines |
- /*
- * @(#)GradientPaint.java 1.22 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt;
-
- import java.awt.geom.Point2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.geom.AffineTransform;
- import java.awt.image.ColorModel;
- import java.awt.image.BufferedImage;
-
- /**
- * This class provides a way to fill a shape with a linear color gradient
- * pattern.
- * Given two points in user space, P1 with the color C1 and P2 with the
- * color C2, the color
- * on the P1, P2 connecting line is changed from C1 to C2 proportionally to
- * the distance to P1.
- * Any point P not on the extended P1, P2 connecting line has the color of
- * the point P' that is the perpendicular projection of P on the extended
- * P1, P2 connecting line.
- * Points on the extended line outside of the P1, P2 segment can be colored
- * in one of two ways.
- * If the gradient is cyclic, then the points on the extended P1, P2
- * connecting line cycle back and forth through the colors from C1 to C2
- * and back to C1 again continuously at an even rate.
- * If the gradient is acyclic, then points on the P1 side of the segment
- * have the constant color C1 while points on the P2 side have the contsant
- * color C2.
- *
- * @see Paint
- * @see Graphics2D#setPaint
- * @version 10 Feb 1997
- */
-
- public class GradientPaint implements Paint {
- Point2D p1;
- Point2D p2;
- Color color1;
- Color color2;
- boolean cyclic;
-
- /**
- * Constructs a simple acyclic GradientPaint object.
- * @param x1,y1 Coordinates of the 1st point in user space.
- * @param color1 Color at the 1st point.
- * @param x2,y2 Coordinates of the 2nd point in user space.
- * @param color2 Color at the 2nd point.
- */
- public GradientPaint(float x1,
- float y1,
- Color color1,
- float x2,
- float y2,
- Color color2) {
- p1 = new Point2D.Float(x1, y1);
- p2 = new Point2D.Float(x2, y2);
- this.color1 = color1;
- this.color2 = color2;
- }
-
- /**
- * Constructs a simple acyclic GradientPaint object.
- * @param pt1 Coordinates of the 1st point in user space.
- * @param color1 Color at the 1st point.
- * @param pt2 Coordinates of the 2nd point in user space.
- * @param color2 Color at the 2nd point.
- */
- public GradientPaint(Point2D pt1,
- Color color1,
- Point2D pt2,
- Color color2) {
- p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());
- p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());
- this.color1 = color1;
- this.color2 = color2;
- }
-
- /**
- * Constructs either a cyclic or acyclic GradientPaint object
- * depending on the boolean parameter.
- * @param x1,y1 Coordinates of the 1st point in user space.
- * @param color1 Color at the 1st point.
- * @param x2,y2 Coordinates of the 2nd point in user space.
- * @param color2 Color at the 2nd point.
- * @param cyclic true if the gradient pattern should cycle
- * repeatedly between the two colors.
- */
- public GradientPaint(float x1,
- float y1,
- Color color1,
- float x2,
- float y2,
- Color color2,
- boolean cyclic) {
- this (x1, y1, color1, x2, y2, color2);
- this.cyclic = cyclic;
- }
-
- /**
- * Constructs either a cyclic or acyclic GradientPaint object
- * depending on the boolean parameter.
- * @param pt1 Coordinates of the 1st point in user space.
- * @param color1 Color at the 1st point.
- * @param pt2 Coordinates of the 2nd point in user space.
- * @param color2 Color at the 2nd point.
- * @param cyclic true if the gradient pattern should cycle
- * repeatedly between the two colors.
- */
- public GradientPaint(Point2D pt1,
- Color color1,
- Point2D pt2,
- Color color2,
- boolean cyclic) {
- this (pt1, color1, pt2, color2);
- this.cyclic = cyclic;
- }
-
- /**
- * Creates and returns a context used to generate the color pattern.
- * @param cm ColorModel in which the caller wishes to receive the
- * paint data. This is used only as a hint.
- * @param deviceBounds The rectangle describing the bounding box in
- * device space of the graphics primitive being rendered.
- * @param userBounds The rectangle describing the bounding box in
- * user space of the graphics primitive being rendered.
- * @param xform The Transform from user space into device space.
- * @return The PaintContext for generating color patterns.
- * @see PaintContext
- */
- public PaintContext createContext(ColorModel cm,
- Rectangle deviceBounds,
- Rectangle2D userBounds,
- AffineTransform xform) {
-
- Point2D t1 = xform.transform(p1, null);
- Point2D t2 = xform.transform(p2, null);
- return (new GradientPaintContext(t1, t2, color1, color2, cyclic));
- }
-
- /**
- * Return the transparency mode for this GradientPaint.
- * @see Transparency
- */
- public int getTransparency() {
- int a1 = color1.getAlpha();
- int a2 = color2.getAlpha();
- return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
- }
-
- }
-