Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中生成Treelayout图_Java_Layout_Graph_Jung_Jgrapht - Fatal编程技术网

如何在Java中生成Treelayout图

如何在Java中生成Treelayout图,java,layout,graph,jung,jgrapht,Java,Layout,Graph,Jung,Jgrapht,我正在为一个程序工作,该程序读取一些xml文件,并根据这些xml文件中的信息创建图表 我做图表有困难。我希望我的图表看起来: 所以,我希望它有一个树形图,顶点的名称在一些彩色矩形内(pref对不同类型的项目有不同的颜色),我还希望在两个顶点之间的边缘上有一些文字。除此之外,我非常希望顶点是我在项目中创建的一些对象,因此当我单击顶点以获取放置在顶点上的对象的实例时 到目前为止,我尝试了两种框架来轻松绘制图形,而无需对绘制算法进行太多工作: 起初,我尝试了jgraph Framework:

我正在为一个程序工作,该程序读取一些xml文件,并根据这些xml文件中的信息创建图表

我做图表有困难。我希望我的图表看起来:

所以,我希望它有一个树形图,顶点的名称在一些彩色矩形内(pref对不同类型的项目有不同的颜色),我还希望在两个顶点之间的边缘上有一些文字。除此之外,我非常希望顶点是我在项目中创建的一些对象,因此当我单击顶点以获取放置在顶点上的对象的实例时

到目前为止,我尝试了两种框架来轻松绘制图形,而无需对绘制算法进行太多工作:

起初,我尝试了jgraph Framework:

    package mainPack;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    import javax.swing.JApplet;
    import javax.swing.JFrame;

    import org.jgraph.JGraph;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.GraphConstants;
    import org.jgrapht.ListenableGraph;
    import org.jgrapht.ext.JGraphModelAdapter;
    import org.jgrapht.graph.ListenableDirectedGraph;
    import org.jgrapht.graph.DefaultEdge;
    import org.jgrapht.graph.ListenableDirectedWeightedGraph;

    /**
     * A demo applet that shows how to use JGraph to visualize JGraphT graphs.
     *
     * @author Barak Naveh
     *
     * @since Aug 3, 2003
     */
    public class Test extends JApplet {
  private static final Color     DEFAULT_BG_COLOR = Color.decode( "#FAFBFF" );
            private static final Dimension DEFAULT_SIZE = new Dimension( 530, 320 );

    // 
    private JGraphModelAdapter m_jgAdapter;

    /**
     * @see java.applet.Applet#init().
     */
    public void init(  ) {
        // create a JGraphT graph
        ListenableGraph g = new ListenableDirectedGraph( DefaultEdge.class );

        // create a visualization using JGraph, via an adapter
        m_jgAdapter = new JGraphModelAdapter( g );

        JGraph jgraph = new JGraph( m_jgAdapter );

        adjustDisplaySettings( jgraph );
        getContentPane(  ).add( jgraph );
        resize( DEFAULT_SIZE );

        // add some sample data (graph manipulated via JGraphT)
        g.addVertex( "v1" );
        g.addVertex( "v2" );
        g.addVertex( "v3" );
        g.addVertex( "v4" );

        g.addEdge( "v1", "v2", "1" );
        g.addEdge( "v2", "v3", "2" );
        g.addEdge( "v3", "v1", "3" );
        g.addEdge( "v4", "v3", "4" );

        // position vertices nicely within JGraph component
        positionVertexAt( "v1", 130, 40 );
        positionVertexAt( "v2", 60, 200 );
        positionVertexAt( "v3", 310, 230 );
        positionVertexAt( "v4", 380, 70 );

        // that's all there is to it!...
    }


    private void adjustDisplaySettings( JGraph jg ) {
        jg.setPreferredSize( DEFAULT_SIZE );

        Color  c        = DEFAULT_BG_COLOR;
        String colorStr = null;

        try {
            colorStr = getParameter( "bgcolor" );
        }
         catch( Exception e ) {}

        if( colorStr != null ) {
            c = Color.decode( colorStr );
        }

        jg.setBackground( c );
    }


    private void positionVertexAt( Object vertex, int x, int y ) {
        DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );

        Map             attr = cell.getAttributes();
   //      Rectangle       b    =  new Rectangle((int)(Math.random()*1000),(int)(Math.random()*500),100,30);

        Rectangle       b    =  new Rectangle(20,50 ,100,30);



       GraphConstants.setBounds( attr, b );

         Map cellAttr = new HashMap(  );
         cellAttr.put( cell, attr );

         m_jgAdapter.edit( cellAttr, null, null, null );
    }
}

该框架可以很好地制作图形,在矩形中使用顶点名称,在边上使用名称,并且可以在使用MouseStener单击顶点时获得顶点内的字符串

但是我找不到一种方法使它有一个树形图,并添加顶点作为一些对象,当单击顶点时,我可能会返回这些对象。我尝试使用本例中的泛型类,但在尝试运行时只得到异常。我在互联网上搜索过,但找不到在这个图上应用treelayout的方法

我还尝试了JavaJung框架,它有很多用于绘制图形的选项。我发现这个例子:

    @SuppressWarnings("serial")
    public class TreeLayoutDemo extends JApplet {

/**
 * the graph
 */
Forest<String,Integer> graph;

Factory<DirectedGraph<String,Integer>> graphFactory = 
    new Factory<DirectedGraph<String,Integer>>() {

        public DirectedGraph<String, Integer> create() {
            return new DirectedSparseMultigraph<String,Integer>();
        }
    };

Factory<Tree<String,Integer>> treeFactory =
    new Factory<Tree<String,Integer>> () {

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

Factory<Integer> edgeFactory = new Factory<Integer>() {
    int i=0;
    public Integer create() {
        return i++;
    }};

Factory<String> vertexFactory = new Factory<String>() {
    int i=0;
    public String create() {
        return "V"+i++;
    }};

/**
 * the visual component and renderer for the graph
 */
VisualizationViewer<String,Integer> vv;

VisualizationServer.Paintable rings;

String root;

TreeLayout<String,Integer> treeLayout;

RadialTreeLayout<String,Integer> radialLayout;

public TreeLayoutDemo() {

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





    createTree();

    treeLayout = new TreeLayout<String,Integer>(graph);
    radialLayout = new RadialTreeLayout<String,Integer>(graph);
    radialLayout.setSize(new Dimension(600,600));
    vv =  new VisualizationViewer<String,Integer>(treeLayout, new Dimension(600,600));
    vv.setBackground(Color.white);
   vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
   // vv.getRenderContext().setVertexShapeTransformer(arg0);
    // add a listener for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller());
    vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
    rings = new Rings();

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

    JToggleButton radial = new JToggleButton("Radial");
    radial.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED) {

                LayoutTransition<String,Integer> lt =
                    new LayoutTransition<String,Integer>(vv, treeLayout, radialLayout);
                Animator animator = new Animator(lt);
                animator.start();
                vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
                vv.addPreRenderPaintable(rings);
            } else {
                LayoutTransition<String,Integer> lt =
                    new LayoutTransition<String,Integer>(vv, radialLayout, treeLayout);
                Animator animator = new Animator(lt);
                animator.start();
                vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
                vv.removePreRenderPaintable(rings);
            }
            vv.repaint();
        }});

    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(radial);
    controls.add(scaleGrid);
    controls.add(modeBox);

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

class Rings implements VisualizationServer.Paintable {

    Collection<Double> depths;

    public Rings() {
        depths = getDepths();
    }

    private Collection<Double> getDepths() {
        Set<Double> depths = new HashSet<Double>();
        Map<String,PolarPoint> polarLocations = radialLayout.getPolarLocations();
        for(String v : graph.getVertices()) {
            PolarPoint pp = polarLocations.get(v);
            depths.add(pp.getRadius());
        }
        return depths;
    }

    public void paint(Graphics g) {
        g.setColor(Color.lightGray);

        Graphics2D g2d = (Graphics2D)g;
        Point2D center = radialLayout.getCenter();

        Rectangle2D rectangle = new Rectangle2D.Double();   // (center.getX()-10, center.getY()-20, 20, 40);

        Ellipse2D ellipse = new Ellipse2D.Double();
        for(double d : depths) {


            ellipse.setFrameFromDiagonal(center.getX()-d, center.getY()-d, 
                    center.getX()+d, center.getY()+d);

            rectangle.setFrameFromDiagonal(center.getX()-d, center.getY()-d, 
                    center.getX()+d, center.getY()+d);

            Shape shape = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).transform(rectangle);
            g2d.draw(shape);
        }
    }

    public boolean useTransform() {
        return true;
    }
}

/**
 * 
 */
private void createTree() {
    graph.addVertex("V0");
    graph.addEdge(edgeFactory.create(), "V0", "V1"); 
    graph.addEdge(edgeFactory.create(), "V0", "V2");
    graph.addEdge(edgeFactory.create(), "V1", "V4");
    graph.addEdge(edgeFactory.create(), "V2", "V3");
    graph.addEdge(edgeFactory.create(), "V2", "V5");
    graph.addEdge(edgeFactory.create(), "V4", "V6");
    graph.addEdge(edgeFactory.create(), "V4", "V7");
    graph.addEdge(edgeFactory.create(), "V3", "V8");
    graph.addEdge(edgeFactory.create(), "V6", "V9");
    graph.addEdge(edgeFactory.create(), "V4", "V10");

    graph.addVertex("A0");
    graph.addEdge(edgeFactory.create(), "A0", "A1");
    graph.addEdge(edgeFactory.create(), "A0", "A2");
    graph.addEdge(edgeFactory.create(), "A0", "A3");

    graph.addVertex("B0");
    graph.addEdge(edgeFactory.create(), "B0", "B1");
    graph.addEdge(edgeFactory.create(), "B0", "B2");
    graph.addEdge(edgeFactory.create(), "B1", "B4");
    graph.addEdge(edgeFactory.create(), "B2", "B3");
    graph.addEdge(edgeFactory.create(), "B2", "B5");
    graph.addEdge(edgeFactory.create(), "B4", "B6");
    graph.addEdge(edgeFactory.create(), "B4", "B7");
    graph.addEdge(edgeFactory.create(), "B3", "B8");
    graph.addEdge(edgeFactory.create(), "B6", "B9");




}


/**
 * a driver for this demo
 */
public static void main(String[] args) {
    JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    content.add(new TreeLayoutDemo());
    frame.pack();
    frame.setVisible(true);
}
@SuppressWarnings(“串行”)
公共类TreeLayoutDemo扩展了JApplet{
/**
*图表
*/
森林图;
工厂图形工厂=
新工厂(){
公共DirectedGraph创建(){
返回新的DirectedSparMultigraph();
}
};
工厂树=
新工厂(){
公共树创建(){
返回新的DelegateTree(graphFactory);
}
};
Factory edgeFactory=新工厂(){
int i=0;
公共整数创建(){
返回i++;
}};
工厂顶点工厂=新工厂(){
int i=0;
公共字符串create(){
返回“V”+i++;
}};
/**
*图形的可视组件和渲染器
*/
可视化查看器vv;
VisualizationServer.Paintable rings;
弦根;
TreeLayout TreeLayout;
辐射线布置辐射线;
公共树elayoutdemo(){
//为演示创建一个简单的图形
graph=新的DelegateForest();
createTree();
treeLayout=新的treeLayout(图形);
radialLayout=新的RadialTreeLayout(图形);
半径调整尺寸(新尺寸(600600));
vv=新的VisualizationViewer(treeLayout,新维度(600600));
vv.立根地面(颜色:白色);
vv.getRenderContext().setEdgeShapeTransformer(新EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(新ToStringLabeler());
//getRenderContext().setVertexShapeTransformer(arg0);
//添加工具提示的侦听器
vv.调压变压器(新ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(新ConstantTransformer(Color.lightGray));
环=新环();
容器内容=getContentPane();
最终图形ZoomScrollPane面板=新图形ZoomScrollPane(vv);
内容。添加(面板);
final DefaultModalGraphMouse graphMouse=新的DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
JComboBox modeBox=graphMouse.getModeComboBox();
addItemListener(graphMouse.getModelListener());
graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);
最终缩放控制缩放器=新的交叉缩放控制();
JButton plus=新JButton(“+”);
plus.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
scaler.scale(vv,1.1f,vv.getCenter());
}
});
JButton减号=新JButton(“-”);
减去.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
scaler.scale(vv,1/1.1f,vv.getCenter());
}
});
JToggleButton径向=新的JToggleButton(“径向”);
addItemListener(新的ItemListener(){
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
LayoutTransition lt=
新布局转换(vv、treeLayout、radialLayout);
Animator Animator=新动画师(lt);
animator.start();
vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.addPreRenderPaintable(环);
}否则{
LayoutTransition lt=
新布局转换(vv、radialLayout、treeLayout);
Animator Animator=新动画师(lt);
animator.start();
vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.可拆卸预渲染可喷漆(环);
}
vv.repaint();
}});
JPanel scaleGrid=新的JPanel(新的网格布局(1,0));
scaleGrid.setBorder(BorderFactory.createTitleBorder(“缩放”);
JPanel controls=新的JPanel();
scaleGrid.add(加);
scaleGrid.add(减);
控件。添加(径向);
控件。添加(scaleGrid);
控件。添加(modeBox);
添加(控件,BorderLayout.SOUTH);
}
类实现VisualizationServer.Paintable{
收集深度;
公众戒指(){
深度=获取深度();
}
私人收藏{
设置深度=新的HashSet();
Map polarLocations=radialLayout.getPolarLocations();
对于(字符串v:graph.getVertices()){
PolarPoint pp=polarLocations.get(v);
decepts.add(pp.getRadius());
}
返回深度;
}
公共空间涂料(图g){
g、 setColor(颜色:浅灰色);
Graphics2D g2d=(Graphics2D)g;
Point2D center=radialyout.getCenter();
矩形2D矩形=新矩形2D.Double();/(cente)
    vv.getRenderContext().setVertexShapeTransformer(new Transformer<String, Shape>()
    {
        @Override
        public Shape transform(String vertex)
        {
            return new Rectangle2D.Double(-10,-10,40,20);
        }
    });

    BasicVertexLabelRenderer<String, Integer> vertexLabelRenderer = 
        new BasicVertexLabelRenderer<String, Integer>();
    vertexLabelRenderer.setPosition( Renderer.VertexLabel.Position.CNTR);
    vv.getRenderer().setVertexLabelRenderer(vertexLabelRenderer);