欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-06 15:25:14
废话不多说了,直接给大家贴java代码了。 import java.awt.color; import java.awt.graphics; impo...

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