package dtmgame; import java.awt.Graphics; import java.awt.Point; /** * @author Shaohan Hu * */ public class CircleParticle { private static final long serialVersionUID = 1L; private int x, y; private int r; private int vx, vy; private int left, right, top, bottom; private int rvx, rvy; private int randomMagnitude = 1; public CircleParticle(int x, int y, int r, int vx, int vy, int left, int right, int top, int bottom) { this.x = x; this.y = y; this.r = r; this.vx = vx; this.vy = vy; this.left = left; this.right = right; this.top = top; this.bottom = bottom; } public Point getPosition() { return new Point(x, y); } public void takeOneStep() { x += (vx + rvx); y += (vy + rvy); this.updateDirections(); } public void updateDirections() { if (Math.random() >= 0.5) { rvx = randomMagnitude; } else { rvx = randomMagnitude * (-1); } if (Math.random() >= 0.5) { rvy = 3; } else { rvy = randomMagnitude * (-1); } if (x + vx + rvx <= left || x + vx + rvx >= right) { vx += rvx; vx *= -1; } if (y + vy <= top || y + vy >= bottom) { vy += rvy; vy *= -1; } } public void drawParticle(Graphics g) { g.fillOval(x - r, y - r, 2 * r, 2 * r); } }