I am writing a simple application that draws the NYC subway map. I have succeeded in doing so and my maps prints in JFrame object. However, it prints on the wrong side because I assume it references the 0 0 point on the top left corner. How can I make it reference the bottom left corner so it will print the right way?

private final List<Shape> shapes;
private double maxLat = 40.903125;
private double maxLon = -73.755405;
private double minLat = 40.512764;
private double minLon = -74.251961;
private final double latLength;
private final double lonLength;

public Shapes() throws IOException {
    this.shapes = new ArrayList<Shape>();

    CSVReader in = new CSVReader(new FileReader(
            "src/charnetskaya/subwaymap/shapes.txt"));
    String[] line;
    in.readNext();
    while ((line = in.readNext()) != null) {
        double lat = Double.valueOf(line[1]);
        double lon = Double.valueOf(line[2]);
        Shape shape = new Shape(line[0], lat, lon);
        shapes.add(shape);

        this.maxLat = Math.max(this.maxLat, shape.getLat());
        this.maxLon = Math.max(this.maxLon, shape.getLon());
        this.minLat = Math.min(this.minLat, shape.getLat());
        this.minLon = Math.min(this.minLon, shape.getLon());
    }

    this.latLength = Math.abs(this.maxLat - this.minLat);
    this.lonLength = Math.abs(this.maxLon - this.minLon);
    System.out.println(latLength + " " + lonLength);

}

graphics method

public void paintComponent(Graphics pen) {
    System.out.println("Tring to draw");
    Graphics2D pen2D = (Graphics2D) pen;
    int width = getWidth();
    int height = getHeight();
    System.out.println(width + " | " + height);

    double minLat = this.shapes.getMinLat();
    double minLon = this.shapes.getMinLon();
    double latLength = this.shapes.getLatLength();
    double lonLength = this.shapes.getLonLength();
    List<String> shapeIds = this.shapes.getShapeIds();
    for (String shapeId : shapeIds) {

        List<Shape> list = this.shapes.getShapes(shapeId);

        Trip trip = this.trips.getTrip(shapeId);
        if (trip != null) {
            Color color = this.routes.getColor(trip.getRouteId());
            pen2D.setColor(color);
            for (int i = 1; i < list.size(); i++) {
                Shape a = list.get(i - 1);
                Shape b = list.get(i);
                int x1 = (int) ((a.getLat() - minLat) / latLength * height);
                int y1 = (int) ((a.getLon() - minLon) / lonLength * height);
                int x2 = (int) ((b.getLat() - minLat) / latLength * height);
                int y2 = (int) ((b.getLon() - minLon) / lonLength * height);

                // if ((x1 != x2) || (y1 != y2)) {
                pen2D.drawLine(x1, y1, x2, y2);
                // }
            }
        }

    }

Picture of my disaster NYC subway map

enter image description here enter image description here

有帮助吗?

解决方案

Change

int x1 = (int) ((a.getLat() - minLat) / latLength * height);
int y1 = (int) ((a.getLon() - minLon) / lonLength * height);

to

int x1 = (int) ((a.getLon() - minLon) / lonLength * width);
int y1 = (int) ((maxLat - a.getlat()) / latLength * height);

and similarly for x2 and y2.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top