简体   繁体   中英

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM