import java.awt.*; import java.applet.Applet; import java.math.*; import java.util.*; import java.awt.image.ImageObserver; import java.awt.image.PixelGrabber; import java.net.*; /* ** --- SnowFlakes.java --- ** ** (c) 2000 Stefan Robl ** ** Web: http://www.qdev.de ** ** 2004-11-07: fixed a problem which made it impossible ** to run properly in some VMs */ public class SnowFlakes extends Applet implements Runnable { Image img; Graphics gfx; Thread thread; Random random=new Random(); int snowflakes, flakedither; int stormnew, storm, stormw, stormv; boolean stormon=true; int sideglue, moveglue; int iteration=0; int xx[], yy[], pixels[]; int width, height; Color black=new Color(0,0,0); Color white=new Color(255,255,255); // **************************************************************** public int get_int_param(String str, int def, int min, int max) { int val=def; if ((str=getParameter(str))!=null) { try { val=new Integer(str).intValue(); } catch ( NumberFormatException e ) { } if (valmax) val=max; } return val; } public void init() { String str; width=this.size().width; height=this.size().height; img=createImage(width,height); gfx=img.getGraphics(); gfx.setColor(black); gfx.fillRect(0, 0, width, height); snowflakes = get_int_param("SNOWFLAKES", 5000, 1, 100000); flakedither = get_int_param("FLAKEDITHER", -1, 4, 100000); sideglue = get_int_param("SIDEGLUE", -1, -1, 100000); moveglue = get_int_param("MOVEGLUE", -1, -1, 100000); xx = new int [snowflakes]; yy = new int [snowflakes]; pixels = new int [width*height]; if ((str=getParameter("PICTURE"))!=null) { Image image=getImage(getDocumentBase(), str); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForAll(); } // wait for image loading catch (InterruptedException e) { } PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { } gfx.drawImage(image, 0, 0, width, height, this); } if ((str=getParameter("STORM"))!=null) { if (str.equalsIgnoreCase("OFF")) stormon=false; } for(int i=0; i=0 && x=1 && y=0 && x=1 && y=width-1) out=true; if (yy[n]>=height-1) { reset_snowflake(n); } else if (point(xx[n]+xp, yy[n])) { boolean dr=true; if (!point(xx[n], yy[n])) { dr=false; } else if (!point(xx[n]-xp, yy[n])) { xx[n]-=xp; dr=false; } if (dr || (sideglue>=0 && rand(sideglue)==0)) { reset_snowflake(n); } } else if (point(xx[n]+xp, yy[n]+1)) { xx[n]+=xp; boolean dr=true; int r, f=0; do { r=rand(2); switch (r) { case 0: if (!point(xx[n]-1, yy[n]) && point(xx[n]+1, yy[n]) && !point(xx[n]-1, yy[n]+1)) { xx[n]--; dr=false; } f|=1; break; case 1: if (point(xx[n]-1, yy[n]) && !point(xx[n]+1, yy[n]) && !point(xx[n]+1, yy[n]+1)) { xx[n]++; dr=false; } f|=2; break; case 2: if (!point(xx[n]-1, yy[n]) && !point(xx[n]+1, yy[n]) && !point(xx[n]-1, yy[n]+1) && !point(xx[n]+1, yy[n]+1)) { xx[n]+=(rand(1)*2)-1; dr=false; } f|=4; break; } } while (f!=7 && dr); if (!out && (xx[n]<=0 || xx[n]>=width-1)) dr=true; if (dr || (moveglue>=0 && rand(moveglue)==0)) { reset_snowflake(n); } } else { xx[n]+=xp; } } public void setstorm(int s) { stormv=0; stormw=0; if (stormon) { if (s<-100) s=-100; if (s>100) s=100; if (s<0) { stormv=-1; stormw=100+s+5; } else if (s>0) { stormv=1; stormw=100-s+5; } } } public void calc_storm() { if (stormstormnew) storm--; setstorm(storm); if (iteration++>500) { stormnew=75+rand(25); if (rand(1)==0) stormnew*=-1; iteration=0; } } // ******************************************************* public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } public void run() { while (true) { repaint(); try { Thread.sleep(10); } catch(InterruptedException e) {} } } public void paint( Graphics g ) { int i; for(i=0; i=0 && (rand(flakedither)==0 || flakedither==0)) control_snowflake(i, rand(2)-1); if (rand(stormw)==0 || stormw==0) control_snowflake(i, stormv); control_snowflake(i, 0); } for(i=0; i