/*
* Created on 2006-1-1
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;
/**
* @author hyvin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Snow extends Applet implements Runnable{
Thread mainThread;
Image im,gAlc[];
Random rand;
int stop,snows,wind,threadSleep;
int[] snowX,snowY;
long stopTime=0;
Dimension dim;
MediaTracker mt;
public Snow() {
}
String getParameter(String s1,String s2){
String s=getParameter(s1);
return (s!=null)?s:s2;
}
int getParameter(String s1,int s2, int max, int min){
String s=getParameter(s1);
if(s!=null){
if((s2=Integer.parseInt(s))> max)
return max;
else if(s2<min)
return min;
else return s2;
}
else return s2;
}
//Initialize the applet
public void init() {
rand=new Random();
dim=getSize();
im=createImage(dim.width,dim.height);
snows=getParameter("Snows",100,500,0);
snowX=new int[snows];
snowY=new int[snows];
for(int i=0;i<snows;i++){
snowX[i]=rand.nextInt()%(dim.width/2)+dim.width/2;
snowY[i]=rand.nextInt()%(dim.height/2)+dim.height/2;
}
mt=new MediaTracker(this);
gAlc=new Image[1];
gAlc[0]=getImage(getDocumentBase(),getParameter("graphic","leaf.gif"));
mt.addImage(gAlc[0],0);
try{
mt.waitForID(0);
}
catch(InterruptedException ex){
return;
}
stop=0;
}
public void start(){
if(mainThread==null){
mainThread=new Thread(this);
mainThread.start();
}
}
public void stop(){
mainThread=null;
}
public void run(){
while(mainThread!=null){
try{
Thread.sleep(threadSleep);
}
catch(InterruptedException ex){
return ;
}
repaint();
}
}
public void drawBackSnow(Graphics g){
g.setColor(Color.white);
for(int i=0;i<snows;i++){
g.fillOval(snowX[i],snowY[i],3,3);
snowX[i]+=rand.nextInt()%2+wind;
snowY[i]+=(rand.nextInt()%6+5)/5+1;
if(snowX[i]>=dim.width)
snowX[i]=0;
if(snowX[i]<0)
snowX[i]=dim.width-1;
if(snowY[i]>=dim.height||snowY[i]<0){
snowX[i]=Math.abs(rand.nextInt()%dim.width);
snowY[i]=0;
}
}
wind=rand.nextInt()%5-2;
}
public void paint(Graphics g){
im.getGraphics().setColor(Color.black);
im.getGraphics().fillRect(0,0,dim.width,dim.height);
im.getGraphics().drawImage(gAlc[0],0,0,this);
drawBackSnow(im.getGraphics());
g.drawImage(im,0,0,null);
}
public void updata(Graphics g){
paint(g);
}
}
声音图片你用自己的吧