How can I update JPanel continuously?

≯℡__Kan透↙ 提交于 2019-11-28 14:39:24

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!