views:

236

answers:

1

Hello everyone... I am using JUNG to make a network diagram. I want to shape the vertices depending upon its type. The vertices are pickable and colored. The code for vertices so far is as under:

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;
    }
}

I am using the following statement for each vertex:

vv.getRenderContext().setVertexFillPaintTransformer(new VertexColors(vv.getPickedVertexState()));

Now, I cannot find a way to shape the vertices while keeping them pickable and to wrap the vertices around their labels. Please help...

+1  A: 

Hi,

All you need is to add another Transformer that provides the vertex shape when it is selected. The Transformer should choose the shape based on the whether the vertex is "picked" or not. To get the picked state, you need to obtain a PickedState object from the visualization. When the selection is changed, the transformer will be asked for the shape and the vertices will be updated with the returned shape. Here is an example of how to do this:

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);
Klarth