如何配置VisualizationViewer以便用户可以使用鼠标移动节点?

时间:2014-01-15 18:33:43

标签: java jung

我现在正在使用Jung 2.0一段时间,但我对随附的各种演示中的所有配置代码感到困惑。

有人能告诉我如何配置VisualizationViewer,以便我可以通过点击鼠标来拖动图形节点吗?

我的代码如下。这是我研究的各种演示的混合。但我只能用它平移和旋转图形。

public class GrafoParticipacaoSocietaria extends JFrame {

    Graph<VerticeParticipacaoSocietaria, Integer> graph;
    VisualizationViewer<String, String> vv;
    Layout<VerticeParticipacaoSocietaria, Integer> layout;

    public GrafoParticipacaoSocietaria(Graph<VerticeParticipacaoSocietaria, Integer> grafoPart) {
        super("Participação Societária");

        graph = grafoPart;
        layout = new ISOMLayout(graph);

        final VisualizationModel visualizationModel = new DefaultVisualizationModel(layout);
        vv = new VisualizationViewer(visualizationModel);

        vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
        vv.setForeground(Color.blue);
        getContentPane().add(vv);

        // this class will provide both label drawing and vertex shapes
        VertexLabelAsShapeRenderer<String, String> vlasr = new VertexLabelAsShapeRenderer<String, String>(
                vv.getRenderContext());

        vv.getRenderContext().setVertexShapeTransformer(vlasr);
        vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.lightGray));
        vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.lightGray));
        vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(1.0f)));

        // customize the renderer
        // vv.getRenderer().setVertexRenderer(new MyGradientVertexRenderer<String, String>(Color.cyan, Color.white, true));
        vv.getRenderer().setVertexLabelRenderer(vlasr);

        vv.setGraphMouse(new DefaultModalGraphMouse());
    }
}

2 个答案:

答案 0 :(得分:3)

感谢GrahamA's answer,我找到了我要找的代码:

    DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
    graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
    vv.setGraphMouse(graphMouse);

该代码将ModalGraphMouse中的模式更改为PICKING,允许用户选择并移动节点。

答案 1 :(得分:1)

您可以在荣格网站上找到一个示例here使用Editing添加节点,然后切换到Picking以移动您添加的节点。可以从SourceFource查看jung-samples-X.X.X.jar这个名为GraphEditorDemo的类

enter image description here Jung网站的屏幕截图