炫酷的代码雨
实现过程:在Java IDE里新建一个名为Rain的java文件
//package chapterAWTandSwing;import javax.swing.*;
import javax.tools.Tool;
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;public class Rain extends JDialog implements ActionListener {private Random random = new Random();private Dimension screenSize;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);//设置光标不可见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);//9键退出KeyPressListener keyPressListener = new KeyPressListener();this.addKeyListener(keyPressListener);//去标题栏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 getChar() {return (char) (random.nextInt(94) + 33);}public void actionPerformed(ActionEvent event) {graphicsPanel.repaint();}public class GraphicsPanel extends JPanel {@Overridepublic 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(getChar()), 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(getChar()),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 {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == KeyEvent.VK_9) {//按9键退出,这里可以随意设置System.exit(0);}}}public static void main(String[] args) {new Rain();}
}
然后运行IDE进行编译,生成class文件,找到class文件目录,新建一个文本文档,写上
@echo off
set CLASSPATH=\%JAVA_HOME%\bin;./bin;
start javaw Rain
然后保存,将后缀名改为.bat,然后拖动到电脑的开机启动目录里(win8.1开机启动目录:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp),即可实现开机启动。