Pure Java code to achieve meteor across the sky

  • 2020-04-01 04:19:25
  • OfStack

Without further ado, I will post the Java code directly to you.


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 = ; //The speed of the meteor (the higher the value, the slower the speed)
   final int COLORLV = ; //(~) color level (can change the halo size)
   final String COLOR = null; //("#"~"# FFFFFF ") halo color (default 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 a 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.paintComponents(g);
     g.drawImage(OffScreen, , , this);
   }
   @Override
   final public void run() {
     while (true) {
     // drawOffScreen.clearRect(, , AppletWidth, AppletHeight); //
     //Clear the screen
     for (int i = ; i < MAX; i++) {
       drawOffScreen.setColor(p[i].color); //RGB color
       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(); //Extraction of color
       int G = p[i].color.getGreen();
       int B = p[i].color.getBlue();
       while (true) {
       if (R == && G == && B == ) {
         break;
       }
       R -= COLORLV; //Tail fading
       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 the tail
       y -= p[i].my;
       drawOffScreen.setColor(color);
       drawOffScreen.fillOval(x, y, SIZE, SIZE);
       }
       if (x > AppletWidth || y > AppletHeight) { //Meteor flies out of 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 location
   int mx, my; //fall
   Color color; //Meteor color
   public Meteor() {
     reset();
   }
   public void reset() {
     int rand = (int) (Math.random() * ); //Randomly generate the location of the meteor
     if (rand > ) {
     x = (int) (Math.random() * );
     y = ;
     } else {
     y = (int) (Math.random() * );
     x = ;
     }
     mx = (int) (Math.random() * + ); //Random generation of falling velocity and Angle
     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 the article to tell you about the pure Java code to achieve the meteor across the sky, I hope this article can bring you unexpected gains.


Related articles: