在JFrame中更改方向

时间:2014-03-12 20:40:02

标签: java swing user-interface graphics paintcomponent

我正在编写一个绘制纽约地铁地图的简单应用程序。我已成功完成此操作,并且我的地图在JFrame对象中打印。但是,它打印在错误的一侧,因为我认为它引用了左上角的0 0点。如何让它参考左下角,以便以正确的方式打印?

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

}

图形方法

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

1 个答案:

答案 0 :(得分:2)

更改

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

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

,同样适用于x2y2