更改 JUNG 中顶点的大小/颜色
如何更改荣格可视化库中特定顶点的大小?
我正在阅读文档,但我对java不是很熟悉,我无法在网上找到任何好的例子。
如何更改荣格可视化库中特定顶点的大小?
我正在阅读文档,但我对java不是很熟悉,我无法在网上找到任何好的例子。
我花了一段时间,但这是一个可读的,完全注释的程序,可以更改图形中的顶点大小和颜色。享受!
public class SimpleGraphView {
public SimpleGraphView() {
// Create a graph with Integer vertices and String edges
Graph<Integer, String> g = new SparseGraph<Integer, String>();
for(int i = 0; i < 5; i++) g.addVertex(i);
g.addEdge("Edge", 1, 2);
g.addEdge("Another Edge", 1, 4);
// Layout implements the graph drawing logic
Layout<Integer, String> layout = new CircleLayout<Integer, String>(g);
layout.setSize(new Dimension(300,300));
// VisualizationServer actually displays the graph
BasicVisualizationServer<Integer,String> vv = new BasicVisualizationServer<Integer,String>(layout);
vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size
// Transformer maps the vertex number to a vertex property
Transformer<Integer,Paint> vertexColor = new Transformer<Integer,Paint>() {
public Paint transform(Integer i) {
if(i == 1) return Color.GREEN;
return Color.RED;
}
};
Transformer<Integer,Shape> vertexSize = new Transformer<Integer,Shape>(){
public Shape transform(Integer i){
Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
// in this case, the vertex is twice as large
if(i == 2) return AffineTransform.getScaleInstance(2, 2).createTransformedShape(circle);
else return circle;
}
};
vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);
vv.getRenderContext().setVertexShapeTransformer(vertexSize);
JFrame frame = new JFrame("Simple Graph View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new SimpleGraphView();
}
}