gpt4 book ai didi

java - 如何持续更新JPanel?

转载 作者:行者123 更新时间:2023-12-01 10:01:17 25 4
gpt4 key购买 nike

我遇到了一个小问题,我正在编写一个 GPS 跟踪应用程序来同时跟踪多个对象。数据通过串行接口(interface)输入,据我所知,这一切都很好。问题是我需要不断更新创建和显示 map 的 JPanel。

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;
}

这就是我所拥有的,很高兴显示一次该点,但不会更新。如果我在串行方法中打印出 1Lat 变量,它会不断打印,但这里只打印一次。

我发现的很多答案都涉及通过数组设置标记,但是在这种情况下这不起作用,因为我正在跟踪的对象可能位于任何地方。

任何帮助将不胜感激:)

最佳答案

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

不一定。在 SwingWorker ,您的 doInBackground() 的实现方法可以publish()当结果可用时。请特别注意“多次调用 publish() 的结果通常会针对单次调用 process() 进行累积”。在你的process() ,只需循环 List<Coordinate> ,更新routerepaint() 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);
}
}

多路由管理留作练习。

关于java - 如何持续更新JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com