用java写的烟火效果

//http://community.csdn.net/Expert/topic/3672/3672834.xml?temp=5.533999E-02
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class yanhua extends JComponent implements Runnable{
  Image img; Graphics ig;
  public static void main(String arg[]){
    JFrame f=new JFrame("Paint");
    f.setBackground(Color.black);
    Container c=f.getContentPane();
    Flash flash=new Flash();
    c.add(flash);
    f.pack();
    f.show();

    new Thread(flash).start();   
  }
  public Dimension getPreferredSize(){return (new Dimension(300,300));}
  public void run(){
    int i,j,k; int t=15,per=20,n=t*per; int x[],y[],delay[];
    x=new int[t];y=new int[t];delay=new int[t];
    Color c[]=new Color[t];
    try{Thread.sleep(1000);}catch(Exception e){}
    Fire f[]=new Fire[n];
    for (i=0;i      c[i]=new Color((int)(Math.random()*100+150),(int)(Math.random()*100+150),(int)(Math.random()*100+150));
      delay[i]=(int)(Math.random()*50);
    }
    for (i=0;i      f[i]=new Fire(x[i/per],y[i/per],Math.cos(i*360/per)*30,Math.sin(i*360/per)*30,ig,c[i/per]);
    }
    for (i=0;i<100;i++){
      for (j=0;j        if (delay[j]>0) delay[j]--;
        else
          for (k=0;k            f[j*per+k].move();
          }
      }
      try{Thread.sleep(20);}catch(Exception e){}
      getGraphics().drawImage(img,0,0,300,300,null);
      repaint();
    }
  }

  public void paint(Graphics g){
    if (img==null){
      img=createImage(300,300);
      ig=img.getGraphics();
    }
    g.drawImage(img,0,0,300,300,null);
  }
}

class Fire{
  static double ga=9.8, delta=0.05;
  private double dx,dy;
  private double [] x=new double[3],y=new double[3];
  Graphics g; Color c;
  private int cnt;

  public Fire(double x, double y,double xDirection, double yDirection, Graphics g, Color c){
    dx=xDirection;dy=yDirection; this.x[0]=x; this.y[0]=y;
    this.g=g;this.c=c;   
    cnt=0;
  }

  public void move(){
    cnt++;
    if (cnt%20==0) this.c=this.c.darker();
    Color c=this.c; int i;
    for (i=2;;i--){
      g.setColor(Color.black);
      g.fillOval((int)x[i],(int)y[i],3,3);
      if (i==0) break;
      x[i]=x[i-1];y[i]=y[i-1];
    }
    x[0]+=dx*delta; y[0]+=dy*delta; dy+=ga*delta;
    for (i=0;i<3;i++){
      g.setColor(c);
      g.fillOval((int)x[i],(int)y[i],3,3);
      c=c.darker();
    }
  }
}

你可能感兴趣的:(文档资料)