纯Java代码实现流星划过天空

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

废话不多说了,直接给大家贴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代码实现流星划过天空,希望本文分享能够给大家带来意想不到的收获。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多