JAVA流星雨


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.security.Key;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rain4 {

    public static void main(String[] args) {
        JFrame frame = new JFrame();          //实例化窗体对象frame
        Pool pool = new Pool();               //容器
        frame.setUndecorated(true);           //启用装饰
        frame.add(pool);                      //添加容器到对象 
        frame.setVisible(true);               //显示窗口  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
        frame.setSize(1920, 1080);            //宽,高
        frame.setResizable(false);            //不可改变窗口大小
        frame.setBackground(Color.black);     //背景黑色
        frame.setLocationRelativeTo(null);    //居中
        frame.setOpacity(1.0f);               // 设置窗体透明度
        frame.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new ImageIcon("/images/back.png").getImage(),     //鼠标样式
                new Point(10, 20), "stick"));
        frame.addKeyListener(new KeyListener() {//键盘监听

            public void keyPressed(KeyEvent e) {//键盘事件

                if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_SPACE) {                            //ESC或空格见退出  
                    System.exit(0);
                }
            }

            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
        });
        pool.action();
    }
}

@SuppressWarnings("serial")
class Pool extends JPanel {
    Random r = new Random();
    Rain[] str;

    public Pool() {
        str = new Rain[200];
        for (int i = 0; i < str.length; i++) {
            str[i] = new Rain();

        }
    }

    public void paint(Graphics g) {
        ImageIcon back = new ImageIcon("images/back.jpg");// 背景图
        g.drawImage(back.getImage(), 0, 0, getWidth(), getHeight(), null);

        // BufferedImage image = new BufferedImage(1920, 1080,
        // BufferedImage.TYPE_INT_RGB);
        // g.drawImage(image, 0, 0, null);
        for (int i = 0; i < str.length; i++) {
            Font f = new Font("微软雅黑", Font.PLAIN, 2);
            g.setFont(f);
            g.setColor(str[i].color);
            g.drawLine(str[i].x, str[i].y, str[i].x, str[i].y - r.nextInt(50));

        }
    }

    public void action() {

        for (int i = 0; i < str.length; i++) {
            str[i].start();
        }
        while (true) {
            repaint();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

class Rain extends Thread {
    Random r = new Random();
    int x, y, e, g, b, step;
    Color color;

    public Rain() {
        y = r.nextInt(1000);
        x = r.nextInt(1920);
        step =100;// 防止移动距离为零

    }

    public void move() {
    step-=1;
        if(step<2){
            step +=200;
       }

        if (r.nextInt(5) <2) {
              y += step;
               if (y >= 1080) {
                   color = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
                   y = 0;
                   x = r.nextInt(1920);
            }
        } else {
                if (x > 1920) {

                color = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
                x = 0;
            }
        }
    }

    public void run() {
        while (true) {


            move();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    }
}

JAVA流星雨_第1张图片

你可能感兴趣的:(JAVA流星雨)