你的位置:首页 > Java教程

[Java教程]【原创】java 流星划过天空


 1 import java.awt.Color; 2 import java.awt.Graphics; 3 import java.awt.image.BufferedImage; 4  5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7  8 public class MeteorFly extends JFrame { 9  10   final int MAX = 5; // (1~1000)流星的个数 11   final int SLEEP = 30; // 流星飞行的速度(数值越大,速度越慢) 12   final int COLORLV = 2; // (2~20)色阶(可改变光晕大小) 13   final String COLOR = null; // ("#000000"~"#ffffff")光晕颜色(如果不填或null,则为默认颜色) 14   final int SIZE = 3; // (2~50)流星大小 15  16   private MyPanel panel; 17  18   public MeteorFly() { 19   panel = new MyPanel(); 20   this.getContentPane().add(panel); 21  22   this.setSize(800, 400); // 创建窗体 23   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24   this.setVisible(true); 25   } 26  27   public static void main(String[] args) { 28   new MeteorFly(); 29   } 30  31   class MyPanel extends JPanel implements Runnable { 32  33   Meteor p[]; 34  35   int AppletWidth, AppletHeight; 36  37   BufferedImage OffScreen; 38   Graphics drawOffScreen; 39   Thread pThread; 40  41   public MyPanel() { 42     setBackground(Color.black); //窗体初始化 43     AppletWidth = 800; 44     AppletHeight = 400; 45     p = new Meteor[MAX]; 46     for (int i = 0; i < MAX; i++) 47     p[i] = new Meteor(); 48     OffScreen = new BufferedImage(AppletWidth, AppletHeight, 49       BufferedImage.TYPE_INT_BGR); 50     drawOffScreen = OffScreen.getGraphics(); 51     pThread = new Thread(this); 52     pThread.start(); 53   } 54  55   @Override 56   public void paintComponent(Graphics g) { 57     // TODO Auto-generated method stub 58     super.paintComponents(g); 59     g.drawImage(OffScreen, 0, 0, this); 60   } 61  62   @Override 63   final public void run() { 64     while (true) { 65     // drawOffScreen.clearRect(0, 0, AppletWidth, AppletHeight); // 66     // 清屏 67  68     for (int i = 0; i < MAX; i++) { 69       drawOffScreen.setColor(p[i].color); // RGB颜色 70       drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE); 71       p[i].x += p[i].mx; 72       p[i].y += p[i].my; 73       // if (p[i].x > AppletWidth || p[i].y > AppletHeight) { 74       // p[i].reset(); 75       // } 76  77       int x = p[i].x; 78       int y = p[i].y; 79       int R = p[i].color.getRed(); // 提取颜色 80       int G = p[i].color.getGreen(); 81       int B = p[i].color.getBlue(); 82       while (true) { 83       if (R == 0 && G == 0 && B == 0) { 84         break; 85       } 86       R -= COLORLV; // 尾部颜色淡化 87       if (R < 0) { 88         R = 0; 89       } 90       G -= COLORLV; 91       if (G < 0) { 92         G = 0; 93       } 94       B -= COLORLV; 95       if (B < 0) { 96         B = 0; 97       } 98       Color color = new Color(R, G, B); 99       x -= p[i].mx; // 覆盖尾部100       y -= p[i].my;101       drawOffScreen.setColor(color);102       drawOffScreen.fillOval(x, y, SIZE, SIZE);103       }104       if (x > AppletWidth || y > AppletHeight) { // 流星飞出窗口,重置流星105       p[i].reset();106       }107     }108     repaint();109 110     try {111       Thread.sleep(SLEEP);112     } catch (InterruptedException e) {113       // TODO Auto-generated catch block114       e.printStackTrace();115     }116     }117   }118 119   }120 121   class Meteor { // 流星类122   int x, y; // 流星的位置123   int mx, my; // 下落速度124   Color color; // 流星颜色125 126   public Meteor() {127     reset();128   }129 130   public void reset() {131     int rand = (int) (Math.random() * 100); //随机生成流星出现位置132     if (rand > 35) {133     x = (int) (Math.random() * 600);134     y = 0;135     } else {136     y = (int) (Math.random() * 150);137     x = 0;138     }139     mx = (int) (Math.random() * 2 + 1); //随机生成下落速度和角度140     my = (int) (Math.random() * 2 + 1);141     if (COLOR == null || COLOR.length() == 0) {142     color = new Color(143       // 随机颜色144       (new Double(Math.random() * 128)).intValue() + 128,145       (new Double(Math.random() * 128)).intValue() + 128,146       (new Double(Math.random() * 128)).intValue() + 128);147     } else {148     color = Color.decode(COLOR);149     }150   }151   }152 153 }

本人原创未经允许请勿转载