Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rovingdots3;
- import java.awt.Rectangle;
- import java.awt.geom.Rectangle2D;
- import java.util.Random;
- public class Dot {
- private String name = "";
- private int x = 300;
- private int y = 300;
- private int deltaX = 1;
- private int deltaY = 1;
- private int width = 30;
- private int height = 30;
- private int speedX = 1;
- private int speedY = 1;
- private int speedLimit = 12;
- Random rand = new Random();
- long lastGeneration = 0;
- int waitTime = 2000;
- public Dot(String newName, int newX, int newY) {
- name = newName;
- x = rand.nextInt(1200);
- y = rand.nextInt(700);
- deltaX = nonZeroRandom();
- deltaY = nonZeroRandom();
- speedX = nonZeroRandom();
- speedY = nonZeroRandom();
- }
- public String getName() {
- return name;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public int getDeltaX() {
- return deltaX;
- }
- public int getDeltaY() {
- return deltaY;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public int getSpeedX() {
- return speedX;
- }
- public int getSpeedY() {
- return speedY;
- }
- void moveIt(int newW, int newH) {
- x = x + deltaX * speedX;
- y = y + deltaY * speedY;
- if (x > (newW - width)) {
- deltaX = -1;
- speedUp();
- }
- if (x < 0) {
- deltaX = 1;
- speedUp();
- }
- if (y > (newH - height)) {
- deltaY = -1;
- speedUp();
- }
- if (y < 0) {
- deltaY = 1;
- speedUp();
- }
- }
- Rectangle2D getBounds() {
- Rectangle r = new Rectangle(x, y, width, height);
- return r;
- }
- private int nonZeroRandom() {
- int rando = rand.nextInt(3)+1;
- boolean rb = rand.nextBoolean();
- if (rb) {
- rando = 0 - rando;
- }
- return rando;
- }
- boolean generateOK() {
- long currentTime = System.currentTimeMillis();
- if (currentTime - lastGeneration > waitTime) {
- return true;
- }
- return false;
- }
- void changeDirection() {
- x = x - deltaX;
- y = y - deltaY;
- deltaX = 0 - deltaX;
- deltaY = 0 - deltaY;
- }
- void generated() {
- lastGeneration = System.currentTimeMillis();
- }
- private void speedUp() {
- if (speedX <= speedLimit) {
- speedX++;
- speedY++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement