How to manually set the zoom on a Jung visualisation?

老子叫甜甜 提交于 2019-12-01 22:00:49

问题


I have a jung tree displayed in a JPanel. The constructor of my tree looks like this:

  Forest<String, Integer> graph = new DelegateForest<String, Integer>();
  static GraphZoomScrollPane panel = null;
  static DefaultModalGraphMouse graphMouse = null;
  static JComboBox modeBox = null;
  static ScalingControl scaler;

  public PanelTree(List<Cluster> clist) {
    setBounds(215, 10, 550, 550);
    updateData(clist); // adds vertex and edges to graph

    treeLayout = new TreeLayout<String, Integer>(graph);
    vv = new VisualizationViewer<String, Integer>(treeLayout, new Dimension(500, 500));
    vv.setBackground(Color.white);
    vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    // add a listener for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller());
    vv.getRenderContext()
            .setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));

    panel = new GraphZoomScrollPane(vv);
    add(panel);

    graphMouse = new DefaultModalGraphMouse();

    vv.setGraphMouse(graphMouse);

    modeBox = graphMouse.getModeComboBox();
    modeBox.addItemListener(graphMouse.getModeListener());
    graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

    scaler = new CrossoverScalingControl();
}

But the tree is quite large. So I want to know is there is a way to either automatically zoom out so the tree fits in the windows, and otherwise just set a default zoom that is less than the default one. How can I do that ?


回答1:


ScalingControl scaler = new CrossoverScalingControl();

public void zoomIn() {
    setZoom(1);
}

public void zoomOut() {
    setZoom(-1);
}

private void setZoom(int amount) {
    scaler.scale(vv, amount > 0 ? 1.1f : 1 / 1.1f, vv.getCenter());
}

To fits the graph on the window, you can calculate the diference between the graph size and the panel size, and call the setZoom() method passing the factor of diference.




回答2:


ScalingControl is not good method if you use Mouse Transformer.

Try to:

// for zoom:
vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setScale(scale_x1, scale_y1, vv.getCenter());
// for out:
vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).setScale(scale_x2, scale_y2, vv.getCenter());


来源:https://stackoverflow.com/questions/9761391/how-to-manually-set-the-zoom-on-a-jung-visualisation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!