001: /*
002: * $Id: BasicGradientPainter.java,v 1.1 2006/03/22 19:05:44 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.painter.gradient;
023:
024: import java.awt.Color;
025: import java.awt.GradientPaint;
026: import java.awt.Paint;
027: import java.awt.PaintContext;
028: import java.awt.Rectangle;
029: import java.awt.RenderingHints;
030: import java.awt.color.ColorSpace;
031: import java.awt.geom.AffineTransform;
032: import java.awt.geom.Point2D;
033: import java.awt.geom.Rectangle2D;
034: import java.awt.image.ColorModel;
035: import javax.swing.UIManager;
036:
037: /**
038: * <p>A Gradient based Painter that uses GradientPaint to paint the gradient.
039: * Simply specify the GradientPaint to use.</p>
040: *
041: * <p>In order for resizing to work properly with GradientPaint
042: * it is necessary that the various control points used in
043: * these paints be specified in such a manner that they can be reliably resized.
044: * For example, BasicGradientPainter takes GradientPaints who's point1 and point2
045: * properties are specified between 0 and 1, representing at what percentage of
046: * the distance from the origin the gradient begins and ends. Thus, if I created
047: * a GradientPaint like this:
048: * <pre><code>
049: * GradientPaint gp = new GradientPaint(
050: * new Point2D.Double(.2d, 0),
051: * Color.BLUE,
052: * new Point2D.Double(.8d, 0),
053: * Color.WHITE);
054: * </code></pre>
055: *
056: * then when painted, the gradient will start with a BLUE at 20% of the width of
057: * the component, and finish with WHITE at 80% of the width of the component.</p>
058: *
059: * <p>Various built in gradients also exist as public static final properties.
060: * They are defined as GradientPaints rather than BasicGradientPainters because
061: * BasicGradientPainter is mutable and thus don't make very reliable public static
062: * final defaults. To use:
063: * <pre><code>
064: * panel.setBackgroundPainter(new BasicGradientPainter(BasicGradientPainter.BLUE_EXPERIENCE));
065: * </code></pre></p>
066: *
067: * @author rbair
068: */
069: public class BasicGradientPainter extends AbstractGradientPainter {
070: // public static final GradientPaint WHITE_TO_CONTROL_HORZONTAL = new GradientPaint(
071: // new Point2D.Double(0, 0),
072: // Color.WHITE,
073: // new Point2D.Double(1, 0),
074: // new UIColor("control"));
075: // public static final GradientPaint WHITE_TO_CONTROL_VERTICAL = new GradientPaint(
076: // new Point2D.Double(0, 0),
077: // Color.WHITE,
078: // new Point2D.Double(0, 1),
079: // new UIColor("control"));
080: public static final GradientPaint BLUE_EXPERIENCE = new GradientPaint(
081: new Point2D.Double(0, 0), new Color(168, 204, 241),
082: new Point2D.Double(0, 1), new Color(44, 61, 146));
083: public static final GradientPaint MAC_OSX_SELECTED = new GradientPaint(
084: new Point2D.Double(0, 0), new Color(81, 141, 236),
085: new Point2D.Double(0, 1), new Color(36, 96, 192));
086: public static final GradientPaint MAC_OSX = new GradientPaint(
087: new Point2D.Double(0, 0), new Color(167, 210, 250),
088: new Point2D.Double(0, 1), new Color(99, 147, 206));
089: public static final GradientPaint AERITH = new GradientPaint(
090: new Point2D.Double(0, 0), Color.WHITE, new Point2D.Double(
091: 0, 1), new Color(64, 110, 161));
092: public static final GradientPaint GRAY = new GradientPaint(
093: new Point2D.Double(0, 0), new Color(226, 226, 226),
094: new Point2D.Double(0, 1), new Color(250, 248, 248));
095: public static final GradientPaint RED_XP = new GradientPaint(
096: new Point2D.Double(0, 0), new Color(236, 81, 81),
097: new Point2D.Double(0, 1), new Color(192, 36, 36));
098: public static final GradientPaint NIGHT_GRAY = new GradientPaint(
099: new Point2D.Double(0, 0), new Color(102, 111, 127),
100: new Point2D.Double(0, 1), new Color(38, 45, 61));
101: public static final GradientPaint NIGHT_GRAY_LIGHT = new GradientPaint(
102: new Point2D.Double(0, 0), new Color(129, 138, 155),
103: new Point2D.Double(0, 1), new Color(58, 66, 82));
104:
105: private GradientPaint paint;
106:
107: /**
108: * Creates a new instance of BasicGradientPainter
109: */
110: public BasicGradientPainter() {
111: }
112:
113: /**
114: * Creates a new instance of BasicGradientPainter
115: */
116: public BasicGradientPainter(GradientPaint paint) {
117: this .paint = paint;
118: }
119:
120: /**
121: * Constructs a simple acyclic <code>BasicGradientPainter</code> object.
122: *
123: * @param x1 x coordinate of the first specified
124: * <code>Point</code> in user space
125: * @param y1 y coordinate of the first specified
126: * <code>Point</code> in user space
127: * @param startColor <code>Color</code> at the first specified
128: * <code>Point</code>
129: * @param x2 x coordinate of the second specified
130: * <code>Point</code> in user space
131: * @param y2 y coordinate of the second specified
132: * <code>Point</code> in user space
133: * @param endColor <code>Color</code> at the second specified
134: * <code>Point</code>
135: * @throws NullPointerException if either one of colors is null
136: */
137: public BasicGradientPainter(float x1, float y1, Color startColor,
138: float x2, float y2, Color endColor) {
139: this .paint = new GradientPaint(x1, y1, startColor, x2, y2,
140: endColor);
141: }
142:
143: /**
144: * Constructs a simple acyclic <code>BasicGradientPainter</code> object.
145: *
146: * @param startPoint the first specified <code>Point</code> in user space
147: * @param startColor <code>Color</code> at the first specified
148: * <code>Point</code>
149: * @param endPoint the second specified <code>Point</code> in user space
150: * @param endColor <code>Color</code> at the second specified
151: * <code>Point</code>
152: * @throws NullPointerException if either one of colors or points
153: * is null
154: */
155: public BasicGradientPainter(Point2D startPoint, Color startColor,
156: Point2D endPoint, Color endColor) {
157:
158: this .paint = new GradientPaint(startPoint, startColor,
159: endPoint, endColor);
160: }
161:
162: /**
163: * Constructs either a cyclic or acyclic <code>BasicGradientPainter</code>
164: * object depending on the <code>boolean</code> parameter.
165: *
166: * @param x1 x coordinate of the first specified
167: * <code>Point</code> in user space
168: * @param y1 y coordinate of the first specified
169: * <code>Point</code> in user space
170: * @param startColor <code>Color</code> at the first specified
171: * <code>Point</code>
172: * @param x2 x coordinate of the second specified
173: * <code>Point</code> in user space
174: * @param y2 y coordinate of the second specified
175: * <code>Point</code> in user space
176: * @param endColor <code>Color</code> at the second specified
177: * <code>Point</code>
178: * @param cyclic <code>true</code> if the gradient pattern should cycle
179: * repeatedly between the two colors; <code>false</code> otherwise
180: */
181: public BasicGradientPainter(float x1, float y1, Color startColor,
182: float x2, float y2, Color endColor, boolean cyclic) {
183: paint = new GradientPaint(x1, y1, startColor, x2, y2, endColor,
184: cyclic);
185: }
186:
187: /**
188: * Constructs either a cyclic or acyclic <code>BasicGradientPainter</code>
189: * object depending on the <code>boolean</code> parameter.
190: *
191: * @param startPoint the first specified <code>Point</code>
192: * in user space
193: * @param startColor <code>Color</code> at the first specified
194: * <code>Point</code>
195: * @param endPoint the second specified <code>Point</code>
196: * in user space
197: * @param endColor <code>Color</code> at the second specified
198: * <code>Point</code>
199: * @param cyclic <code>true</code> if the gradient pattern should cycle
200: * repeatedly between the two colors; <code>false</code> otherwise
201: * @throws NullPointerException if either one of colors or points
202: * is null
203: */
204: public BasicGradientPainter(Point2D startPoint, Color startColor,
205: Point2D endPoint, Color endColor, boolean cyclic) {
206: paint = new GradientPaint(startPoint, startColor, endPoint,
207: endColor, cyclic);
208: }
209:
210: /**
211: * Set the gradient paint to use. This may be null. If null, nothing is painted
212: *
213: * @param paint the GradientPaint to use
214: */
215: public void setGradientPaint(GradientPaint paint) {
216: GradientPaint old = getGradientPaint();
217: this .paint = paint;
218: firePropertyChange("gradientPaint", old, getGradientPaint());
219: }
220:
221: /**
222: * @return the GradientPaint used for painting. This may be null
223: */
224: public GradientPaint getGradientPaint() {
225: return paint;
226: }
227:
228: /**
229: * @inheritDoc
230: */
231: protected Paint calculateSizedPaint(int width, int height) {
232: GradientPaint paint = getGradientPaint();
233: if (paint == null) {
234: return null;
235: }
236:
237: Point2D startPoint = paint.getPoint1();
238: Point2D endPoint = paint.getPoint2();
239:
240: double x1 = isResizeHorizontal() ? startPoint.getX() * width
241: : startPoint.getX();
242: double y1 = isResizeVertical() ? startPoint.getY() * height
243: : startPoint.getY();
244: double x2 = isResizeHorizontal() ? endPoint.getX() * width
245: : endPoint.getX();
246: double y2 = isResizeVertical() ? endPoint.getY() * height
247: : endPoint.getY();
248: startPoint = new Point2D.Double(x1, y1);
249: endPoint = new Point2D.Double(x2, y2);
250:
251: return new GradientPaint(startPoint, paint.getColor1(),
252: endPoint, paint.getColor2());
253: }
254:
255: //Experimental support for getting colors for gradients out of UIManager
256: //and hiding that fact from users, at least part of the time
257: // private static final class UIColor extends Color {
258: // private Color c = null;
259: // private Object key;
260: //
261: // public UIColor(Object key) {
262: // super(0xffffffff);
263: // this.key = key;
264: // maybeUpdate();
265: // }
266: //
267: // private void maybeUpdate() {
268: // Color newc = UIManager.getColor(key);
269: // if (c != newc) {
270: // c = newc;
271: // }
272: // if (c == null) {
273: // c = Color.WHITE;
274: // }
275: // }
276: //
277: // public float[] getComponents(ColorSpace cspace, float[] compArray) {
278: // maybeUpdate();
279: // return c.getComponents(cspace, compArray);
280: // }
281: //
282: // public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
283: // maybeUpdate();
284: // return c.getColorComponents(cspace, compArray);
285: // }
286: //
287: // public PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints) {
288: // maybeUpdate();
289: // return c.createContext(cm, r, r2d, xform, hints);
290: // }
291: //
292: // public float[] getRGBComponents(float[] compArray) {
293: // maybeUpdate();
294: // return c.getRGBComponents(compArray);
295: // }
296: //
297: // public float[] getRGBColorComponents(float[] compArray) {
298: // maybeUpdate();
299: // return c.getRGBColorComponents(compArray);
300: // }
301: //
302: // public float[] getColorComponents(float[] compArray) {
303: // maybeUpdate();
304: // return c.getColorComponents(compArray);
305: // }
306: //
307: // public float[] getComponents(float[] compArray) {
308: // maybeUpdate();
309: // return c.getComponents(compArray);
310: // }
311: //
312: // public int getGreen() {
313: // maybeUpdate();
314: // return c.getGreen();
315: // }
316: //
317: // public int getBlue() {
318: // maybeUpdate();
319: // return c.getBlue();
320: // }
321: //
322: // public ColorSpace getColorSpace() {
323: // maybeUpdate();
324: // return c.getColorSpace();
325: // }
326: //
327: // public Color brighter() {
328: // maybeUpdate();
329: // return c.brighter();
330: // }
331: //
332: // public int getAlpha() {
333: // maybeUpdate();
334: // return c.getAlpha();
335: // }
336: //
337: // public int getRed() {
338: // maybeUpdate();
339: // return c.getRed();
340: // }
341: //
342: // public int getRGB() {
343: // maybeUpdate();
344: // return c.getRGB();
345: // }
346: //
347: // public int getTransparency() {
348: // maybeUpdate();
349: // return c.getTransparency();
350: // }
351: //
352: // public Color darker() {
353: // maybeUpdate();
354: // return c.darker();
355: // }
356: // }
357: }
|