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

📄 marble.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.awt.*;

public class Marble
        extends Thread {
    private int xdir = 2 * (1 - 2 * (int) Math.round(Math.random()));
    private int ydir = 2 * (1 - 2 * (int) Math.round(Math.random()));
    private boolean running = false;
    private Table table = null;
    protected int x, y;
    public Marble(Table _table, int _x, int _y) {
        table = _table;
        x = _x;
        y = _y;
        start();
    }

    public void start() {
        running = true;
        super.start();
    }

    public void halt() {
        running = false;
    }

    public void run() {
        while (running) {
            move();
            try {
                sleep(50);
            }
            catch (InterruptedException ie) {
                System.err.println("Thread interrupted");
            }
            table.repaint();
        }
    }

    public void draw(Graphics g) {
        g.setColor(Color.black);
        g.fillOval(x, y, 20, 20);
    }

    private void move() {
        x += xdir;
        y += ydir;
        if ( (x > table.getSize().width) || (x < 0)) {
            xdir *= ( -1);
        }
        if ( (y > table.getSize().height) || (y < 0)) {
            ydir *= ( -1);
        }
    }
}

⌨️ 快捷键说明

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