KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticXPUtils


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35
36 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
37
38 import com.jgoodies.looks.LookUtils;
39
40
41 /**
42  * Consists exclusively of static methods that provide convenience behavior.
43  *
44  * @author Karsten Lentzsch
45  * @version $Revision: 1.1 $
46  */

47
48 public final class PlasticXPUtils {
49
50     /*
51      * Draws a plain border for an xp button.
52      */

53     static void drawPlainButtonBorder(Graphics JavaDoc g, int x, int y, int w, int h) {
54         drawButtonBorder(g, x, y, w, h,
55                     PlasticLookAndFeel.getControl(),
56                     PlasticLookAndFeel.getControlDarkShadow(),
57                     LookUtils.getSlightlyBrighter(
58                         PlasticLookAndFeel.getControlDarkShadow(),
59                         1.25f)
60                       );
61     }
62
63     /*
64      * Draws a border for a pressed xp button.
65      */

66     static void drawPressedButtonBorder(Graphics JavaDoc g, int x, int y, int w, int h) {
67         drawPlainButtonBorder(g, x, y, w, h);
68         Color JavaDoc darkColor =
69                 translucentColor(PlasticLookAndFeel.getControlDarkShadow(),
70                                  128);
71         Color JavaDoc lightColor =
72                 translucentColor(PlasticLookAndFeel.getControlHighlight(),
73                                  80);
74         g.translate(x, y);
75         g.setColor(darkColor);
76         g.fillRect(2, 1, w-4, 1);
77
78         g.setColor(lightColor);
79         g.fillRect(2, h-2, w-4, 1);
80         g.translate(-x, -y);
81     }
82
83     /*
84      * Draws a border for a default xp button.
85      */

86     static void drawDefaultButtonBorder(Graphics JavaDoc g, int x, int y, int w, int h) {
87         drawPlainButtonBorder(g, x, y, w, h);
88         drawInnerButtonDecoration(g, x, y, w, h,
89                     PlasticLookAndFeel.getPrimaryControlDarkShadow());
90     }
91
92     /*
93      * Draws a border for a focused xp button.
94      */

95     static void drawFocusedButtonBorder(Graphics JavaDoc g, int x, int y, int w, int h) {
96         drawPlainButtonBorder(g, x, y, w, h);
97         drawInnerButtonDecoration(g, x, y, w, h,
98                     PlasticLookAndFeel.getFocusColor());
99     }
100     
101     /*
102      * Draws a border for a disabled xp button.
103      */

104     static void drawDisabledButtonBorder(Graphics JavaDoc g, int x, int y, int w, int h) {
105         drawButtonBorder(g, x, y, w, h,
106                     PlasticLookAndFeel.getControl(),
107                     MetalLookAndFeel.getControlShadow(),
108                     LookUtils.getSlightlyBrighter(
109                         MetalLookAndFeel.getControlShadow()));
110     }
111
112
113     /*
114      * Draws a button border for an xp button with the given colors.
115      */

116     public static void drawButtonBorder(
117         Graphics JavaDoc g,
118         int x, int y, int w, int h,
119         Color JavaDoc backgroundColor,
120         Color JavaDoc edgeColor,
121         Color JavaDoc cornerColor) {
122             
123         g.translate(x, y);
124         // Edges
125
g.setColor(edgeColor);
126         drawRect(g, 0, 0, w-1, h-1);
127         
128         // Near corners
129
g.setColor(cornerColor);
130         g.fillRect(0, 0, 2, 2);
131         g.fillRect(0, h-2, 2, 2);
132         g.fillRect(w-2, 0, 2, 2);
133         g.fillRect(w-2, h-2, 2, 2);
134
135         // Corners
136
g.setColor(backgroundColor);
137         g.fillRect(0, 0, 1, 1);
138         g.fillRect(0, h-1, 1, 1);
139         g.fillRect(w-1, 0, 1, 1);
140         g.fillRect(w-1, h-1, 1, 1);
141
142         g.translate(-x, -y);
143     }
144     
145     /*
146      * Draws a button border for an xp button with the given colors.
147      */

148     private static void drawInnerButtonDecoration(
149         Graphics JavaDoc g,
150         int x, int y, int w, int h,
151         Color JavaDoc baseColor) {
152             
153         Color JavaDoc lightColor = translucentColor(baseColor, 90);
154         Color JavaDoc mediumColor = translucentColor(baseColor, 120);
155         Color JavaDoc darkColor = translucentColor(baseColor, 200);
156
157         g.translate(x, y);
158         g.setColor(lightColor);
159         g.fillRect(2, 1, w-4, 1);
160         
161         g.setColor(mediumColor);
162         g.fillRect (1, 2, 1, h-4);
163         g.fillRect (w-2, 2, 1, h-4);
164         drawRect(g, 2, 2, w-5, h-5);
165
166         g.setColor(darkColor);
167         g.fillRect(2, h-2, w-4, 1);
168         g.translate(-x, -y);
169     }
170         
171
172     /*
173      * An optimized version of Graphics.drawRect.
174      */

175     static void drawRect(Graphics JavaDoc g, int x, int y, int w, int h) {
176         g.fillRect(x, y, w+1, 1);
177         g.fillRect(x, y+1, 1, h);
178         g.fillRect(x+1, y+h, w, 1);
179         g.fillRect(x+w, y+1, 1, h);
180     }
181     
182     
183     /**
184      * Answers a color that is a translucent copy of the given color.
185      *
186      * @param baseColor the base color
187      * @param alpha the alpha value
188      * @return Color the translucent color with specified alpha
189      */

190     private static Color JavaDoc translucentColor(Color JavaDoc baseColor, int alpha) {
191         return new Color JavaDoc(baseColor.getRed(),
192                           baseColor.getGreen(),
193                           baseColor.getBlue(),
194                           alpha);
195     }
196     
197 }
Popular Tags