summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/GradientPaint.java
blob: 0e06528f988475557618bf440182d9affdf26ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.awt;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 * The GradientPaint class defines a way to fill a Shape with a linear color
 * gradient pattern. 
 * <p>
 * The GradientPaint's fill pattern is determined by two points and two colors, 
 * plus the cyclic mode option.
 * Each of the two points is painted with its corresponding color, and on 
 * the line segment connecting the two points, the color is proportionally
 * changed between the two colors. For points on the same line which are not 
 * between the two specified points (outside of the connecting segment) their
 * color is determined by the cyclic mode option. If the mode is cyclic, then
 * the rest of the line repeats the color pattern of the connecting segment, 
 * cycling back and forth between the two colors. If not, the mode is acyclic 
 * which means that all points 
 * on the line outside the connecting line segment are given the same 
 * color as the closest of the two specified points.
 * <p>
 * The color of points that are not on the line connecting the two 
 * specified points are given by perpendicular projection: by taking 
 * the set of lines perpendicular to the connecting line and for each 
 * one, the whole line is colored with the same color.
 */
public class GradientPaint implements Paint {
    
    /** The start point color. */
    Color color1;

    /** The end color point. */
    Color color2;

    /** The location of the start point. */
    Point2D point1;

    /** The location of the end point. */
    Point2D point2;

    /** The indicator of cycle filling. If TRUE filling 
     * repeated outside points stripe, if FALSE solid color filling outside. */
    boolean cyclic;

    /**
     * Instantiates a new GradientPaint with cyclic or acyclic mode.
     * 
     * @param point1 the first specified point.
     * @param color1 the Color of the first specified point. 
     * @param point2 the second specified point.
     * @param color2 the Color of the second specified point.
     * @param cyclic the cyclic mode - true if the gradient pattern should cycle 
     * repeatedly between the two colors; false otherwise.
     */
    public GradientPaint(Point2D point1, Color color1, Point2D point2,
            Color color2, boolean cyclic) {
        if (point1 == null || point2 == null) {
            // awt.6D=Point is null
            throw new NullPointerException(Messages.getString("awt.6D")); //$NON-NLS-1$
        }
        if (color1 == null || color2 == null) {
            // awt.6E=Color is null
            throw new NullPointerException(Messages.getString("awt.6E")); //$NON-NLS-1$
        }

        this.point1 = point1;
        this.point2 = point2;
        this.color1 = color1;
        this.color2 = color2;
        this.cyclic = cyclic;
    }

    /**
     * Instantiates a new GradientPaint with cyclic or acyclic mode;
     * points are specified by coordinates.
     * 
     * @param x1 the X coordinate of the first point.
     * @param y1 the Y coordinate of the first point.
     * @param color1 the color of the first point.
     * @param x2 the X coordinate of the second point.
     * @param y2 the Y coordinate of the second point.
     * @param color2 the color of the second point.
     * @param cyclic the cyclic mode - true if the gradient pattern should cycle 
     * repeatedly between the two colors; false otherwise.
     */
    public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2,
            boolean cyclic) {
        this(new Point2D.Float(x1, y1), color1, new Point2D.Float(x2, y2), color2, cyclic);
    }

    /**
     * Instantiates a new acyclic GradientPaint;
     * points are specified by coordinates.
     * 
     * @param x1 the X coordinate of the first point.
     * @param y1 the Y coordinate of the first point.
     * @param color1 the color of the first point.
     * @param x2 the X coordinate of the second point.
     * @param y2 the Y coordinate of the second point.
     * @param color2 the color of the second point.
     */
    public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2) {
        this(x1, y1, color1, x2, y2, color2, false);
    }

    /**
     * Instantiates a new acyclic GradientPaint.
     * 
     * @param point1 the first specified point.
     * @param color1 the Color of the first specified point. 
     * @param point2 the second specified point.
     * @param color2 the Color of the second specified point.
     */
    public GradientPaint(Point2D point1, Color color1, Point2D point2, Color color2) {
        this(point1, color1, point2, color2, false);
    }

    /**
     * Creates PaintContext for a color pattern generating. 
     * 
     * @param cm the ColorModel of the Paint data.
     * @param deviceBounds the bounding Rectangle of graphics primitives
     * being rendered in the device space. 
     * @param userBounds tthe bounding Rectangle of graphics primitives
     * being rendered in the user space. 
     * @param t the AffineTransform from user space into device space.
     * @param hints the RrenderingHints object.
     * 
     * @return the PaintContext for color pattern generating.
     * 
     * @see java.awt.Paint#createContext(java.awt.image.ColorModel, java.awt.Rectangle, java.awt.geom.Rectangle2D, java.awt.geom.AffineTransform, java.awt.RenderingHints)
     */
    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
            Rectangle2D userBounds, AffineTransform t, RenderingHints hints) {
        return new GradientPaintContext(cm, t, point1, color1, point2, color2, cyclic);
    }

    /**
     * Gets the color of the first point.
     * 
     * @return the color of the first point.
     */
    public Color getColor1() {
        return color1;
    }

    /**
     * Gets the color of the second point.
     * 
     * @return the color of the second point.
     */
    public Color getColor2() {
        return color2;
    }

    /**
     * Gets the first point.
     * 
     * @return the Point object - the first point. 
     */
    public Point2D getPoint1() {
        return point1;
    }

    /**
     * Gets the second point.
     * 
     * @return the Point object - the second point.
     */
    public Point2D getPoint2() {
        return point2;
    }

    /**
     * Gets the transparency mode for the GradientPaint.
     * 
     * @return the transparency mode for the GradientPaint.
     * 
     * @see java.awt.Transparency#getTransparency()
     */
    public int getTransparency() {
        int a1 = color1.getAlpha();
        int a2 = color2.getAlpha();
        return (a1 == 0xFF && a2 == 0xFF) ? OPAQUE : TRANSLUCENT;
    }

    /**
     * Returns the GradientPaint mode: true for cyclic mode, false for
     * acyclic mode.
     * 
     * @return true if the gradient cycles repeatedly between the two colors; 
     * false otherwise.
     */
    public boolean isCyclic() {
        return cyclic;
    }
}