0

I am creating a map using JMapViewer in Swing. I have several MapMarkerDots on the map which represent cars. I'm trying to update the positions of these markers so that they appear to be driving around the map however it's not quite working properly. I have a list of coordinates that the "car" is to follow but what happens is that the positions are updated but the markers are not redrawn until this is finished meaning that the marker is drawn at the initial location and at the final location rather that at every point between the two. The code I'm using for this is below. Any idea as to why this may be happening?

public void drawRoute(String id){

    MapMarkerDot mmd;                                                       
    String evMarkerObject;          // ID and Marker position
    String[] items, locations;
    double lat, lon;

    for(int i = 0; i < route.length-1; i+=2){       // Iterate through the route

         List markers = zmap.getMapMarkerList();        // Get the markers that are currently on the map


        for(int j = 0; j < Daemon.evMarkers.size(); j++){  // Go through the list of recorded marker IDs and locations
            evMarkerObject = Arrays.toString(Daemon.evMarkers.get(j));      // Get marker id and location
            items = evMarkerObject.split(", ");                             // Split the ID and location
            if(items[0].substring(1).equals(id)){                           // If an ID match is found

                locations = items[1].split(" ");                            // Split location values by " "
                lat = Double.parseDouble(locations[2]);                     // Get latitude of marker
                lon = Double.parseDouble(locations[3]);                     // Get longitude of marker
                for(int k = 0; k < markers.size(); k++){                    // Go through list of markers currently on map
                    mmd = (MapMarkerDot) markers.get(k);                    // Get each marker in turn
                    if((mmd.getLat() == lat) && (mmd.getLon() == lon)){     // Check if recorded position matches marker position                               
                        zmap.removeMapMarker(mmd);                          // Remove marker from the map
                        break;                                              // Break from loop (appropriate marker found)
                    }
                }

                Daemon.evMarkers.remove(j);                                                 // Remove record of marker ID and position
                zaddMarker(Color.BLUE, route[i], route[i+1], 'e', items[0].substring(1));   // Add marker at new position
                    //zmap.repaint();
            }
        }
    }

Calling the function (based on answer from @Catalina):

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

                                @Override
                                protected Void doInBackground() throws Exception {
                                    drawRoute(markerID);
                                    return null;
                                }
                            };

                            worker.execute();

This is called on a mouse click event.

Riddle
  • 89
  • 2
  • 11

1 Answers1

2

Daemon sounds like a background thread, so you need to do any updates on the event dispatch thread (EDT) with SwingUtilities.invokeLater. If that works, SwingWorker might be a good way to make your Daemon do regular EDT updates in the worker's process method.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • That works however it makes my CPU usage reach 100% and I get this exception `java.util.ConcurrentModificationException`. Do you have any idea why this could be? I've never used SwingWorker before so I'm not certain that what I have is ok. Does the edit I made to my question seem ok? – Riddle Mar 03 '13 at 11:03
  • 1
    @Riddle something wrong with the code you are not showing (the _SSCCE_ - google is your friend on finding the link, don't have it handy right here) – kleopatra Mar 03 '13 at 11:31
  • 1
    You definitely shouldn't draw in `doInBackground`; `publish` your new point and draw in `process`. – Catalina Island Mar 04 '13 at 12:20