package dtmgame; import java.awt.Graphics; import java.awt.Point; /** * @author Shaohan Hu * */ public class SquareParticle { private static final long serialVersionUID = 1L; private int x, y; private int width, height; private int vx, vy; private int left, right, top, bottom; private int rvx, rvy; private int randomMagnitude = 1; public SquareParticle(int x, int y, int width, int height, int vx, int vy, int left, int right, int top, int bottom) { this.x = x; this.y = y; this.width = width; this.height = height; 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 + width >= right) { vx += rvx; vx *= -1; } if (y + vy <= top || y + vy + height >= bottom) { vy += rvy; vy *= -1; } } public void drawParticle(Graphics g) { g.fillRect(x, y, width, height); } }