如何让 Java 点的 getX() 方法起作用?

How do I get the getX() method of Java Point to work?

我正试图在 Java 中为学校的一个项目编写一群鸟类。因为我只在 2d 中这样做,所以我认为最好使用 Points 数组,因为它会为我提供 x 和 y 坐标。 但是,当尝试在 Point 上使用 getX()getY() 方法时,出现 "Cannot resolve method" 错误。

这是我将 x 和 y 位置分配给 "birds" 的 Point 数组的方法:

    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;
}

并且在这个方法中我绘制了我的 "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);
    }
    }

就像我说的,我在 getX()getY() 方法上遇到 "Cannot resolve method" 错误,但我不明白为什么。我还尝试先提取数组的单个 Point,然后使用其上的方法,但我得到了同样的错误。我能找到的只有 this question,我像他们一样调用了这个方法。

我对 Java 和一般编程还很陌生,所以非常感谢您的帮助。 这是我第一次 post 来到这里,我很高兴成为社区的一员,你们已经不止一次帮助我了:)

java.awt.Point#getX/Y return double,但您将值分配给 int.

假设您导入了正确的 class,例如

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

应该可以正常工作

已更新...

对我来说似乎工作得很好...

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;
        }

    }

}

没有你自己的 Point class 定义了一些在哪里?