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.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 = Graphics();
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].Red(); // 提取颜⾊
int G = p[i].Green();
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();
实现特效的代码js}
}
}
}
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);
}
}
}
}

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