⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 这是《Java 2 简明教程(第2版)》一书配套的源代码。
💻 JAVA
字号:
import java.awt.*;

class Main extends Frame implements Runnable {
    int dotSize = 15;

    Main() {
        super("setColor Example");
        setSize(150, 150);
        show();
        (new Thread(this)).start();
    }

    // Returns an integer in the range [0..r-1].
    int random(int r) {
        return (int)(Math.floor(Math.random()*r));
    }

    public void update(Graphics g) {
        Insets insets = this.getInsets();
        int w = getSize().width-insets.left-insets.right;
        int h = getSize().height-insets.top-insets.bottom;

        for (int i=0; i<10; i++) {
            int x = random(w), y = random(h);
            g.setColor(new Color(random(256), random(256), random(256)));
            g.fillOval(x-x%dotSize, y-y%dotSize, dotSize, dotSize);
        }
    } 

    public void run() {
        while (true) {
            try {Thread.sleep(100);} catch (Exception e) {}
            repaint();
        }
    }

    static public void main(String[] args) {
        new Main();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -