KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > EFCNBackgroundPanel


1 package snow.utils.gui;
2
3   /**
4    * A panel used as background for panels.
5    * Has some gradient and other drawing stuff on it.
6    *
7    * Note: You have to call setOpaque(false) for all
8    * swing objects, which you add to this panel,
9    * otherwise you wont see the panel color gradient.
10    *
11    * Copied from EFCN: http://www.snowraver.org
12    */

13
14 import javax.swing.*;
15 import javax.swing.border.*;
16 import java.awt.*;
17 import java.io.*;
18 import java.util.*;
19 import java.awt.event.*;
20 import java.beans.*;
21
22
23 public final class EFCNBackgroundPanel extends JPanel
24 {
25
26   // The gradient types :
27
public static final int ApplyUpperLeftCornerHighLight = 0;
28   public static final int ApplyVerticalHighLight = 1;
29
30   // Some predefined color keystrings :
31
public static final String JavaDoc ActiveTitleBackground = "InternalFrame.activeTitleBackground";
32   public static final String JavaDoc PanelBackground = "Panel.background";
33
34
35   // The gradient strength :
36
public static final int LightGradientStrength = 0;
37   public static final int MediumGradientStrength = 1;
38   public static final int StrongGradientStrength = 2;
39
40
41   private Color lightColor = new Color(190,190,250);
42   private Color mediumColor = new Color(120,120,180);
43   private Color darkColor = new Color(80,80,120);
44   float xGradient;
45
46   private Color basisColor; // around this the gradient will be
47

48   // gradient look : (defaults)
49
private int gradientType = ApplyUpperLeftCornerHighLight;
50   private int gradientStrength = MediumGradientStrength;
51   private String JavaDoc colorKey = ActiveTitleBackground;
52
53
54   private boolean useParentBackGround = false; // when a background color has been assigned
55

56
57
58
59
60  /**
61   * Creates a panel where you can pass
62   * theGradientType = ApplyUpperLeftCornerHighLight
63   * or ApplyVerticalHighLight
64   *
65   * and
66   * theGradientStrength = LightGradientStrength
67   * or MediumGradientStrength
68   * or StrongGradientStrength
69   *
70   * and
71   * theColorKey = null
72   * or ActiveTitleBackground
73   * or PanelBackground
74   * or any valid theme colorkey.
75   *
76   */

77   public EFCNBackgroundPanel( int theGradientType,
78                               int theGradientStrength,
79                               String JavaDoc theColorKey )
80   {
81     this( new BorderLayout(),true,theGradientType,theGradientStrength,theColorKey );
82   }
83
84
85
86
87
88  /**
89   * Creates a panel where you can pass
90   * theGradientType = ApplyUpperLeftCornerHighLight
91   * or ApplyVerticalHighLight
92   *
93   * and
94   * theGradientStrength = LightGradientStrength
95   * or MediumGradientStrength
96   * or StrongGradientStrength
97   *
98   * and
99   * theColorKey = null
100   * or ActiveTitleBackground
101   * or PanelBackground
102   * or any valid theme colorkey.
103   *
104   */

105   public EFCNBackgroundPanel( LayoutManager layout,
106                               int theGradientType,
107                               int theGradientStrength,
108                               String JavaDoc theColorKey )
109   {
110     this(layout,true,theGradientType,theGradientStrength,theColorKey);
111   }
112
113
114
115
116  /**
117   * Creates a panel where you can pass
118   * theGradientType = ApplyUpperLeftCornerHighLight
119   * or ApplyVerticalHighLight
120   *
121   * and
122   * theGradientStrength = LightGradientStrength
123   * or MediumGradientStrength
124   * or StrongGradientStrength
125   *
126   * and
127   * theColorKey = null
128   * or ActiveTitleBackground
129   * or PanelBackground
130   * or any valid theme colorkey.
131   *
132   */

133   public EFCNBackgroundPanel( LayoutManager layout,
134                               boolean isDoubleBuffered,
135                               int theGradientType,
136                               int theGradientStrength,
137                               String JavaDoc theColorKey )
138   {
139     super(layout,isDoubleBuffered);
140     this.gradientType = theGradientType;
141     this.gradientStrength = theGradientStrength;
142     this.updateSpecialUI(); // sets basisColor, startColor and endColor
143
if( colorKey != null )
144     {
145        this.colorKey = theColorKey;
146     } // else use the default
147
// scale the gradient along with the current font size :
148
float unitSize = UIManager.getFont("TextField.font").getSize2D();
149     this.xGradient = unitSize;
150
151     // additional tasks on lookand feel changes :
152
this.addPropertyChangeListener(
153      new PropertyChangeListener()
154      {
155         public void propertyChange(PropertyChangeEvent e)
156         {
157          if( !useParentBackGround ) // only if setBackground was never called
158
{
159             String JavaDoc name = e.getPropertyName();
160             if (name.equals("ancestor"))
161              {
162                SwingUtilities.invokeLater( new Runnable JavaDoc()
163                 {
164                    public void run()
165                    {
166                     updateSpecialUI();
167                     // rescale the gradient along with the current font size :
168
float unitSize = UIManager.getFont("TextField.font").getSize2D();
169                     xGradient = unitSize;
170                    }
171                 });
172              }
173           }
174         }
175      });
176
177   } // Constructor
178

179
180
181  /**
182   * Seta a fixed background color, and with that : turns out the
183   * UIManager update mechanism.
184   */

185   public void setBackground( Color bgColor )
186   {
187     super.setBackground(bgColor);
188     this.useParentBackGround = true; // turns off UIManager special update
189
this.basisColor = super.getBackground();
190     this.calculateColors();
191   }
192
193
194
195  /**
196   * Calculate the start and endcolor of the gradient
197   * taking the basisColor as center color :
198   */

199   private void calculateColors()
200   {
201     int colorOffset = 50; // medium gradient strength
202
if( this.gradientStrength == LightGradientStrength )
203      {
204        colorOffset = 30;
205      }
206     if( this.gradientStrength == StrongGradientStrength )
207      {
208        colorOffset = 70;
209      }
210     int rBase = this.basisColor.getRed();
211     int gBase = this.basisColor.getGreen();
212     int bBase = this.basisColor.getBlue();
213     // start color is lighter :
214
int rStart = rBase + colorOffset;
215     int gStart = gBase + colorOffset;
216     int bStart = bBase + colorOffset;
217     if( (rStart <= 255) && (gStart <= 255) && (bStart <= 255) )
218      {
219        this.lightColor = new Color( rStart,gStart,bStart );
220      } else
221      {
222        if( rStart > 255 ) rStart = 255;
223        if( gStart > 255 ) gStart = 255;
224        if( bStart > 255 ) bStart = 255;
225        this.lightColor = new Color( rStart,gStart,bStart );
226      }
227
228     this.mediumColor = this.basisColor;
229
230     rStart = rBase - colorOffset;
231     gStart = gBase - colorOffset;
232     bStart = bBase - colorOffset;
233     if( (rStart >= 0) && (gStart >= 0) && (bStart >= 0) )
234      {
235        this.darkColor = new Color( rStart,gStart,bStart );
236      } else
237      {
238        if( rStart < 0 ) rStart = 0;
239        if( gStart < 0 ) gStart = 0;
240        if( bStart < 0 ) bStart = 0;
241        this.darkColor = new Color( rStart,gStart,bStart );
242      }
243
244   }
245
246
247  /**
248   * Must be called, when the lf theme changes.
249   * Called by the propertychange listener above.
250   */

251   public void updateSpecialUI()
252   {
253     // Derive the basisColor :
254
Color color = UIManager.getColor(this.colorKey);
255     if( color == null )
256      {
257        color = new Color(250,170,150); // give it a green color to show
258
} // that the key was invalid.
259
// give a little bit more blue
260
int r = color.getRed() - 3;
261     int g = color.getGreen() - 3;
262     int b = color.getBlue() + 6;
263     // level out grayscale value a bit :
264
if( r+g+b > 384 )
265      {
266        r -= 10;
267        g -= 10;
268        b -= 10;
269      } else
270      {
271        r += 10;
272        g += 10;
273        b += 10;
274      }
275     // keep in range :
276
if( r < 0 ) r = 0; if( r > 255 ) r = 255;
277     if( g < 0 ) g = 0; if( g > 255 ) g = 255;
278     if( b < 0 ) b = 0; if( b > 255 ) b = 255;
279     // and set it as basis :
280
this.basisColor = new Color(r,g,b);
281     // Calculate the start and endColors from that :
282
this.calculateColors();
283   }
284
285
286  /**
287   * Overwritten paint method to have a slight color gradient.
288   */

289   public void paint( Graphics g )
290   {
291     Graphics2D graphics2D = (Graphics2D)g;
292     final Paint savePaint = graphics2D.getPaint();
293     if( this.gradientType == ApplyUpperLeftCornerHighLight )
294      {
295       GradientPaint upperLeftGradientPaint =
296                     new GradientPaint( 0f,0f,
297                                        lightColor,
298                                        xGradient,xGradient*5.0f,
299                                        mediumColor );
300
301       graphics2D.setPaint( upperLeftGradientPaint );
302       graphics2D.fill( graphics2D.getClip() );
303      }
304     else if( this.gradientType == ApplyVerticalHighLight )
305      {
306
307       float gradientLength = xGradient;
308       if( gradientLength > this.getHeight()/2.5f )
309        {
310          gradientLength = this.getHeight()/2.5f;
311        }
312       GradientPaint upperVerticalGradientPaint =
313                     new GradientPaint( 0f,0f,
314                                        this.lightColor,
315                                        0f, gradientLength,
316                                        this.mediumColor );
317
318       GradientPaint lowerVerticalGradientPaint =
319                     new GradientPaint( 0f,getHeight(),
320                                        this.darkColor,
321                                        0f,getHeight() - gradientLength,
322                                        this.mediumColor );
323
324       Shape saveClip = graphics2D.getClip();
325
326       graphics2D.setPaint( lowerVerticalGradientPaint );
327       graphics2D.fill( graphics2D.getClip() );
328
329       Rectangle r = new Rectangle( 0,0,getWidth(),1+getHeight()/2 );
330       graphics2D.setPaint( upperVerticalGradientPaint );
331       graphics2D.fill( r );
332
333       graphics2D.setClip(saveClip);
334      }
335     graphics2D.setPaint( savePaint );
336     super.paintChildren(graphics2D);
337   } // paint
338

339
340
341
342  /**
343   * Overwitten, so it doesnt clear all, but
344   * one has to call super, so children are properly rendered.
345   */

346   public void update( Graphics g )
347   {
348    //super.update(g);
349
paint(g);
350   }
351
352
353
354
355 } // EFCNBackgroundPanel
Popular Tags