java实现矩阵雨

java 实现矩阵雨


Rain.java

package com.chenlin;

import javax.swing.*;
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.Random;

/**
 * 矩阵雨
 *
 * @author chenlin
 */
public class Rain extends JDialog implements ActionListener {

    // 随机数
    private Random random = new Random();
    //封装了一个构件的高度和宽度,表明有多少个像素点
    private Dimension screenSize;

    /**
     * JPanel:面板组件,非顶层容器。
     *
     * 一个界面只可以有一个JFrame窗体组件,但是可以有多个JPanel面板组件,
     * 而JPanel上也可以使用FlowLayout,BorderLayout,GridLayout等各种布局管理器,
     * 这样可以组合使用,达到较为复杂的布局效果
     */
    private JPanel graphicsPanel;

    // 行高,列宽
    private final static int gap =20;
    // 存放雨点顶部的位置信息(marginTop)
    private int[] posArr;
    // 行数
    private int lines;
    //列数
    private int columns;

    public Rain(){
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        graphicsPanel = new GraphicsPanel();
        add(graphicsPanel,BorderLayout.CENTER);
        // 设置光标不可见
        // 此类是 Abstract Window Toolkit 的所有实际实现的抽象超类。
        // Toolkit 的子类被用于将各种组件绑定到特定本机工具包实现。
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        
        /**
         * Image是一个抽象类,BufferedImage是其实现类,是一个带缓冲区图像类,
         * 主要作用是将一幅图片加载到内存中(BufferedImage生成的图片在内存里有一个图像缓冲区,
         * 利用这个缓冲区我们可以很方便地操作这个图片),提供获得绘图对象、图像缩放、选择图像平滑度等功能,
         * 通常用来做图片大小变换、图片变灰、设置透明不透明等
         * 
         */
        Image image = defaultToolkit.createImage(new MemoryImageSource(0,0,null,0,0));
        
        //Cursor是封装鼠标光标的位图表示形式的类
        Cursor invisibleCuror = defaultToolkit.createCustomCursor(image,new Point(0,0),"curor");
        setCursor(invisibleCuror);
        //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(100,this).start();

    }


    /**
     * @return 随机字符
     */
    private char getChr(){
        return (char) (random.nextInt(94)+33);
    }

    @Override
    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];
                g2d.setColor(Color.CYAN);
                g2d.drawString(String.valueOf(getChr()),x,endPos * gap);
                int cg = 0;
                for (int j = endPos-15; j < endPos; j++) {
                    //颜色渐变
                    cg += 20;
                    if(cg> 255){
                        cg = 255;
                    }
                    g2d.setColor(new Color(0,cg,0));
                    g2d.drawString(String.valueOf(getChr()),x,j * gap);
                }
                //每放完一帧,当前列上雨点的位置随机下移1-5行
                posArr[currentColumn] += random.nextInt(5);
                //当雨点位置你超过屏幕高度是,重新产生一个随机位置
                if(posArr[currentColumn] * gap > getHeight()){
                    posArr[currentColumn] = random.nextInt(lines);
                }
                currentColumn++;
            }

        }
    }

    private class KeyPressListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
                System.exit(0);
            }
        }
    }
}


App.java

public static void main( String[] args ) {
       // new Rain();
        //new Login().initUI();
    }

Login.java

package com.chenlin;

/**
 * 登录
 *
 * @author chenglin
 * @date 2019/9/9 15:20
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//1.定义Login类,
public class Login {

    // 1.在类中定义初始化界面的方法;
    public void initUI() {
        // 3.在initUI方法中,实例化JFrame类的对象。
        final JFrame frame = new JFrame();
        // 4.设置窗体对象的属性值:标题、大小、显示位置、关闭操作、布局、禁止调整大小、可见、...
        frame.setTitle("Login");// 设置窗体的标题
        frame.setSize(400, 180);// 设置窗体的大小,单位是像素
        frame.setDefaultCloseOperation(3);// 设置窗体的关闭操作;3表示关闭窗体退出程序;2、1、0
        frame.setLocationRelativeTo(null);// 设置窗体相对于另一个组件的居中位置,参数null表示窗体相对于屏幕的中央位置
        frame.setResizable(false);// 设置禁止调整窗体大小

        // 实例化FlowLayout流式布局类的对象,指定对齐方式为居中对齐,组件之间的间隔为5个像素
        FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);
        // 实例化流式布局类的对象
        frame.setLayout(fl);

        // 5.实例化元素组件对象,将元素组件对象添加到窗体上(组件添加要在窗体可见之前完成)。
        // 实例化ImageIcon图标类的对象,该对象加载磁盘上的图片文件到内存中,这里的路径要用两个
        //ImageIcon icon = new ImageIcon("C:\\Desktop\\蓝杰学习\\材料\\爱晚亭.gif");
        // 用标签来接收图片,实例化JLabel标签对象,该对象显示icon图标
        //JLabel labIcon = new JLabel(icon);
        //设置标签大小
        //labIcon.setSize(30,20);setSize方法只对窗体有效,如果想设置组件的大小只能用
        //Dimension dim = new Dimension(400,300);
        //labIcon.setPreferredSize(dim);
        // 将labIcon标签添加到窗体上
        //frame.add(labIcon);


        // 实例化JLabel标签对象,该对象显示"账号:"
        JLabel labName = new JLabel("账号:");
        // 将labName标签添加到窗体上
        frame.add(labName);

        // 实例化JTextField标签对象
        final JTextField textName = new JTextField();
        Dimension dim1 = new Dimension(300,30);
        //textName.setSize(dim);//setSize这方法只对顶级容器有效,其他组件使用无效。
        textName.setPreferredSize(dim1);//设置除顶级容器组件其他组件的大小
        // 将textName标签添加到窗体上
        frame.add(textName);

        //实例化JLabel标签对象,该对象显示"密码:"
        JLabel labpass= new JLabel("密码:");
        //将labpass标签添加到窗体上
        frame.add(labpass);


        //实例化JPasswordField
        final JPasswordField textword=new JPasswordField();
        //设置大小
        textword.setPreferredSize(dim1);//设置组件大小
        //添加textword到窗体上
        frame.add(textword);

        //实例化JButton组件
        JButton button=new JButton();
        //设置按钮的显示内容
        Dimension dim2 = new Dimension(100,30);
        button.setText("登录");
        //设置按钮的大小
        button.setSize(dim2);
        frame.add(button);

        frame.setVisible(true);// 设置窗体为可见

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String trim = textName.getText().trim();
                String text = textword.getText().trim();
                if ("root".equals(trim) && "123".equals(text)){
                    System.out.println("账号: "+trim);
                    System.out.println("密码: "+text);
                    System.exit(0);     
                }
            }
        });
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值