Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've got a slight problem, I'm writing a gps tracking app to track several objects at once. The data comes in over a serial interface, this is coming in fine from what I can tell. The issue is that I need to continually update the JPanel where the map is created and displayed.

public JPanel mapDisplay(){
    JPanel mapPanel = new JPanel();
    mapPanel.setSize(560, 540);
    Coordinate start = new Coordinate (-34.9286, 138.6);
    trackMap.addMapMarker(new MapMarkerDot(1Lat, 1Lon));
    trackMap.setDisplayPosition(start,8);
    System.out.println(1Lat);

    mapPanel.add(trackMap);
    mapPanel.setVisible(true);
    return mapPanel;
}

This is what I have and it's happy to display the point once but won't update. If I print out the 1Lat variable in the serial method it continually prints, however it only does it once here.

A lot of the answers I've found refer to setting markers by arrays, however that won't work in this case as the objects I'm tracking could be anywhere.

Any help would be greatly appreciated :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

Is it possible to use a worker thread and not use an ArrayList? I would run the risk of missing data if I do.

Not necessarily. In a SwingWorker, your implementation of the doInBackground() method can publish() results as they become available. Note in particular that "Results from multiple invocations of publish() are often accumulated for a single invocation of process()." In your process(), simply loop through the List<Coordinate>, update the route and repaint() the map.

map

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;

/**
 * @see http://stackoverflow.com/a/37193636/230513
 */
public class MapWorkerTest {

    private final List<Coordinate> route = new ArrayList<>();

    private void display() {
        JFrame f = new JFrame("MapWorker");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMapViewer map = new JMapViewer() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }

            @Override
            public String getToolTipText(MouseEvent e) {
                Coordinate c = (Coordinate) getPosition(e.getX(), e.getY());
                return c.getLat() + " " + c.getLon();
            }
        };
        map.setToolTipText("");
        Coordinate start = new Coordinate(-34.9286, 138.6);
        route.add(start);
        MapPolygonImpl poly = new MapPolygonImpl(route);
        poly.setColor(Color.blue);
        map.addMapPolygon(poly);
        map.setDisplayPosition(start, 10);
        f.add(map);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        new MapWorker(map, start).execute();
    }

    private class MapWorker extends SwingWorker<Void, Coordinate> {

        private final JMapViewer map;
        private Coordinate last;

        public MapWorker(JMapViewer map, Coordinate start) {
            this.map = map;
            this.last = start;
        }

        @Override
        protected Void doInBackground() throws Exception {
            while (!isCancelled()) {
                last = new Coordinate(last.getLat() + 0.0025, last.getLon() + 0.01);
                publish(last);
                Thread.sleep(1000);
            }
            return null;
        }

        @Override
        protected void process(List<Coordinate> chunks) {
            for (Coordinate c : chunks) {
                route.add(c);
            }
            map.repaint();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new MapWorkerTest()::display);
    }
}

Multiple route management left as a exercise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...