marble.java
来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 57 行
JAVA
57 行
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(40);
}
catch (InterruptedException ie)
{
System.err.println("Thread interrupted");
}
table.repaint();
}
}
public void move()
{
x += xdir;
y += ydir;
if ((x > table.getSize().width) || (x < 0))
xdir *= (- 1);
if ((y > table.getSize().width) || (y < 0))
ydir *= (- 1);
}
public void draw (Graphics g)
{
g.setColor(Color.cyan);
g.fillOval(x, y, 20, 20);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?