I'm trying to program a swarm of birds in Java for a project in school. As I'm only doing this in 2d, I thought it would be best to use an array of Points
as it would provide me with x and y coordinates.
However, when trying to use the getX()
or getY()
methods on a Point
, I get a "Cannot resolve method" error.
This is my method to assign x and y positions to the Point
array of my "birds":
public Point[] pointArray() {
points[0] = new Point(100, 100);
Random randGen = new Random();
for (int i = 1; i < points.length; i++){
randX = (randGen.nextInt(5)-2);
randY = (randGen.nextInt(5)-2);
points[i] = new Point(100+randX, 100+randY);
}
return points;
}
And in this method I draw my "birds":
public void paintComponent(Graphics g) {
super.paintComponent(g);
Point[] coordinates = pointArray();
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.WHITE);
for(int i = 0; i<coordinates.length; i++ ) {
int x = coordinates[i].getX();
int y = coordinates[i].getY();
g.fillOval(x, y, 5, 5);
}
}
Like I said, I get a "Cannot resolve method" error on the getX()
and getY()
methods and I can't see why. I also tried to first extract a singlePoint
of the array and then use the methods on it but I get the same error. All i could find was this question and I called the method like they did.
I'm pretty new to Java and programming in general, so any help is greatly appreciated. This is my first post here and I'm glad to be part of the community, you guys helped me out more than once already :)