我有一个实现优先级队列的Java类。然后,我有一个类测试,该类生成如下图:
digraph G {
Milan (0.0) -> Turin (1.2)
Milan (0.0) -> Montreal (7.0)
Turin (1.2) -> Paris (5.8)
Turin (1.2) -> Tokyo (2.2)
}
此图保存在名为“队列”的文件中。
现在,我希望使用Graphviz在PNG图像中显示该图。因此,我的测试文件的最后一次调用(在您创建优先级并填充队列之后)是:
queue.toString("queue");
行。toString方法如下:
public void toString(String fileDot){
try {
FileOutputStream file = new FileOutputStream(fileDot);
PrintStream Output = new PrintStream(file);
Output.print(this.printQueue());
Output.close();
File f = new File(fileDot);
String arg1 = f.getAbsolutePath();
String arg2 = arg1 + ".png";
String[] c = {"dot", "-Tpng", arg1, "-o", arg2};
Process p = Runtime.getRuntime().exec(c);
int err = p.waitFor();
}
catch(IOException e1) {
System.out.println(e1);
}
catch(InterruptedException e2) {
System.out.println(e2);
}
}
private String printQueue() throws IOException {
String g = new String("");
char c = '"';
g = g.concat("digraph G {\n");
if(isEmpty())
g = g.concat(" " + "Empty priority queue.");
else {
for(int i = 0; i < lastIndex; i++) {
if(heap[2 * i] != null) {
g = g.concat("" + heap[i].elem + " (" + heap[i].prior + ") " + " " + " -> " + " " + "" + heap[i * 2].elem + " (" + heap[i * 2].prior + ") \n" );
if(heap[2 * i + 1] != null)
g = g.concat("" + heap[i].elem + " (" + heap[i].prior + ") " + " " + " -> " + " " + "" + heap[i * 2 + 1].elem + " (" + heap[i * 2 + 1].prior + ") \n" );
}
} //end for
} //end else
g = g.concat("}");
return g;
}
为什么不生成图像.png?我哪里错了?当然我安装了Graphviz。谢谢
当我在命令行中.dot
通过上面的文件运行时dot
,我得到:
$ dot -Tpng queue.dot -oqueue.png
Warning: queue.dot:2: syntax error in line 2 near '('
因此,节点名称中带括号的数字在dot
语法上无效。如果删除它们,我希望.png
文件将成功创建。如果在输出中需要带括号的数字,建议在GraphViz文档中查找节点标签。
我还要注意,toString()
对于创建.png
文件的函数来说,它的名称似乎不太明确,因此建议更改该函数的名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句