Java Swing – Draw Station Names using GPS Coordinates on JPanel

I am working on an assignment and got stuck at a certain point.
I am given a .txt file with a series of stations, and I am supposed to draw the station names using the GPS coordinates given to me, but I can’t translate those coordinates to XY coordinates that a JPanel uses. Also, GPS coordinates are written in doubles but XY coordinates in ints.
How can I achieve this?

Here’s the code that doesn’t work.

     void extents() {
 minLong = Double.MAX_VALUE;
 minLat = Double.MAX_VALUE;
 maxLong = -Double.MIN_VALUE;
 maxLat = -Double.MIN_VALUE;

for(Station station : stations){

    if(station.getLatitude()>maxLat){
        maxLat = station.getLongitude();
        maxLong = station.getLongitude();
    }
    if(station.getLatitude()<minLat){
        minLat = station.getLongitude();
        minLong = station.getLongitude();
    }
    if(station.getLongitude()>maxLong){
        maxLong = station.getLongitude();
    }
    if(station.getLongitude()<minLong){
        minLong = station.getLongitude();
    }
}

I’m trying to draw them using:

 for (int i = 0; i < stations.size(); i++) {

       double scalingFactorx = width/(maxLat - minLat);
double scalingFactory = height/(maxLong - minLong);
double scalingFactor = Math.min(scalingFactorx, scalingFactory);

double drawingX = stations.get(i).getLatitude() * scalingFactor;
double drawingY = stations.get(i).getLongitude() * scalingFactor;
double drawingY2 = height - drawingY; 
gr.drawString(stations.get(i).getName(), (int)drawingX/100, (int)drawingY2/200);
      }

I’m getting different coordinates for each station now, however, they are still too close, which means that they didn’t get scaled to the JPanel.

Thanks!

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Well, Firstly, you should make the minimum and maximum values to be the actual minimums and maximums of the stations: double minLong = Double.MAX_VALUE; double minLat = Double.MAX_VALUE; double maxLong = -Double.MIN_VALUE; double maxLat = -Double.MIN_VALUE; for(Station station : stations){ if(station.getLatitude()>maxLat){ maxLat = station.getLongitude(); maxLong = station.getLongitude(); } if(station.getLatitude()<minLat){ minLat = station.getLongitude(); minLong = station.getLongitude(); } if(station.getLongitude()>maxLong){ maxLong = station.getLongitude(); } if(station.getLongitude()<minLong){ minLong = station.getLongitude(); } } And then you can do this in your draw method: for (int i = 0; i < stations.size(); i++) { double scalingFactorx = width/(maxLat - minLat); double scalingFactory = height/(maxLong - minLong); double… Read more »

Jason
Guest
   for (int i = 0; i < stations.size(); i++) {

    extents();

    scalingFactorx = width/(maxLat - minLat);
    scalingFactory = height/(maxLong - minLong);
    scalingFactor = Math.min(scalingFactorx, scalingFactory);

    drawingX = (stations.get(i).getLatitude() - minLat)   * scalingFactorx;
    drawingY = (stations.get(i).getLongitude() - minLong)  * scalingFactory;
    drawingY2 = height - drawingY; 
    gr.drawString(stations.get(i).getName(), (int)drawingX, (int)drawingY2);

Something like this should fix it.