Comment faire fonctionner la méthode getX() de Java Point?


J'essaie de programmer un essaim d'oiseaux en Java pour un projet à l'école. Comme je ne le fais qu'en 2d, j'ai pensé qu'il serait préférable d'utiliser un tableau de Points car il me fournirait les coordonnées x et y. Cependant, lorsque vous essayez d'utiliser les méthodes getX() ou getY() sur un Point, j'obtiens une erreur "Impossible de résoudre la méthode".

C'est ma méthode pour attribuer des positions x et y au tableau Point de mes "oiseaux":

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

Et dans cette méthode je dessine mes "oiseaux":

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

Comme je dit, j'obtiens une erreur" Impossible de résoudre la méthode " sur les méthodes getX() et getY() et je ne vois pas pourquoi. J'ai également essayé d'extraire d'abord un seulPoint du tableau, puis d'utiliser les méthodes dessus, mais j'obtiens la même erreur. Tout ce que j'ai pu trouver était cette question et j'ai appelé la méthode comme ils l'ont fait.

Je suis assez nouveau en Java et en programmation en général, donc toute aide est grandement appréciée. Ceci est mon premier post ici et je suis heureux de faire partie de la communauté, vous m'avez aidé plus que une fois déjà :)

Author: Community, 2016-01-07

1 answers

java.awt.Point#getX/Y retournez double, mais vous attribuez les valeurs à un int.

En supposant que vous avez importé la classe correcte, quelque chose comme

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

Devrait fonctionner très bien

Mise à jour...

Semble fonctionner très bien pour moi...

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

    }

}

Vous n'avez pas votre propre classe Point définie où?

 0
Author: MadProgrammer, 2016-01-07 01:05:18