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;
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 = 20;
? ? //存放雨點(diǎn)頂部的位置信息(marginTop)
? ? private int[] posArr;
? ? //行數(shù)
? ? private int lines;
? ? //列數(shù)
? ? private int columns;
? ? public Rain() {
? ? ? ? initComponents();
? ? }
? ? private void initComponents() {
? ? ? ? setLayout(new BorderLayout());
? ? ? ? graphicsPanel = new GraphicsPanel();
? ? ? ? add(graphicsPanel, BorderLayout.CENTER);
? ? ? ? //設(shè)置光標(biāo)不可見
? ? ? ? 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);
? ? ? ? //去標(biāo)題欄
? ? ? ? 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 隨機(jī)字符
? ? */
? ? 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);
? ? ? ? ? ? //當(dāng)前列
? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //每放完一幀,當(dāng)前列上雨點(diǎn)的位置隨機(jī)下移1~5行
? ? ? ? ? ? ? ? posArr[currentColumn] += random.nextInt(5);
? ? ? ? ? ? ? ? //當(dāng)雨點(diǎn)位置超過屏幕高度時(shí),重新產(chǎn)生一個(gè)隨機(jī)位置
? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? new Rain();
? ? }
}