純Java代碼實(shí)現(xiàn)流星劃過(guò)天空
廢話不多說(shuō)了,直接給大家貼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 = ; // (~)流星的個(gè)數(shù)
final int SLEEP = ; // 流星飛行的速度(數(shù)值越大,速度越慢)
final int COLORLV = ; // (~)色階(可改變光暈大?。?
final String COLOR = null; // ("#"~"#ffffff")光暈顏色(如果不填或null,則為默認(rèn)顏色)
final int SIZE = ; // (~)流星大小
private MyPanel panel;
public MeteorFly() {
panel = new MyPanel();
this.getContentPane().add(panel);
this.setSize(, ); // 創(chuàng)建窗體
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() * ); //隨機(jī)生成流星出現(xiàn)位置
if (rand > ) {
x = (int) (Math.random() * );
y = ;
} else {
y = (int) (Math.random() * );
x = ;
}
mx = (int) (Math.random() * + ); //隨機(jī)生成下落速度和角度
my = (int) (Math.random() * + );
if (COLOR == null || COLOR.length() == ) {
color = new Color(
// 隨機(jī)顏色
(new Double(Math.random() * )).intValue() + ,
(new Double(Math.random() * )).intValue() + ,
(new Double(Math.random() * )).intValue() + );
} else {
color = Color.decode(COLOR);
}
}
}
}
以上代碼就是本文給大家講述的純Java代碼實(shí)現(xiàn)流星劃過(guò)天空,希望本文分享能夠給大家?guī)?lái)意想不到的收獲。
相關(guān)文章
SpringBoot3.3.X整合Mybatis-Plus的實(shí)現(xiàn)示例
本文介紹了在Spring Boot 3.3.2中整合MyBatis-Plus 3.5.7,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
java MyBatis攔截器Inteceptor詳細(xì)介紹
這篇文章主要介紹了java MyBatis攔截器Inteceptor詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
maven package 打包報(bào)錯(cuò) Failed to execute goal的解決
這篇文章主要介紹了maven package 打包報(bào)錯(cuò) Failed to execute goal的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring boot集成redis lettuce代碼實(shí)例
這篇文章主要介紹了Spring boot集成redis lettuce代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring Boot的Controller控制層和頁(yè)面
這篇文章主要介紹了Spring Boot的Controller控制層和頁(yè)面,需要的朋友可以參考下2017-04-04
springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter
這篇文章主要介紹了springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理
這篇文章主要介紹了SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java并發(fā)編程ArrayBlockingQueue的使用
ArrayBlockingQueue是一個(gè)備受矚目的有界阻塞隊(duì)列,本文將全面深入地介紹ArrayBlockingQueue的內(nèi)部機(jī)制、使用場(chǎng)景以及最佳實(shí)踐,感興趣的可以了解一下2024-08-08
詳解在SpringBoot中使用MongoDb做單元測(cè)試的代碼
這篇文章主要介紹了詳解在SpringBoot中使用MongoDb做單元測(cè)試的代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解
這篇文章主要介紹了StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

