KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > PaintUtilities


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * PaintUtilities.java
29  * -------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PaintUtilities.java,v 1.9 2005/11/16 15:58:41 taqua Exp $
36  *
37  * Changes
38  * -------
39  * 13-Nov-2003 : Version 1 (DG);
40  * 04-Oct-2004 : Renamed PaintUtils --> PaintUtilities (DG);
41  * 23-Feb-2005 : Rewrote equal() method with less indenting required (DG);
42  *
43  */

44
45 package org.jfree.util;
46
47 import java.awt.GradientPaint JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.awt.Color JavaDoc;
50 import java.lang.reflect.Field JavaDoc;
51 import java.lang.reflect.Modifier JavaDoc;
52
53 /**
54  * Utility code that relates to <code>Paint</code> objects.
55  *
56  * @author David Gilbert
57  */

58 public class PaintUtilities {
59
60     /**
61      * Private constructor prevents object creation.
62      */

63     private PaintUtilities() {
64     }
65
66     /**
67      * Returns <code>true</code> if the two <code>Paint</code> objects are equal
68      * OR both <code>null</code>. This method handles
69      * <code>GradientPaint</code> as a special case.
70      *
71      * @param p1 paint 1 (<code>null</code> permitted).
72      * @param p2 paint 2 (<code>null</code> permitted).
73      *
74      * @return A boolean.
75      */

76     public static boolean equal(final Paint JavaDoc p1, final Paint JavaDoc p2) {
77
78         // handle cases where either or both arguments are null
79
if (p1 == null) {
80             return (p2 == null);
81         }
82         if (p2 == null) {
83             return false;
84         }
85         
86         boolean result = false;
87         // handle GradientPaint as a special case...
88
if (p1 instanceof GradientPaint JavaDoc && p2 instanceof GradientPaint JavaDoc) {
89             final GradientPaint JavaDoc gp1 = (GradientPaint JavaDoc) p1;
90             final GradientPaint JavaDoc gp2 = (GradientPaint JavaDoc) p2;
91             result = gp1.getColor1().equals(gp2.getColor1())
92                 && gp1.getColor2().equals(gp2.getColor2())
93                 && gp1.getPoint1().equals(gp2.getPoint1())
94                 && gp1.getPoint2().equals(gp2.getPoint2())
95                 && gp1.isCyclic() == gp2.isCyclic()
96                 && gp1.getTransparency() == gp1.getTransparency();
97         }
98         else {
99             result = p1.equals(p2);
100         }
101         return result;
102
103     }
104
105     /**
106      * Converts a color into a string. If the color is equal to one of the
107      * defined constant colors, that name is returned instead. Otherwise the
108      * color is returned as hex-string.
109      *
110      * @param c the color.
111      * @return the string for this color.
112      */

113     public static String JavaDoc colorToString (final Color JavaDoc c)
114     {
115       try {
116           final Field JavaDoc[] fields = Color JavaDoc.class.getFields();
117           for (int i = 0; i < fields.length; i++) {
118               final Field JavaDoc f = fields[i];
119               if (Modifier.isPublic(f.getModifiers())
120                   && Modifier.isFinal(f.getModifiers())
121                   && Modifier.isStatic(f.getModifiers())) {
122                   final String JavaDoc name = f.getName();
123                   final Object JavaDoc oColor = f.get(null);
124                   if (oColor instanceof Color JavaDoc) {
125                       if (c.equals(oColor)) {
126                           return name;
127                       }
128                   }
129               }
130           }
131       }
132       catch (Exception JavaDoc e) {
133           //
134
}
135
136       // no defined constant color, so this must be a user defined color
137
final String JavaDoc color = Integer.toHexString(c.getRGB() & 0x00ffffff);
138       final StringBuffer JavaDoc retval = new StringBuffer JavaDoc(7);
139       retval.append("#");
140
141       final int fillUp = 6 - color.length();
142       for (int i = 0; i < fillUp; i++) {
143           retval.append("0");
144       }
145
146       retval.append(color);
147       return retval.toString();
148     }
149
150     /**
151      * Converts a given string into a color.
152      *
153      * @param value the string, either a name or a hex-string.
154      * @return the color.
155      */

156     public static Color JavaDoc stringToColor (final String JavaDoc value)
157     {
158       if (value == null) {
159           return Color.black;
160       }
161       try {
162           // get color by hex or octal value
163
return Color.decode(value);
164       }
165       catch (NumberFormatException JavaDoc nfe) {
166           // if we can't decode lets try to get it by name
167
try {
168               // try to get a color by name using reflection
169
final Field JavaDoc f = Color JavaDoc.class.getField(value);
170
171               return (Color JavaDoc) f.get(null);
172           }
173           catch (Exception JavaDoc ce) {
174               Log.info("No such Color : " + value);
175               // if we can't get any color return black
176
return Color.black;
177           }
178       }
179     }
180 }
181
Popular Tags