如何在JUNG圖形可視化中使用自定義頂點標籤?如何在JUNG圖形可視化中添加自定義頂點標籤?
我在關注Jung 2.0 Tutorial,我發現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」?
請引用您正在關注的教程。另見[*初始線程*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod 2012-02-19 16:41:08
既然您已將'SparseMultigraph'的泛型定義爲'SparseMultigraph ',其中頂點的通用V爲整數,邊的泛型E爲String,因此每個頂點的標籤值都是整數,每個邊的標籤在字符串中。所以,如果你想通過名稱像q1,v2等每個頂點,使用通用V的字符串,所以你可以傳遞一個頂點名稱,像這樣'g.addVertex(「q1」);' –
ecle
2012-02-19 18:04:11
要更改默認頂點標籤轉換器在JUNG呈現上下文中,搜索關鍵字'getRenderContext()。setVertexLabelTransformer' – ecle 2012-02-19 18:22:04