0

我正在尝试将字符串文本放在 JUNG 树图的边缘。见下面的例子。我尝试的是简单地添加这一行:

graph.addEdge("arrow", "Vmain", "Vsecond");

...但没有任何结果(见下面的截图)。我需要一些建议。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.commons.collections15.Factory;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Forest;
import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.DelegateTree;
import edu.uci.ics.jung.graph.Tree;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;

public class TreeVisualiser extends JApplet {

    Forest<String, String> graph;

    Factory<DirectedGraph<String, String>> graphFactory = new Factory<DirectedGraph<String, String>>() {
        public DirectedGraph<String, String> create() {
            return new DirectedSparseMultigraph<String, String>();
        }
    };

    Factory<Tree<String, String>> treeFactory = new Factory<Tree<String, String>>() {
        public Tree<String, String> create() {
            return new DelegateTree<String, String>(graphFactory);
        }
    };

    VisualizationViewer<String, String> vv;
    String root;
    TreeLayout<String, String> treeLayout;

    @SuppressWarnings({"rawtypes", "unchecked"})
    public TreeVisualiser() {

        // create a simple graph for the demo
        graph = new DelegateForest<String, String>();

        createTree();

        treeLayout = new TreeLayout<String, String>(graph);
        vv = new VisualizationViewer<String, String>(treeLayout, new Dimension(600, 600));
        vv.setBackground(Color.white);
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        // add a listener for ToolTips
        vv.setVertexToolTipTransformer(new ToStringLabeller());

        Container content = getContentPane();
        final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
        content.add(panel);

        final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();

        vv.setGraphMouse(graphMouse);

        JComboBox modeBox = graphMouse.getModeComboBox();
        modeBox.addItemListener(graphMouse.getModeListener());
        graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

        final ScalingControl scaler = new CrossoverScalingControl();

        JButton plus = new JButton("+");
        plus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                scaler.scale(vv, 1.1f, vv.getCenter());
            }
        });
        JButton minus = new JButton("-");
        minus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                scaler.scale(vv, 1 / 1.1f, vv.getCenter());
            }
        });

        JPanel scaleGrid = new JPanel(new GridLayout(1, 0));
        scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));

        JPanel controls = new JPanel();
        scaleGrid.add(plus);
        scaleGrid.add(minus);
        controls.add(scaleGrid);
        controls.add(modeBox);

        content.add(controls, BorderLayout.SOUTH);
    }

    private void createTree() {

        graph.addEdge("arrow", "Vmain", "Vsecond");
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        content.add(new TreeVisualiser());
        frame.pack();
        frame.setVisible(true);
    }
}
4

1 回答 1

2

哦,我错过了这行代码。现在它正在按我的意愿工作。

vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
于 2013-08-31T21:10:33.453 回答