国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【原創】java流星劃過天空

2019-11-14 15:15:42
字體:
來源:轉載
供稿:網友
  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 }

本人原創未經允許請勿轉載


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白城市| 奉贤区| 启东市| 玉环县| 甘南县| 黑河市| 松滋市| 仁布县| 孟连| 东丽区| 阿巴嘎旗| 克什克腾旗| 永修县| 赣州市| 金秀| 聂荣县| 达拉特旗| 兴义市| 工布江达县| 鲁甸县| 鞍山市| 隆德县| 富蕴县| 车险| 鄯善县| 高青县| 齐齐哈尔市| 静海县| 静乐县| 云安县| 黎川县| 平武县| 三原县| 宁武县| 民和| 鹤庆县| 灵山县| 襄汾县| 岳阳县| 昌平区| 桂平市|