RandomPoint.java
1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.awt.Color;
/**
* RandomPoint class is similar to a point with a color. It stores points,
* it is used for the random drawing method.
**/
public class RandomPoint
{
private Color c;
private int x,y,width,height;
/** Creates a default RandomPoint. **/
public RandomPoint(){}
/** Creates a RandomPoint and takes in the screen sizes width and height. **/
public RandomPoint(int width, int height)
{
// takes in variables and then randomly generates a starting x
// and y location
this.width = width;
this.height = height;
x = (int)(Math.random()*width);
y = (int)(Math.random()*height);
}
/** Figures out the next x location. **/
public int nextX()
{
// checks if its a negative of a positive number then generates a random
// point up to 50 places away, if its out of bounds then it returns to
// the bounds
int sign = (int)(Math.random()*2);
if(sign == 0)
x = x + (int)(Math.random()*50+1);
else
x = x - (int)(Math.random()*50+1);
if(x < 0)
x = 0;
if(x > width)
x = width;
return x;
}
/** Figures out the next y location. **/
public int nextY()
{
// checks if its a negative of a positive number then generates a random
// point up to 50 places away, if its out of bounds then it returns to
// the bounds
int sign = (int)(Math.random()*2);
if(sign == 0)
y = y + (int)(Math.random()*200+1);
else
y = y - (int)(Math.random()*200+1);
if(y < 0)
y = 0;
if(y > height)
y = height;
return y;
}
/** This method returns a random color. **/
public Color getColor()
{
return new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
}
/** This method returns the x value. **/
public int getX(){return x;}
/** This method returns the y value. **/
public int getY(){return y;}
}