烟花小程序,来源于网络

这是一个烟花小程序,基于java的可视化实现的,因为其中应用到了Applet函数,所以现阶段只能在eclipse中实现。

  1 package 烟花;
  2 import java.applet.Applet;
  3 import java.awt.Color;
  4 import java.awt.Graphics;
  5 import java.net.URL;
  6 import java.util.Random;
  7 /**
  8  * 烟花
  9  *
 10  */
 11 @SuppressWarnings("serial")
 12 public class MissileDemo extends Applet implements Runnable {
 13 public int speed, variability, Max_Number, Max_Energy, Max_Patch,
 14             Max_Length, G;
 15     public String sound;
 16     private int width, height;                
 17     private Thread thread = null;                
 18     private BeaClassDemo bcd[];                
 19     public void init() {                        
 20         int i;
 21         this.setSize(1500, 800);                 
 22         width = getSize().width - 1;
 23         height = getSize().height - 1;
 24         speed = 60;                        // 烟花绽放的速度
 25         variability = 10;
 26         Max_Number = 180;                    // 可发出烟花的最大数目
 27         Max_Energy = width + 50;
 28         Max_Patch = 60;                    // 最大的斑点数
 29         Max_Length = 180;                    // 斑点的最大距离
 30         G = 50;                            // 向地面弯曲的力度
 31         bcd = new BeaClassDemo[Max_Number];            
 32         for (i = 0; i < Max_Number; i++)
 33             bcd[i] = new BeaClassDemo(width, height, G);    
 34     }
 35     public void start() {        
 36         if (thread == null) {
 37             thread = new Thread(this);
 38             thread.start();
 39         }
 40     }
 41     @SuppressWarnings("deprecation")
 42     public void stop() {        
 43         if (thread != null) {
 44             thread.stop();
 45             thread = null;
 46         }
 47     }
 48     @SuppressWarnings({ "unused", "static-access" })
 49     public void run() {
 50         int i;
 51         int E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4 + 1;
 52         int P = (int) (Math.random() * Max_Patch * 3 / 4)    // 烟花的斑点数
 53                 + Max_Patch / 4 + 1;
 54         int L = (int) (Math.random() * Max_Length * 3 / 4)    // 烟花可发射出的距离
 55                 + Max_Length / 4 + 1;
 56         long S = (long) (Math.random() * 10000);            
 57         boolean sleep;                                
 58         Graphics g = getGraphics();
 59         URL u = null;
 60         while (true) {
 61             try {
 62                 thread.sleep(1000 / speed);
 63             } catch (InterruptedException x) {
 64             }
 65             sleep = true;
 66             for (i = 0; i < Max_Number; i++)
 67                 sleep = sleep && bcd[i].sleep;
 68             if (sleep && Math.random() * 100 < variability) {
 69                 E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4
 70                         + 1;
 71 P = (int) (Math.random() * Max_Patch * 3 / 4) + Max_Patch / 4
 72                         + 1;
 73 L = (int) (Math.random() * Max_Length * 3 / 4) + Max_Length / 4
 74                         + 1;
 75                 S = (long) (Math.random() * 10000);
 76             }
 77             for (i = 0; i < Max_Number; i++) {
 78                 if (bcd[i].sleep && Math.random() * Max_Number * L < 1) {
 79 bcd[i].init(E, P, L, S);
 80 bcd[i].start();
 81                 }
 82                 bcd[i].show(g);
 83             }
 84         }
 85     }
 86     public void paint(Graphics g) {                    
 87         g.setColor(Color.black);                    
 88         g.fillRect(0, 0, width + 1, height + 1);        
 89     }
 90 }
 91 class BeaClassDemo {
 92 public boolean sleep = true;
 93 private int energy, patch, length, width, height, G, Xx, Xy, Ex[], Ey[], x,
 94             y, Red, Blue, Green, t;
 95     private Random random;                        
 96     public BeaClassDemo(int a, int b, int g) {            
 97         width = a;
 98         height = b;
 99         G = g;
100     }
101     public void init(int e, int p, int l, long seed) {
102         int i;
103     
104 energy = e;
105 patch = p;
106 length = l;
107         // 创建一个带种子的随机数生成器
108         random = new Random(seed);
109         Ex = new int[patch];                        
110         Ey = new int[patch];                    
111         Red = (int) (random.nextDouble() * 128) + 128;
112         Blue = (int) (random.nextDouble() * 128) + 128;
113         Green = (int) (random.nextDouble() * 128) + 128;
114         Xx = (int) (Math.random() * width / 2) + width / 4;
115         Xy = (int) (Math.random() * height / 2) + height / 4;
116         for (i = 0; i < patch; i++) {
117             Ex[i] = (int) (Math.random() * energy) - energy / 2;
118             Ey[i] = (int) (Math.random() * energy * 7 / 8) - energy / 8;
119         }
120     }
121     public void start() {
122    t = 0;
123    sleep = false;
124     }
125     public void show(Graphics g) {        
126         if (!sleep)                    
127             if (t < length) {
128                 int i, c;
129                 double s;
130                 Color color;
131                 c = (int) (random.nextDouble() * 64) - 32 + Red;
132                 if (c >= 0 && c < 256)
133                     Red = c;
134                 c = (int) (random.nextDouble() * 64) - 32 + Blue;
135                 if (c >= 0 && c < 256)
136                     Blue = c;
137                 c = (int) (random.nextDouble() * 64) - 32 + Green;
138                 if (c >= 0 && c < 256)
139                     Green = c;
140                 color = new Color(Red, Blue, Green);
141                 for (i = 0; i < patch; i++) {
142                     s = (double) t / 100;
143                     x = (int) (Ex[i] * s);
144                     y = (int) (Ey[i] * s - G * s * s);
145                     g.setColor(color);
146                     g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);
147                     if (t >= length / 2) {
148                         int j;
149                         for (j = 0; j < 2; j++) {
150                             s = (double) ((t - length / 2) * 2 + j) / 100;
151                             x = (int) (Ex[i] * s);
152                             y = (int) (Ey[i] * s - G * s * s);
153                             g.setColor(Color.black);
154                             g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);
155                         }
156                     }
157                 }
158                 t++;
159             } else {
160                 sleep = true;
161             }
162     }
163 }

 

posted @ 2020-06-29 22:10  最爱小猫咪  阅读(699)  评论(0)    收藏  举报