gpt4 book ai didi

java - 更改 JFrame 中的方向

转载 作者:行者123 更新时间:2023-12-02 06:05:02 25 4
gpt4 key购买 nike

我正在编写一个简单的应用程序来绘制纽约地铁 map 。我已经成功地做到了这一点,并且我的 map 打印在 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

最佳答案

改变

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也是如此。

关于java - 更改 JFrame 中的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com