[Original] java meteor across the sky, original java meteor across

Source: Internet
Author: User

[Original] java meteor across the sky, original java meteor across

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) Number of meteors 11 final int SLEEP = 30; // The Flying speed of the meteor (the greater the value, the slower the speed) 12 final int COLORLV = 2; // (2 ~ 20) COLOR Order (the halo size can be changed) 13 final String COLOR = null; // ("#000000 "~ "# Ffffff") halo color (default color if not specified or null) 14 final int SIZE = 3; // (2 ~ 50) meteor size 15 16 private MyPanel; 17 18 public MeteorFly () {19 panel = new MyPanel (); 20 this. getContentPane (). add (panel); 21 22 this. setSize (800,400); // create Form 23 this. setdefaclocloseoperation (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); // form initialization 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); 59g. 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 // clear screen 67 68 for (int I = 0; I <MAX; I ++) {69 drawOffScreen. setColor (p [I]. color); // RGB color 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 (); // extract the color 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; // tail color fade 87 if (R <0) {88 R = 0; 89} 90G-= COLORLV; 91 if (G <0) {92G = 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; // overwrite the end of the array 100 y-= p [I]. i; 101 drawOffScreen. setColor (color); 102 drawOffScreen. fillOval (x, y, SIZE, SIZE); 103} 104 if (x> AppletWidth | y> AppletHeight) {// reset the meteor 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 {// Meteor class 122 int x, y; // Meteor position 123 int mx, my; // Fall speed 124 Color color; // Meteor Color 125 126 public Meteor () {127 reset (); 128} 129 130 public void reset () {131 int rand = (int) (Math. random () * 100); // randomly generated meteor location 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); // randomly generated fall speed and angle 140 my = (int) (Math. random () * 2 + 1); 141 if (COLOR = null | COLOR. length () = 0) {142 color = new Color (143 // random color 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}

Do not repost my original files without permission

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.