如何在 JUNG 图可视化中添加自定义顶点标签?
如何在 JUNG 图形可视化中使用自定义顶点标签?
我正在关注 Jung 2.0 教程,我发现 setVertexLabelTransformer( )
可用于标记顶点,但据我所知,这些标签无法自定义。
例如,下面的代码生成三个顶点,顶点标签为 1,2,4:
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import java.awt.Dimension;
import javax.swing.JFrame;
public class SimpleGraphView {
Graph<Integer, String> g;
public SimpleGraphView() {
g = new SparseMultigraph<Integer, String>();
g.addVertex((Integer)1);
g.addVertex((Integer)2);
g.addVertex((Integer)4);
}
public static void main(String[] args) {
SimpleGraphView sgv = new SimpleGraphView();
Layout<Integer, String> layout = new CircleLayout(sgv.g);
layout.setSize(new Dimension(800,800));
BasicVisualizationServer<Integer,String> vv =
new BasicVisualizationServer<Integer,String>(layout);
vv.setPreferredSize(new Dimension(850,850));
JFrame frame = new JFrame("Simple Graph View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
}
如何添加“q0”等标签?
How to use custom vertex labels in JUNG graph visualization?
I am following Jung 2.0 Tutorial where I found that setVertexLabelTransformer()
can be used to label the vertices, but these labels cannot be customized, to my knowledge.
For example, the below code produces three vertices, having vertex-labels 1,2,4:
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import java.awt.Dimension;
import javax.swing.JFrame;
public class SimpleGraphView {
Graph<Integer, String> g;
public SimpleGraphView() {
g = new SparseMultigraph<Integer, String>();
g.addVertex((Integer)1);
g.addVertex((Integer)2);
g.addVertex((Integer)4);
}
public static void main(String[] args) {
SimpleGraphView sgv = new SimpleGraphView();
Layout<Integer, String> layout = new CircleLayout(sgv.g);
layout.setSize(new Dimension(800,800));
BasicVisualizationServer<Integer,String> vv =
new BasicVisualizationServer<Integer,String>(layout);
vv.setPreferredSize(new Dimension(850,850));
JFrame frame = new JFrame("Simple Graph View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
}
How do I add labels like "q0"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已将
SparseMultigraph
的泛型定义为SparseMultigraph
,其中顶点的泛型V
为 Integer,边的通用 E 为String
,因此每个顶点的标签值采用Integer
格式,每条边的标签采用String
格式。因此,如果您希望每个顶点的名称如 q1、v2 等,请使用String
表示通用V
,这样您就可以传递像这样的顶点名称g .addVertex("q1");
Since you have defined the generics of
SparseMultigraph<V, E>
asSparseMultigraph<Integer, String>
where the genericV
for vertex as Integer and the generic E for edge asString
, hence each vertex's label value is inInteger
and each edge's label inString
. So, if you want each vertex by names like q1, v2, etc., useString
for genericV
, so you can pass a vertex name like thisg.addVertex("q1");
如果您有节点的自定义类,我将给出我的项目中的示例。我有一个 Node 类,如下所示:
// 您只需要像下面这样覆盖转换:
// 它将显示 screenName 属性作为图中每个节点的标签。
// 我希望这就是您正在寻找的。
If you have a custom class for nodes, I will give an example from my project. I have a class Node which is like :
// You just need to override transform like below:
// It will show the screenName property as a label for each node in the graph.
// I hope this is what you are looking for.