0

I implement a graph algorithm and I have to make an interface for it. A frame with every step. I will do it manually. I use the Jung package but I can't figure out how to make that graph to look like a tree. I'm struggling for two days.

Here is my attempt:

DirectedSparseGraph<String, ?> g = new DirectedSparseGraph<String, Object>();

        g.addVertex("ROOT");
        g.addVertex("A");
        g.addVertex("B");
        g.addVertex("C");
        g.addVertex("D");
        g.addVertex("E");
         VisualizationImageServer vv = new VisualizationImageServer(new CircleLayout(g), new Dimension(350, 300));  


        JFrame frame = new JFrame("First step");

        frame.getContentPane().add(vv);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

There I used CircleLayout, I should use TreeLayout but I have to implement Forest interface and I don't know how, I'm not a bright programmer. :(

I want my graph to look like this:

Desire result

Wolf Marian
  • 147
  • 12

2 Answers2

0

If you want the graph to look like a tree, you need to provide a layout that does that, i.e., TreeLayout. CircleLayout is going to do what the name suggests.

You don't have to implement the Forest interface; there's already an implementation of it available. Just use DelegateTree instead of DirectedSparseGraph.

Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
0

you need to define a Graph, Transformer for the labels and Vertex and add the VisualizationViewer

Example:

public static void main(String[] args) {
    Graph<Integer, String> graph3 = new DelegateForest<>();

    graph3.addVertex(1);
    graph3.addVertex(2);
    graph3.addVertex(3);

    graph3.addEdge("RAD-A", 1, 2);
    graph3.addEdge("RAD-B", 1, 3);

    Layout<Integer, String> layout3 = new TreeLayout<>((Forest<Integer, String>) graph3);
    VisualizationViewer<Integer, String> vv3 = new VisualizationViewer<>(layout3);

    Transformer<String, String> transformer3 = new Transformer<String, String>() {

        @Override
        public String transform(String arg0) {
        return arg0;
        }
    };

    vv3.getRenderContext().setEdgeLabelTransformer(transformer3);
    Transformer<Integer, String> transformer = new Transformer<Integer, String>() {

        @Override
        public String transform(Integer arg0) {
        return arg0.toString();
        }

    };
    vv3.getRenderContext().setVertexLabelTransformer(transformer);

    transformer3 = new Transformer<String, String>() {
        @Override
        public String transform(String arg0) {
        return arg0;
        }
    };

    final DefaultModalGraphMouse<String, Number> graphMouse3 = new DefaultModalGraphMouse<>();
    vv3.setGraphMouse(graphMouse3);
    graphMouse3.setMode(ModalGraphMouse.Mode.PICKING);

    JFrame frame3 = new JFrame("Pas 3");
    frame3.getContentPane().add(vv3);
    frame3.pack();
    frame3.setSize(350, 300);
    frame3.setVisible(true);
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • You could just use `vv3.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());` etc. with `edu.uci.ics.jung.visualization.decorators.ToStringLabeller` – amoebe Oct 27 '16 at 18:04