java编写流星_纯Java代码实现流星划过天空

废话不多说了,直接给大家贴java代码了。

import java.awt.color;

import java.awt.graphics;

import java.awt.image.bufferedimage;

import javax.swing.jframe;

import javax.swing.jpanel;

public class meteorfly extends jframe {

final int max = ; // (~)流星的个数

final int sleep = ; // 流星飞行的速度(数值越大,速度越慢)

final int colorlv = ; // (~)色阶(可改变光晕大小)

final string color = null; // ("#"~"#ffffff")光晕颜色(如果不填或null,则为默认颜色)

final int size = ; // (~)流星大小

private mypanel panel;

public meteorfly() {

panel = new mypanel();

this.getcontentpane().add(panel);

this.setsize(, ); // 创建窗体

this.setdefaultcloseoperation(jframe.exit_on_close);

this.setvisible(true);

}

public static void main(string[] args) {

new meteorfly();

}

class mypanel extends jpanel implements runnable {

meteor p[];

int appletwidth, appletheight;

bufferedimage offscreen;

graphics drawoffscreen;

thread pthread;

public mypanel() {

setbackground(color.black); //窗体初始化

appletwidth = ;

appletheight = ;

p = new meteor[max];

for (int i = ; i < max; i++)

p[i] = new meteor();

offscreen = new bufferedimage(appletwidth, appletheight,

bufferedimage.type_int_bgr);

drawoffscreen = offscreen.getgraphics();

pthread = new thread(this);

pthread.start();

}

@override

public void paintcomponent(graphics g) {

// todo auto-generated method stub

super.paintcomponents(g);

g.drawimage(offscreen, , , this);

}

@override

final public void run() {

while (true) {

// drawoffscreen.clearrect(, , appletwidth, appletheight); //

// 清屏

for (int i = ; i < max; i++) {

drawoffscreen.setcolor(p[i].color); // rgb颜色

drawoffscreen.filloval(p[i].x, p[i].y, size, size);

p[i].x += p[i].mx;

p[i].y += p[i].my;

// if (p[i].x > appletwidth || p[i].y > appletheight) {

// p[i].reset();

// }

int x = p[i].x;

int y = p[i].y;

int r = p[i].color.getred(); // 提取颜色

int g = p[i].color.getgreen();

int b = p[i].color.getblue();

while (true) {

if (r == && g == && b == ) {

break;

}

r -= colorlv; // 尾部颜色淡化

if (r < ) {

r = ;

}

g -= colorlv;

if (g < ) {

g = ;

}

b -= colorlv;

if (b < ) {

b = ;

}

color color = new color(r, g, b);

x -= p[i].mx; // 覆盖尾部

y -= p[i].my;

drawoffscreen.setcolor(color);

drawoffscreen.filloval(x, y, size, size);

}

if (x > appletwidth || y > appletheight) { // 流星飞出窗口,重置流星

p[i].reset();

}

}

repaint();

try {

thread.sleep(sleep);

} catch (interruptedexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

}

}

}

class meteor { // 流星类

int x, y; // 流星的位置

int mx, my; // 下落速度

color color; // 流星颜色

public meteor() {

reset();

}

public void reset() {

int rand = (int) (math.random() * ); //随机生成流星出现位置

if (rand > ) {

x = (int) (math.random() * );

y = ;

} else {

y = (int) (math.random() * );

x = ;

}

mx = (int) (math.random() * + ); //随机生成下落速度和角度

my = (int) (math.random() * + );

if (color == null || color.length() == ) {

color = new color(

// 随机颜色

(new double(math.random() * )).intvalue() + ,

(new double(math.random() * )).intvalue() + ,

(new double(math.random() * )).intvalue() + );

} else {

color = color.decode(color);

}

}

}

}

以上代码就是本文给大家讲述的纯java代码实现流星划过天空,希望本文分享能够给大家带来意想不到的收获。

希望与广大网友互动??

点此进行留言吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值