0
我正在編寫一個繪製紐約市地鐵地圖的簡單應用程序。我成功地這樣做了,並且我的地圖以JFrame對象打印。但是,它會打印錯誤的一面,因爲我認爲它引用了左上角的0 0點。我怎樣才能使它參考左下角,以便打印正確的方式?在JFrame中更改方向
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);
// }
}
}
}
int x1 =(int)((a.getLon() - minLon)/ lonLength * width); int y1 =(int)(maxLat - (a.getLat())/ latLength * height); int x2 =(int)((b.getLon() - minLon)/ lonLength * width); int y2 =(int)((maxLat - b.getLat())/ latLength * height); – Jenny
你是這個意思嗎?壞消息,它不起作用 – Jenny
不,不像你輸入的內容。你已經把括號放在了我所在的地方的不同位置,在'y1'的表達式中。 –