Access data from a class

2

I'm developing a graph generator to monitor the navigation of a robot. The data of the coordinates by which the robot passes are stored in a txt file, with each line having a latitude and a longitude, as described in that question . I'm using the @VictorStafusa example

But I'm having trouble accessing data from class Coordenada .

To plot a line in my graph, I use the latitude and longitude of two coordinates. Therefore, each object must have the coordinates of a line of the txt. The code I use to plot a line is linha.drawLine(longitudeCoordenada1, latitudeCoordenada1, longitudeCoordenada2, latitudeCoordenada2) . I then thought of instantiating objects as follows Coordenada c = new Coordenada() , to use: linha.drawLine(c.getLongitude(), c.getLatitude(), c2.getLongitude(), c2.getLatitude()) . But I did not succeed.

I have little object-oriented experience, so could you tell me how to do each object have the coordinate of a txt line and how to access those coordinates?

To test I'm using the following main class.

public class Main {

    public static void main(String[] args) throws IOException {
        double longitude1 = 0;
        double latitude1 = 0;
        double longitude2 = 0;
        double latitude2 = 0;

        Leitura l = new Leitura();
        l.lerCoordenadas();

        Coordenada c1 = new Coordenada(longitude1, latitude1);
        Coordenada c2 = new Coordenada(longitude2, latitude2);

        System.out.println(c1.getLongitude());
        System.out.println(c1.getLatitude());
        System.out.println(c2.getLongitude());
        System.out.println(c2.getLatitude());
    }
}
    
asked by anonymous 07.06.2018 / 21:57

1 answer

2

Use the parameters in the constructor:

double longitude1 = ...;
double latitude1 = ...;
double longitude2 = ...;
double latitude2 = ...;

Coordenada c1 = new Coordenada(longitude1, latitude1);
Coordenada c2 = new Coordenada(longitude2, latitude2);

linha.drawLine(c1.getLongitude(), c1.getLatitude(), c2.getLongitude(), c2.getLatitude());

Edited:

Once you have edited the question, the problem is that you are reading the values from the file and are not doing anything with them. Note that the lerCoordenadas() method returns a List<Coordenada> . However, you are completely ignoring the produced list, and instead you create two zero-value coordinates that are totally independent of the list you read from the file and then display those zero-value coordinates.

Try this:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class Main {

    private static List<Coordenada> lerCoordenadas() throws IOException {
        return lerCoordenadas(Paths.get("c:/dados/log.txt"));
    }

    private static List<Coordenada> lerCoordenadas(Path arquivo) throws IOException {
        return Files.readAllLines(arquivo, StandardCharsets.UTF_8)
               .stream()
               .map(Coordenada::parse)
               .collect(Collectors.toList());
    }

    public static void main(String[] args) throws IOException {
        List<Coordenada> coordenadas = lerCoordenadas();

        for (Coordenada c : coordenadas) {    
            System.out.println(c.getLongitude());
            System.out.println(c.getLatitude());
        }
    }
}

The Coordenada class is the same as the my answer to your previous question . The Leitura class of the previous question can be discarded because this Main class has already absorbed the contents of it.

To draw a polygon with these coordinates, you would do something like this:

public void desenhar(List<Coordenada> coordenadas) {
    Coordenada anterior = null;
    for (Coordenada c : coordenadas) {
        if (anterior != null) {
            linha.drawLine(anterior.getLongitude(), anterior.getLatitude(), c.getLongitude(), c.getLatitude());
        }
        anterior = c;
    }
    Coordenada primeira = coordenadas.get(0);
    linha.drawLine(anterior.getLongitude(), anterior.getLatitude(), primeira.getLongitude(), primeira.getLatitude());
}

Of course, for this to work, you need to know what exactly this linha object is.

    
07.06.2018 / 22:04