我正在使用JUNG制作网络图。我想根据其类型来塑造顶点。顶点是可拾取和着色的。到目前为止顶点的代码如下:
class VertexColors extends PickableVertexPaintTransformer<Number> {
VertexColors(PickedInfo<Number> pi) {
super(pi, Color.blue, Color.yellow);
}
public Paint transform(Number v) {
if (pi.isPicked(v.intValue())) return picked_paint;
return v.intValue()%2==1 ? Color.blue : Color.green;
}
}
我对每个顶点使用以下语句:
vv.getRenderContext().setVertexFillPaintTransformer(new VertexColors(vv.getPickedVertexState()));
现在,我无法找到一种方法来塑造顶点,同时保持它们可选择并将顶点包裹在它们的标签周围。
答案 0 :(得分:3)
您只需要添加另一个Transformer,在选中时提供顶点形状。变形金刚应根据顶点是否“拾取”来选择形状。要获取拾取状态,您需要从可视化中获取PickedState对象。更改选择后,将询问变换器的形状,并使用返回的形状更新顶点。以下是如何执行此操作的示例:
final VisualizationViewer<Integer, String> vv = new
VisualizationViewer<Integer, String>(layout);
// Transformer for cycling the vertices between three unique shapes.
Transformer<Integer, Shape> vertexShape = new
Transformer<Integer, Shape>() {
private final Shape[] styles = {
new Ellipse2D.Double(-25, -10, 50, 20),
new Arc2D.Double(-15, -15, 30, 30, 30, 150, Arc2D.PIE) };
@Override
public Shape transform(Integer i) {
// Choose a shape according to the "picked" state.
PickedState<Integer> pickedState = vv.getPickedVertexState();
int shapeIndex = 0;
if (pickedState.isPicked(i)) {
shapeIndex = 1;
}
return styles[shapeIndex];
}
};
vv.getRenderContext().setVertexShapeTransformer(vertexShape);