本文介绍了在JUNG中更改顶点的大小/颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改Jung Visualization Library中特定顶点的大小?
我正在阅读文档,但对java并不是非常熟悉,我也找不到任何好的示例。
我花了一段时间,但这里有一个可读的完全注释的程序,它改变了图形中的顶点大小和颜色。享受!
public class SimpleGraphView {
public SimpleGraphView(){
//用Integer创建一个图顶点和字符串边缘
Graph< Integer,String> g = new SparseGraph< Integer,String>();
for(int i = 0; i g.addEdge(Edge,1,2);
g.addEdge(Another Edge,1,4);
//布局实现图形绘制逻辑
布局<整数,字符串> layout = new CircleLayout< Integer,String>(g);
layout.setSize(new Dimension(300,300));
// VisualizationServer实际显示图
BasicVisualizationServer< Integer,String> vv = new BasicVisualizationServer< Integer,String>(layout);
vv.setPreferredSize(new Dimension(350,350)); //设置查看区域大小
// Transformer将顶点数映射到顶点属性
Transformer< Integer,Paint> vertexColor = new Transformer< Integer,Paint>(){
public Paint transform(Integer i){$ b $ if(i == 1)return Color.GREEN;
返回Color.RED;
}
};
Transformer< Integer,Shape> vertexSize = new Transformer< Integer,Shape>(){
public Shape transform(Integer i){
Ellipse2D circle = new Ellipse2D.Double(-15,-15,30,30);
//在这种情况下,顶点是两倍大
if(i == 2)返回AffineTransform.getScaleInstance(2,2).createTransformedShape(circle);
else返回圆;
}
};
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){
SimpleGraphView();
}
}
How do you change the size of a specific vertex in Jung Visualization Library?
I'm reading the documentation but I'm not extremely familiar with java and I can't find any good examples on line.
解决方案
It took me a while, but here is a readable, fully-commented program that changes the vertex size and color in a graph. Enjoy!
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();
}
}
这篇关于在JUNG中更改顶点的大小/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!