Scalable and centralized JUNG graph

114 views Asked by At

I have the following codes to draw a rooted tree, Every things work great, But I struggled to centralize and making the graph fitted in its parent panel for about 1 hour without any luck.

DelegateTree<Integer, String> g2 = new DelegateTree<>();

    g2.setRoot(1);
    g2.addChild("E1", 1, 2);
    g2.addChild("E2", 1, 3);
    g2.addChild("E3", 1, 4);
    g2.addChild("E4", 1, 5);
    g2.addChild("E6", 2, 6);
    g2.addChild("E7", 2, 7);

    Layout<Integer, String> layout = new TreeLayout<>(g2);

    BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<>(layout);
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
        @Override
        public Paint transform(Integer i) {
            return Color.GREEN;
        }
    };
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    vv.scaleToLayout(new CrossoverScalingControl());
    pnlGraph.add(vv);

Note that I want my graph to be stayed at center of its parent boundaries and when whole JFrame resized it be scaled down to fit its parent.

0

There are 0 answers