0

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 :)

Community
  • 1
  • 1
Sven
  • 17
  • 1
  • 6

1 Answers1

0

java.awt.Point#getX/Y return double, but you're assigning the values to an int.

Assuming your imported the correct class, something like

int x = coordinates[i].x;
int y = coordinates[i].y;

should work just fine

Updated...

Seems to work just fine for me...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Pointy {

    public static void main(String[] args) {
        new Pointy();
    }

    public Pointy() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Point[] points = new Point[10];

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected 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].x;
                int y = coordinates[i].y;
                g.fillOval(x, y, 5, 5);
            }
        }

        public Point[] pointArray() {

            points[0] = new Point(100, 100);
            Random randGen = new Random();

            for (int i = 1; i < points.length; i++) {
                int randX = (randGen.nextInt(5) - 2);
                int randY = (randGen.nextInt(5) - 2);
                points[i] = new Point(100 + randX, 100 + randY);
            }
            return points;
        }

    }

}

Don't have you're own Point class defined some where?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Sorry, i forgot to include that. I also tried to cast the value to an int and tried your recommendation but I get the same error. – Sven Jan 07 '16 at 00:57
  • Do you `import java.awt.Point`? – MadProgrammer Jan 07 '16 at 00:59
  • Oh damn, that actually worked. I thought when using the * when importing it imports all the classes. Thanks a bunch for that! – Sven Jan 07 '16 at 01:05
  • Just did a runnable example, which works just fine for me. Don't have you're own `Point` class defined somewhere? – MadProgrammer Jan 07 '16 at 01:05
  • Sorry for causing you so much effort over nothing. Thanks again! But do you know why `import java.awt.*` won't work as it should contain all classes? – Sven Jan 07 '16 at 01:15