当我尝试在调用Thread和sleep()的循环中更新它时,包含嵌入式单个图形(Graphstream)的我的JFrame
冻结。我试图在独立图上使用相同的更新(显示在它自己的图上),并且按预期工作。Thread.sleep()冻结包含GraphStream的JFrame/GUI图
我有嵌入的JFrame单个图表如下(AppGraph.java):
public static ViewPanel init(){
graph.addAttribute("ui.stylesheet", styleSheet);
graph.setAutoCreate(true);
graph.setStrict(false);
graph.addAttribute("ui.quality");
graph.addAttribute("ui.antialias");
initGraph();
initNodes(graph);
return attachViewPanel();
}
private static ViewPanel attachViewPanel() {
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
viewer.enableAutoLayout();
return viewer.addDefaultView(false);
}
private static void initGraph(){
FileSource fs = new FileSourceDOT();
String graph_filename = "graph.gv";
String absolute_path = System.getProperty("user.home") + File.separator + graph_filename;
fs.addSink(graph);
try {
fs.readAll(absolute_path);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
} finally {
fs.removeSink(graph);
}
}
然后这被称为在JFrame类如下:
/*AppWindow.java
* Set up graph
*/
GridBagConstraints graphConstraints = new GridBagConstraints();
graphConstraints.fill = GridBagConstraints.BOTH;
graphConstraints.gridx = 0;
graphConstraints.gridy = 1;
graphConstraints.weightx = 0.5;
graphConstraints.weighty = 0.5;
graphConstraints.gridwidth = 4;
graphConstraints.gridheight = GridBagConstraints.RELATIVE;
add(AppGraph.init(), graphConstraints);`
在JFrame
是按钮针对不同的搜索算法,如BFS。这些算法的执行过程中,遍历边以固定的时间间隔被着色,以产生一种动画效果,如下所示:
//BFSAlgorithm.java
private void callBFS(Node startNode, Node goalNode) {
startNode.setAttribute("parent", "null");
startNode.setAttribute("level", 0);
startNode.setAttribute("visited?");
LinkedList<Node> queueFrontier = new LinkedList<>();
int level = 1;
queueFrontier.addLast(startNode);
while (!queueFrontier.isEmpty()) {
System.out.println("Level: " + (level - 1));
LinkedList<Node> next = new LinkedList<>();
for (Node node : queueFrontier) {
if (node == goalNode) {
System.out.println(node.getId() + ": Found Found Found!!!");
if (node != startNode) {
colorEdge(node);
}
return;
}
System.out.print(node.getId() + " visited \t");
if (node != startNode) {
colorEdge(node);
}
for (Edge edge : node.getEdgeSet()) {
Node opposite = edge.getOpposite(node);
if (!opposite.hasAttribute("visited?")) {
System.out.print(opposite.getId() + " enqueued \t");
opposite.setAttribute("level", level);
opposite.setAttribute("parent", node);
opposite.setAttribute("visited?");
next.addLast(opposite);
}
}
System.out.print("\n");
}
level++;
queueFrontier = next;
sleep();
}
}
private void colorEdge(Node node) {
Edge visitedEdge = node.getEdgeBetween(node.getAttribute("parent", Node.class));
visitedEdge.setAttribute("ui.color", 0.5);
sleep();
}
private void sleep() {
try {
Thread.sleep(AppWindow.speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
这BFSAlgorithm
器具DynamicAlgorithm
和延伸SinkAdapter
。随着算法的运行,我已经扩展了SinkAdapter
以使它与视图交互。当我调用BFSAlgorithm
时,算法运行并且各种println
语句被延迟sleep()
,则GUI会冻结并且无响应,直到执行完所有访问边被着色为止。我试图在实施ViewerListener
我AppGraph.java
如记录在graphstream documentation但只造成了一个无限循环崩溃的应用程序:
/*...init() method from AppGraph.java*/
ProxyPipe fromViewer = viewer.newThreadProxyOnGraphicGraph();
fromViewer.addSink(graph);
fromViewer.pump();
while(loop) {
fromViewer.pump(); //
}
看一看[并发在Swing(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)和[如何使用Swing定时器](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)和[Worker Threads and SwingWorker](http://docs.oracle.com/javase/) tutorial/uiswing/concurrency/worker.html) – MadProgrammer
我没有足够的时间来回答,但你应该使用[Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer .html)而不是 – Frakcool
尝试重新验证您的图形所在的JPanel – rert588