纯Java代码实现流星划过天空
作者:_Nick 时间:2022-06-01 12:12:35
废话不多说了,直接给大家贴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代码实现流星划过天空,希望本文分享能够给大家带来意想不到的收获。
标签:java,流星划过天空
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于C#操作文件路径(Directory)的常用静态方法详解
2023-06-06 10:40:12
![](https://img.aspxhome.com/file/2023/6/69806_0s.png)
使用SpringBoot配置https(SSL证书)
2022-04-03 19:37:00
![](https://img.aspxhome.com/file/2023/3/108423_0s.png)
使用maven开发springboot项目时pom.xml常用配置(推荐)
2022-09-19 23:33:48
如何在C#项目中链接一个文件夹下的所有文件详解
2022-09-08 08:48:22
springboot注解Aspect实现方案
2022-12-17 19:32:06
WPF自定义控件的实现
2021-07-06 02:34:11
![](https://img.aspxhome.com/file/2023/5/74825_0s.jpg)
java之Object类用法实例
2023-11-05 04:14:26
![](https://img.aspxhome.com/file/2023/8/58828_0s.png)
SpringBoot @Import与@Conditional注解使用详解
2023-12-14 16:42:45
![](https://img.aspxhome.com/file/2023/5/106095_0s.png)
Java PreparedStatement用法详解
2023-08-08 20:20:51
![](https://img.aspxhome.com/file/2023/1/58061_0s.png)
解决jasperreport导出的pdf每页显示的记录太少问题
2023-04-14 13:02:01
![](https://img.aspxhome.com/file/2023/0/77080_0s.jpg)
Java逃逸分析详解及代码示例
2021-05-29 15:23:15
java实现一次性压缩多个文件到zip中的方法示例
2021-11-16 07:24:47
Java笛卡尔积算法原理与实现方法详解
2023-11-18 22:08:17
![](https://img.aspxhome.com/file/2023/1/101981_0s.jpg)
JetBrains IntelliJ IDEA 配置优化技巧
2022-06-19 08:31:38
![](https://img.aspxhome.com/file/2023/7/64067_0s.png)
Java二维数组查找功能代码实现
2023-01-04 19:47:17
java向多线程中传递参数的三种方法详细介绍
2023-07-11 21:52:55
Java中常用时间的一些相关方法
2022-02-06 11:33:38
Unity实现虚拟键盘
2022-05-24 11:25:38
![](https://img.aspxhome.com/file/2023/9/78139_0s.jpg)
docker网络配置过程详解介绍
2023-02-07 23:04:15
![](https://img.aspxhome.com/file/2023/4/108274_0s.jpg)
Java深入探究Object类的方法
2022-11-24 06:48:53
![](https://img.aspxhome.com/file/2023/4/67014_0s.png)