本文介绍了JUNG图形可视化中的顶点标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我写了一个小图形可视化类: pre $ public c ++ simpleGraph(SparseMultigraph< Vertex,SEdge> graph,String name) { 布局< Vertex,SEdge> layout = new ISOMLayout(graph); layout.setSize(new Dimension(800,800)); BasicVisualizationServer< Vertex,SEdge> vv = new BasicVisualizationServer< Vertex,SEdge>(layout); vv.setPreferredSize(new Dimension(850,850)); //设置查看区域大小 JFrame frame = new JFrame(name); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane()。add(vv); frame.pack(); frame.setVisible(true); } 如何为顶点和边添加标签?值存储在我的自定义顶点类中。我可以迭代Layout或BasicVisualizationServer中的所有顶点并添加标签吗? 感谢您的帮助! 解决方案您需要为您的顶点/边缘调用标签转换器: vv.getRenderContext ().setVertexLabelTransformer(new ToStringLabeller()); 这些在样本中很常见。它使用顶点类的toString()方法来指定标签。 一个稍微涉及的例子: vv.getRenderContext()。setEdgeLabelTransformer(new Transformer< MyEdge,String>(){ public String transform(MyEdge e){ return(e.toString ()++ e.getWeight()+/+ e.getCapacity()); } }); 您不需要遍历边; EdgeLabelTransformer或VertexLabelTransformer会在您的边更新属性时标出边缘,VisualizationViewer会立即更新它们。 I wrote a little graph visualizer class: public void simpleGraph(SparseMultigraph<Vertex,SEdge> graph, String name) { Layout<Vertex, SEdge> layout = new ISOMLayout(graph); layout.setSize(new Dimension(800,800)); BasicVisualizationServer<Vertex, SEdge> vv = new BasicVisualizationServer<Vertex, SEdge>(layout); vv.setPreferredSize(new Dimension(850,850)); //Sets the viewing area size JFrame frame = new JFrame(name); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(vv); frame.pack(); frame.setVisible(true);}How can I add labels for vertices and edges? The Values are stored in my custom vertex class. Can I iterate over all vertices in the Layout or BasicVisualizationServer and add labels?Thanks for your help! 解决方案 You need to call a label transformer for your vertex/edge: vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());This is something you'd find pretty often in the samples. It uses the toString() method of your vertex class to specify the label.A slightly more involved example: vv.getRenderContext().setEdgeLabelTransformer(new Transformer<MyEdge, String>() { public String transform(MyEdge e) { return (e.toString() + " " + e.getWeight() + "/" + e.getCapacity()); } });You don't need to iterate over the edges; the EdgeLabelTransformer or VertexLabelTransformer will label your edges as and when their properties are updated, and the VisualizationViewer will update them on the fly. 这篇关于JUNG图形可视化中的顶点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 05:26