java - How can I update JPanel continuously? -
i've got slight problem, i'm writing gps tracking app track several objects @ once. data comes in on serial interface, coming in fine can tell. issue need continually update jpanel map created , 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 have , it's happy display point once won't update. if print out 1lat variable in serial method continually prints, once here.
a lot of answers i've found refer setting markers arrays, won't work in case objects i'm tracking anywhere.
any appreciated :)
is possible use worker thread , not use
arraylist
? run risk of missing data if do.
not necessarily. in swingworker
, implementation of doinbackground()
method can publish()
results become available. note in particular "results multiple invocations of publish()
accumulated single invocation of process()
." in process()
, loop through list<coordinate>
, update route
, repaint()
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) { (coordinate c : chunks) { route.add(c); } map.repaint(); } } public static void main(string[] args) { eventqueue.invokelater(new mapworkertest()::display); } }
multiple route management left exercise.
Comments
Post a Comment