java编写流星_纯Java代码实现流星划过天空废话不多说了,直接给⼤家贴java代码了。
import lor;
import aphics;
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;
java爱心代码编程简单
public meteorfly() {
panel = new mypanel();
this.setsize(, ); // 创建窗体
this.it_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, pe_int_bgr);
drawoffscreen = aphics();
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].d(); // 提取颜⾊
int g = p[i].en();
int b = p[i].blue();
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代码实现流星划过天空,希望本⽂分享能够给⼤家带来意想不到的收获。希望与⼴⼤⽹友互动??
点此进⾏留⾔吧!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。