RunnableGBall
Retired DISLab
public class RunnableGBall extends GBall implements Runnable { /** Creates a new ball with radius r centered at the origin */ public RunnableGBall(double r) { super(r); } public void setEnclosureSize(double width, double height) { enclosureWidth = width; enclosureHeight = height; } public void setVelocity(double vx, double vy) { dx = vx; dy = vy; } @Override public void run() { // TODO Auto-generated method stub while (true) { advanceOneTimeStep(); pause(PAUSE_TIME); } } private void advanceOneTimeStep() { double bx = getX(); double by = getY(); double r = getWidth() / 2; if (bx < r || bx > enclosureWidth - r) dx = -dx; if (by < r || by > enclosureHeight - r) dy = -dy; move(dx, dy); } /* Private constants */ private static final int PAUSE_TIME = 20; /* Private instance variables */ private double enclosureWidth; private double enclosureHeight; private double dx; private double dy; }