KickJava   Java API By Example, From Geeks To Geeks.

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


1 package snow.utils.gui;
2
3 import java.util.*;
4 import java.awt.*;
5 import java.awt.geom.*;
6 import javax.swing.*;
7
8 public class SnowBackgroundPanel extends JPanel
9 {
10   final SnowIcon snowIcon = new SnowIcon(55,55); //not 1
11
int numberOfFlocks = 5;
12   double[] posX, posY, sizes;
13
14   public boolean highContrast = false;
15
16   public SnowBackgroundPanel()
17   {
18      this(new BorderLayout());
19   }
20   public SnowBackgroundPanel(LayoutManager lm)
21   {
22      super(lm);
23   }
24
25   // indicate for which width the flocks were computed
26
int widthForFlocks = 0;
27
28   private void createFlocksPositions()
29   {
30      posX = new double[numberOfFlocks];
31      posY = new double[numberOfFlocks];
32      sizes = new double[numberOfFlocks];
33      widthForFlocks = this.getWidth();
34
35      for(int i=0; i<this.numberOfFlocks; i++)
36      {
37        sizes[i] = Math.random() * Math.max(widthForFlocks, getHeight())/3.7+0.01;
38
39        posX[i] = Math.random()*widthForFlocks-sizes[i]/2;
40        posY[i] = Math.random()*getHeight()-sizes[i]/2;
41      }
42
43   }
44
45   long calls = 0;
46
47   /** call to shift flocks on y
48     call this in the EDT !!
49   */

50   private void letSnowFall()
51   {
52     if(!SwingUtilities.isEventDispatchThread())
53     {
54       throw new RuntimeException JavaDoc("Must be called in the EDT");
55     }
56     calls++;
57
58     widthForFlocks = this.getWidth();
59     if(widthForFlocks != this.getWidth() || posX==null)
60     {
61        createFlocksPositions();
62     }
63     // so we're sure that the flocks are made
64

65     for(int i=0; i<this.numberOfFlocks; i++)
66     {
67
68        posX[i] += widthForFlocks*0.0008*Math.random();
69        posY[i] += widthForFlocks*0.0009*Math.random();
70
71        if(calls%500>250)
72        {
73          sizes[i] += (Math.random())*widthForFlocks/480.0;
74        }
75        else
76        {
77          sizes[i] -= (Math.random())*widthForFlocks/480.0;
78        }
79
80        if(sizes[i]<widthForFlocks/100)
81        {
82          sizes[i] = widthForFlocks/100;
83        }
84
85        if(sizes[i]>widthForFlocks/2)
86        {
87          sizes[i] = widthForFlocks/2;
88        }
89
90
91        if((posX[i]-sizes[i])>widthForFlocks)
92        {
93          posX[i] = -sizes[i];
94        }
95
96        if((posY[i]-sizes[i])>this.getHeight())
97        {
98          posY[i] = -sizes[i];
99        }
100
101     }
102
103
104   }
105
106
107
108  /**
109   * Overwritten paint method to have a slight color gradient.
110   */

111   public void paint( Graphics g )
112   {
113     // cool animation when the mouse drag, ...
114
// letSnowFall(); // not nice in transp trees and tables
115

116     Graphics2D graphics2D = (Graphics2D)g;
117     final Paint savePaint = graphics2D.getPaint();
118
119     int width = getWidth();
120     int height = getHeight();
121
122     if(width==0) width=1;
123     if(height==0) height=1;
124
125     int max = Math.max(width,height);
126
127     Color bgColor = UIManager.getColor("Panel.background");
128     Color destColor = null;
129
130     if(this.highContrast)
131     {
132       destColor = SnowBackgroundPanel.createMediumLighterOrDarkerColor(bgColor);
133     }
134     else
135     {
136       destColor = SnowBackgroundPanel.createSmallLighterOrDarkerColor(bgColor);
137     }
138
139     GradientPaint upperLeftGradientPaint =
140                     new GradientPaint( 0f,0f,
141                                        bgColor,
142                                        (float)max*3.2f, (float)max*3.2f,
143                                        destColor);
144     graphics2D.setPaint( upperLeftGradientPaint );
145
146     graphics2D.fill( graphics2D.getClip() );
147
148     if(widthForFlocks != this.getWidth())
149     {
150        createFlocksPositions();
151     }
152
153     if(posX!=null)
154     {
155       for(int i=0; i<posX.length; i++)
156       {
157          snowIcon.paintSnowFlake(graphics2D, bgColor, destColor, posX[i], posY[i], sizes[i]);
158       }
159     }
160
161
162     graphics2D.setPaint( savePaint );
163     super.paintChildren(graphics2D);
164   } // paint
165

166
167
168
169  /**
170   * Overwitten, so it doesnt clear all, but
171   * one has to call super, so children are properly rendered.
172   */

173   public void update( Graphics g )
174   {
175    //super.update(g);
176
paint(g);
177   }
178
179   Animator animator = null;
180
181
182   /** important: CALL stopAnimation() when you're disposing this...
183   */

184   public void startAnimation()
185   {
186     if(animator==null)
187     {
188       animator = new Animator();
189       animator.setPriority(Thread.MIN_PRIORITY);
190       animator.start();
191     }
192   }
193
194   public void stopAnimation()
195   {
196     if(animator!=null)
197     {
198       animator.stop = true;
199       animator = null;
200     }
201   }
202
203   class Animator extends Thread JavaDoc
204   {
205     public boolean stop;
206     public void run()
207     {
208       while(!stop)
209       {
210          try
211          {
212            Thread.sleep(150);
213          }
214          catch(Exception JavaDoc e) {}
215
216          EventQueue.invokeLater(new Runnable JavaDoc()
217          {
218             public void run()
219             {
220                letSnowFall();
221                repaint();
222             }
223          });
224       }
225     }
226   }
227
228
229   public static Color createSmallLighterOrDarkerColor(Color bgColor)
230   {
231     int r = bgColor.getRed();
232     int g = bgColor.getGreen();
233     int b = bgColor.getBlue();
234
235     if(r>245&&g>245&&b>245)
236     {
237       r -=20;
238       g -=20;
239       b -=15; // keep blue
240
}
241     else
242     {
243       r +=15;
244       g +=15;
245       b +=20; // give blue
246
}
247     if(r>255) r=255;
248     if(g>255) g=255;
249     if(b>255) b=255;
250
251     if(r<0) r=0;
252     if(g<0) g=0;
253     if(b<0) b=0;
254
255     return new Color(r,g,b);
256   }
257
258   public static Color createMediumLighterOrDarkerColor(Color bgColor)
259   {
260     int r = bgColor.getRed();
261     int g = bgColor.getGreen();
262     int b = bgColor.getBlue();
263
264     if(r>225&&g>225&&b>225)
265     {
266       r -=50;
267       g -=50;
268       b -=25; // keep blue
269
}
270     else
271     {
272       r +=50;
273       g +=50;
274       b +=25; // give blue
275
}
276     if(r>255) r=255;
277     if(g>255) g=255;
278     if(b>255) b=255;
279
280     if(r<0) r=0;
281     if(g<0) g=0;
282     if(b<0) b=0;
283
284     return new Color(r,g,b);
285   }
286
287
288   public static void main(String JavaDoc[] aa)
289   {
290     JFrame frame = new JFrame();
291     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
292     frame.getContentPane().setLayout(new BorderLayout());
293     SnowBackgroundPanel snowBack = new SnowBackgroundPanel();
294     frame.getContentPane().add(snowBack, BorderLayout.CENTER);
295     frame.setSize(200,200);
296     frame.setVisible(true);
297
298     snowBack.startAnimation();
299
300   }
301
302
303 } // SnowBackgroundPanel
Popular Tags