Represent eclipse coordinates

0

I'm trying to draw a series of points (coordinates) in eclipse, these coordinates are read through two ArrayList, but I have no idea to represent them in the JPanel, I leave some code:

MAIN

public class Servidor extends JFrame {

private static ArrayList<Integer> xPasos=new ArrayList<Integer>();
private static ArrayList<Integer> yPasos=new ArrayList<Integer>();

public static void main(String args[]){
        Servidor fondo= new Servidor();
        fondo.setSize(380,456);
        fondo.setVisible(true);
        TestPane p=new TestPane("/imagenes/planta2.png");
        fondo.add(p);

Class with the JPanel

public class TestPane extends JPanel {
public void paintComponent(Graphics g){
    Dimension d=getSize();
    imagen=new ImageIcon(getClass().getResource(url));
    g.drawImage(imagen.getImage(),0,0,d.width,d.height, null);
    setOpaque(false);

    //Dibujar la coordenada en un sitio exacto
    g.setColor(Color.red);
    g.fillOval(x, y, SIZE, SIZE);
    g.drawString("Punto de partida:("+x+", "+y+")", 5, 15);

    //Intento de dibujar las coordenadas en un sitio exacto
    g.setColor(Color.green);    
    for(int i=0;i<xPasos.size();i++){
        g.fillOval(xPasos.get(i), yPasos.get(i), 3,3);

    super.paintComponent(g);
}                    
    }

That is, my goal is to pass the values of the ArrayList (which have values even if it is not visible in the code) to the class with the JPanel to draw them, if code is included I would appreciate it a lot

Greetings

    
asked by Antonio 06.03.2018 в 16:13
source

1 answer

1

This can be done in several ways, but I think the simplest one is to use the panel builders that you have defined. There you pass the two lists and verify that they are the same size and that they are not null or empty. If everything is correct, you save them in the attributes to print them later.

Here is an example of code that does what you ask (your class TestPane I have called PointsPanel ).

public class PointsPanel extends JPanel {

    private Set<Point> puntos = new HashSet<>();

    public PointsPanel(List<Integer> x, List<Integer> y) {
        if (x != null && y != null) {

            if (x.size() != y.size())
                throw new IllegalArgumentException();

            if (!x.isEmpty()) {
                Integer[] abscisas = new Integer[x.size()];
                Integer[] ordenadas = new Integer[y.size()];

                x.toArray(abscisas);
                y.toArray(ordenadas);

                for (int i = 0; i < abscisas.length; i++)
                    puntos.add(new Point(abscisas[i], ordenadas[i]));
            }

        }
    }

    /* Resto de la clase */

}

In the constructor I accept two lists, but I convert them to a set of points ( Set<Point> ) to have in each element of the set the two coordinates, and to avoid repeated points.

@Override
public void paint(Graphics g) {
    if (puntos == null || puntos.isEmpty()) return;
    for (Point p : puntos) {
        g.fillOval(p.x, p.y, 3, 3);
    }
}

The paint(Graphics g) method would work in the same way (I have simplified it so as not to stretch too long) that your paintComponent(Graphics g) method.

    
answered by 06.03.2018 / 19:14
source