代码雨【code rain】 cmd 命令快速实现 + java 实现

1 保存 .bat 批处理文件

2 核心代码  参考

@echo off
title AHHHHHH
color 02
echo This Matrix Affect was created by Superman
echo This program will shut down your computer!
echo The Matrix Effect begins in
ping -n 2 127.0.0.1>nul
echo 5
ping -n 2 127.0.0.1>nul
echo 4
ping -n 2 127.0.0.1>nul
echo 3
ping -n 2 127.0.0.1>nul
echo 2
ping -n 2 127.0.0.1>nul
echo 1

:superman
echo %random%%random%%random%%random%%random%%random%%random%%random%
goto superman

 

java 实现【每个雨滴头部变化,尾巴不变】

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
import java.util.*;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Timer;
 
public class Rain extends JDialog implements ActionListener {
 
    private Random random = new Random();
    private Dimension screenSize;
    private JPanel graphicsPanel;
    //行高,列宽
    private final static int gap = 15;
    //存放雨点顶部的位置信息(marginTop)
    private int[] posArr;
    //行数
    private int lines;
    //列数
    private int columns;

    private String[] xiaodaka = new String[]{"小","打","卡"};

    Map<Integer, java.util.List<String>> page = Maps.newHashMap();


    public Rain() {
        initComponents();
    }
 
    private void initComponents() {
        setLayout(new BorderLayout());
        graphicsPanel = new GraphicsPanel();
        add(graphicsPanel, BorderLayout.CENTER);
        //设置光标不可见
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Image image = defaultToolkit.createImage(new MemoryImageSource(0, 0, null, 0, 0));
        Cursor invisibleCursor = defaultToolkit.createCustomCursor(image, new Point(0, 0), "cursor");
        setCursor(invisibleCursor);
        //ESC键退出
        KeyPressListener keyPressListener = new KeyPressListener();
        this.addKeyListener(keyPressListener);
        //this.setAlwaysOnTop(true);
        //去标题栏
        this.setUndecorated(true);
        //全屏
        this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(true);
 
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        lines = screenSize.height / gap;
        columns = screenSize.width / gap;
 
        posArr = new int[columns + 1];
        random = new Random();
        for (int i = 0; i < posArr.length; i++) {
            posArr[i] = random.nextInt(lines);
        }
 
        //每秒10帧
        new Timer(50, this).start();
    }
    

    private String getStr() {
        return xiaodaka[(random.nextInt(3) )];
    }
 
    public void actionPerformed(ActionEvent e) {
        graphicsPanel.repaint();
    }
 
    private class GraphicsPanel extends JPanel {
        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setFont(getFont().deriveFont(Font.BOLD));
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, screenSize.width, screenSize.height);
            //当前列
            int currentColumn = 0;
            for (int x = 0; x < screenSize.width; x += gap) {
                int endPos = posArr[currentColumn];
                List<String> strings = page.get(currentColumn);
                g2d.setColor(Color.CYAN);
                g2d.drawString(String.valueOf(getStr()), x, endPos * gap);
                int cg = 0;
                int charIndex = 0;
                for (int j = endPos - 10; j < endPos; j++) {
                    //颜色渐变
                    cg += 20;
                    if (cg > 255) {
                        cg = 255;
                    }
                    g2d.setColor(new Color(0, cg, 0));
                    if(strings == null) {
                        strings = Lists.newArrayList();
                        strings.add(String.valueOf(getStr()));
                        g2d.drawString(strings.get(charIndex++), x, j * gap);
                        page.put(currentColumn,strings);
                    }else if(strings.size() > charIndex){
                        g2d.drawString(strings.get(charIndex++), x, j * gap);
                    }else{
                        strings.add(String.valueOf(getStr()));
                        g2d.drawString(strings.get(charIndex++), x, j * gap);
                    }


                }
                //每放完一帧,当前列上雨点的位置随机下移1~5行
                posArr[currentColumn] += random.nextInt(5);
                //当雨点位置超过屏幕高度时,重新产生一个随机位置
                if (posArr[currentColumn] * gap > getHeight()) {
                    posArr[currentColumn] = random.nextInt(lines);
                    page.put(currentColumn,null);
                }
                currentColumn++;
            }
        }
    }
 
    private class KeyPressListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
        }
    }
 
    public static void main(String[] args) {
        new Rain();
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页