java效果_java编写烟花效果

packagecom.java622p.wenzhi;importjava.applet.Applet;importjava.awt.Color;importjava.awt.Graphics;importjava.net.URL;importjava.util.Random;/*** 烟花

*@authorwenzhi

**/@SuppressWarnings("serial")public class MissileDemo extends Applet implementsRunnable {public intspeed, variability, Max_Number, Max_Energy, Max_Patch,

Max_Length, G;publicString sound;private intwidth, height;private Thread thread = null;privateBeaClassDemo bcd[];public voidinit() {inti;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 = newBeaClassDemo[Max_Number];for (i = 0; i < Max_Number; i++)

bcd[i]= newBeaClassDemo(width, height, G);

}public voidstart() {if (thread == null) {

thread= new Thread(this);

thread.start();

}

}

@SuppressWarnings("deprecation")public voidstop() {if (thread != null) {

thread.stop();

thread= null;

}

}

@SuppressWarnings({"unused", "static-access"})public voidrun() {inti;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);booleansleep;

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

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 voidpaint(Graphics g) {

g.setColor(Color.black);

g.fillRect(0, 0, width + 1, height + 1);

}

}classBeaClassDemo {public boolean sleep = true;private intenergy, patch, length, width, height, G, Xx, Xy, Ex[], Ey[], x,

y, Red, Blue, Green, t;privateRandom random;public BeaClassDemo(int a, int b, intg) {

width=a;

height=b;

G=g;

}public void init(int e, int p, int l, longseed) {inti;

energy=e;

patch=p;

length=l;//创建一个带种子的随机数生成器

random = newRandom(seed);

Ex= new int[patch];

Ey= new int[patch];

Red= (int) (random.nextDouble() * 128) + 128;

Blue= (int) (random.nextDouble() * 128) + 128;

Green= (int) (random.nextDouble() * 128) + 128;

Xx= (int) (Math.random() * width / 2) + width / 4;

Xy= (int) (Math.random() * height / 2) + height / 4;for (i = 0; i < patch; i++) {

Ex[i]= (int) (Math.random() * energy) - energy / 2;

Ey[i]= (int) (Math.random() * energy * 7 / 8) - energy / 8;

}

}public voidstart() {

t= 0;

sleep= false;

}public voidshow(Graphics g) {if (!sleep)if (t

Color color;

c= (int) (random.nextDouble() * 64) - 32 +Red;if (c >= 0 && c < 256)

Red=c;

c= (int) (random.nextDouble() * 64) - 32 +Blue;if (c >= 0 && c < 256)

Blue=c;

c= (int) (random.nextDouble() * 64) - 32 +Green;if (c >= 0 && c < 256)

Green=c;

color= newColor(Red, Blue, Green);for (i = 0; i < patch; i++) {

s= (double) t / 100;

x= (int) (Ex[i] *s);

y= (int) (Ey[i] * s - G * s *s);

g.setColor(color);

g.drawLine(Xx+ x, Xy - y, Xx + x, Xy -y);if (t >= length / 2) {intj;for (j = 0; j < 2; j++) {

s= (double) ((t - length / 2) * 2 + j) / 100;

x= (int) (Ex[i] *s);

y= (int) (Ey[i] * s - G * s *s);

g.setColor(Color.black);

g.drawLine(Xx+ x, Xy - y, Xx + x, Xy -y);

}

}

}

t++;

}else{

sleep= true;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值