如何在Jung可视化上手动设置缩放?

时间:2012-03-18 19:11:42

标签: java zoom jung

我在JPanel中显示了一个jung树。我树的构造函数如下所示:

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

但这棵树很大。所以我想知道是否有一种方法可以自动缩小以使树适合窗口,否则只需设置一个小于默认值的默认缩放。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

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()方法传递差异因子。

答案 1 :(得分:2)

如果使用鼠标变换器,ScalingControl不是一个好方法。

尝试:

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