代碼雨

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();

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 方法1 (數(shù)據(jù)類型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math...
    GB_speak閱讀 41,411評(píng)論 2 6
  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    開心的鑼鼓閱讀 3,404評(píng)論 0 9
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會(huì)聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,304評(píng)論 0 9
  • #早餐日記# 包子?粽子?小米粥?咸鴨蛋?水果 周一早安 新周順利 ???
    晨晨媽媽_8882閱讀 95評(píng)論 0 0

友情鏈接更多精彩內(nèi)容