Pure Java code to achieve meteor across the sky _java

Source: Internet
Author: User
Tags clear screen rand

Nonsense not to say more, directly to everyone paste Java code.

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 =;///(~) Number of meteors final int sleep =;//meteor flying speed (the greater the number, the slower the speed) F inal int colorlv =; (~) level (can change halo size) Final String color = null; ("#" ~ "#ffffff") Halo color (default color if not filled or null) final int SIZE =;
   (~) Meteor size private Mypanel panel;
   Public meteorfly () {panel = new Mypanel ();
   This.getcontentpane (). Add (panel); This.setsize (,);
   Create Form 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);//form initialization 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.paintcompone
     NTS (g);
   G.drawimage (offscreen,,, this); @Override final public void run () {while (true) {//Drawoffscreen.clearrect (,, Appletwidth, Applethei Ght); Clear screen for (int i =; i < MAX; i++) {Drawoffscreen.setcolor (P[i].color);//RGB color DRAWOFFSCR
       Een.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 ();
 Extract Color      int G = P[i].color.getgreen ();
       int B = P[i].color.getblue ();
       while (true) {if (R = = && G = = && B = =) {break; } R-= COLORLV;
       Tail color Fade if (R <) {r =;
       } G-= COLORLV;
       if (g <) {G =;
       B-= COLORLV;
       if (b <) {B =;
       Color color = new Color (R, G, B); x = p[i].mx;
       Cover tail Y-= p[i].my;
       Drawoffscreen.setcolor (color);
       Drawoffscreen.filloval (x, y, size, size);
       } if (x > Appletwidth | | | y > Appletheight) {//Meteor fly out window, reset meteor P[i].reset ();
     } repaint ();
     try {thread.sleep (sleep);
     catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();  Class Meteor {//Meteor class int x, y;//meteor position int mx, my;//drop speed color color;//Meteor Color public
   Meteor () {reset ();public void Reset () {int rand = (int) (Math.random () *);//Randomly generated meteor occurrence position if (Rand >) {x = (int) (M
     Ath.random () *);
     y =;
     else {y = (int) (Math.random () *);
     x =; MX = (int) (Math.random () * +);
     Randomly generated drop velocities and angles my = (int) (Math.random () * +); if (COLOR = = NULL | | Color.length () = =) {color = new color (///Random Color (new Double (Math.random () *)). Intvalue () +, (
     New Double (Math.random () *)). Intvalue () +, (new double (Math.random () *)). Intvalue () +);
     else {color = Color.decode (color); }
   }
   }
 }

The above code is this article to you about the pure Java code to achieve a meteor across the sky, I hope this article to share can bring unexpected harvest.

Related Article

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.