How to manually adjust the scale on Young's visualization?

I have a jung tree displayed in a JPanel. My tree constructor 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 pretty big. So I want to know if there is a way to automatically zoom out so that the tree fits into the windows, and otherwise just set the default scale to less than the default. How can i do this?

+3


source to share


2 answers


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 fit the graph in the window, you can calculate the difference between the graph size and the pane size and call the setZoom () method passing the difference coefficient.

+2


source


ScalingControl is not a good method if you are using Mouse Transformer.

Try:



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

      

+2


source







All Articles