问题
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