我现在正在使用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());
}
}
答案 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的类
Jung网站的屏幕截图