java-如何手动设置Jung可视化的缩放?

提问

我在JPanel中显示了一个钟形树.我的树的构造函数如下所示:

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

但是树很大.因此,我想知道是否有一种方法可以自动缩小以使树适合窗口,否则可以设置一个默认缩放,该默认缩放小于默认缩放.我怎样才能做到这一点 ?

最佳答案

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

要使图形适合窗口,您可以计算图形大小和面板大小之间的差异,然后调用setZoom()方法并传递差异因子.