package com.java622p.wenzhi;
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.net.URL; import java.util.Random; /
- 烟花
- @author wenzhi * */ @SuppressWarnings(“serial”) public class MissileDemo extends Applet implements Runnable { public int speed, variability, Max_Number, Max_Energy, Max_Patch, Max_Length, G; public String sound; private int width, height;
private Thread thread = null;
private BeaClassDemo bcd[];
public void init() {
int i; this.setSize(400, 400);
width
= getSize().width - 1; height = getSize().height - 1; speed = 60; // 烟花绽放的速度 variability = 10; Max_Number = 180; // 可发出烟花的最大数目 Max_Energy = width + 50; Max_Patch = 60; // 最大的斑点数 Max_Length = 180; // 斑点的最大距离 G = 50; // 向地面弯曲的力度 bcd = new BeaClassDemo[Max_Number];
for (i = 0; i < Max_Number; i++) bcd[i] = new BeaClassDemo(width, height, G);
}
public void start() {
if (thread == null) { thread = new Thread(this); thread.start(); } } @SuppressWarnings(“deprecation”) public void stop() {
if (thread != null) { thread.stop(); thread = null; } } @SuppressWarnings({ “unused”, “static-access” }) public void run() { int i; int E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4 + 1; int P = (int) (Math.random() * Max_Patch * 3 / 4) // 烟花的斑点数 + Max_Patch / 4 + 1; int L = (int) (Math.random() * Max_Length * 3 / 4) // 烟花可发射出的距离 + Max_Length / 4 + 1; long S = (long) (Math.random() * 10000);
boolean sleep;
Graphics g
= getGraphics(); URL u = null; while (true) { try { thread.sleep(1000 / speed); } catch (InterruptedException x) { } sleep = true; for (i = 0; i < Max_Number; i++) sleep = sleep && bcd[i].sleep; if (sleep && Math.random() * 100 < variability) { E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4 + 1; P = (int) (Math.random() * Max_Patch * 3 / 4) + Max_Patch / 4 + 1; L = (int) (Math.random() * Max_Length * 3 / 4) + Max_Length / 4 + 1; S = (long) (Math.random() * 10000); } for (i = 0; i < Max_Number; i++) { if (bcd[i].sleep && Math.random() * Max_Number * L < 1) { bcd[i].init(E, P, L, S); bcd[i].start(); } bcd[i].show(g); } } } public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(
0, 0, width + 1, height + 1);
} }
class BeaClassDemo { public boolean sleep = true; private int energy, patch, length, width, height, G, Xx, Xy, Ex[], Ey[], x, y, Red, Blue, Green, t; private Random random;
public BeaClassDemo(int a, int b, int g) {
width
= a; height = b; G = g; } public void init(int e, int p, int l, long seed) { int i;
energy = e; patch = p; length = l;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个带种子的随机数生成器</span> random = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Random(seed); Ex </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[patch]; Ey </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[patch]; Red </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 128) + 128<span style="color: rgba(0, 0, 0, 1)">; Blue </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 128) + 128<span style="color: rgba(0, 0, 0, 1)">; Green </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 128) + 128<span style="color: rgba(0, 0, 0, 1)">; Xx </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Math.random() * width / 2) + width / 4<span style="color: rgba(0, 0, 0, 1)">; Xy </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Math.random() * height / 2) + height / 4<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i < patch; i++<span style="color: rgba(0, 0, 0, 1)">) { Ex[i] </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Math.random() * energy) - energy / 2<span style="color: rgba(0, 0, 0, 1)">; Ey[i] </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Math.random() * energy * 7 / 8) - energy / 8<span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> start() {
t = 0; sleep = false;
} </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> show(Graphics g) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">sleep) </span><span style="color: rgba(0, 0, 255, 1)">if</span> (t <<span style="color: rgba(0, 0, 0, 1)"> length) { </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> i, c; </span><span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> s; Color color; c </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 64) - 32 +<span style="color: rgba(0, 0, 0, 1)"> Red; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c >= 0 && c < 256<span style="color: rgba(0, 0, 0, 1)">) Red </span>=<span style="color: rgba(0, 0, 0, 1)"> c; c </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 64) - 32 +<span style="color: rgba(0, 0, 0, 1)"> Blue; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c >= 0 && c < 256<span style="color: rgba(0, 0, 0, 1)">) Blue </span>=<span style="color: rgba(0, 0, 0, 1)"> c; c </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (random.nextDouble() * 64) - 32 +<span style="color: rgba(0, 0, 0, 1)"> Green; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c >= 0 && c < 256<span style="color: rgba(0, 0, 0, 1)">) Green </span>=<span style="color: rgba(0, 0, 0, 1)"> c; color </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Color(Red, Blue, Green); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i < patch; i++<span style="color: rgba(0, 0, 0, 1)">) { s </span>= (<span style="color: rgba(0, 0, 255, 1)">double</span>) t / 100<span style="color: rgba(0, 0, 0, 1)">; x </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Ex[i] *<span style="color: rgba(0, 0, 0, 1)"> s); y </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Ey[i] * s - G * s *<span style="color: rgba(0, 0, 0, 1)"> s); g.setColor(color); g.drawLine(Xx </span>+ x, Xy - y, Xx + x, Xy -<span style="color: rgba(0, 0, 0, 1)"> y); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (t >= length / 2<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> j; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (j = 0; j < 2; j++<span style="color: rgba(0, 0, 0, 1)">) { s </span>= (<span style="color: rgba(0, 0, 255, 1)">double</span>) ((t - length / 2) * 2 + j) / 100<span style="color: rgba(0, 0, 0, 1)">; x </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Ex[i] *<span style="color: rgba(0, 0, 0, 1)"> s); y </span>= (<span style="color: rgba(0, 0, 255, 1)">int</span>) (Ey[i] * s - G * s *<span style="color: rgba(0, 0, 0, 1)"> s); g.setColor(Color.black); g.drawLine(Xx </span>+ x, Xy - y, Xx + x, Xy -<span style="color: rgba(0, 0, 0, 1)"> y); } } } t</span>++<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { sleep </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/17745.html