jung实践-拓扑图形绘制

最近在研究涉及到网络中的算路问题,自然会涉及到图相关的知识。经验表明好的数据结构往往比算法本身更为重要。

JUNG (Java Universal Network/Graph Framework) 是一个通用的可扩展的,用来创建图表的类库。一个用Java来建模、分析和做可视化图表的框架。官网:http://jung.sourceforge.net/site/jung-samples/source-repository.html

先看下示例绘制图形:

 

使用的依赖有:

        <dependency>
            <groupId>net.sf.jung</groupId>
            <artifactId>jung-graph-impl</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.jung/jung-visualization -->
        <dependency>
            <groupId>net.sf.jung</groupId>
            <artifactId>jung-visualization</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.jung/jung-algorithms -->
        <dependency>
            <groupId>net.sf.jung</groupId>
            <artifactId>jung-algorithms</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.collections/collections-generic -->
        <dependency>
            <groupId>net.sourceforge.collections</groupId>
            <artifactId>collections-generic</artifactId>
            <version>4.01</version>
        </dependency>
相关内容参考注释:
 
package com.zte.sunquan.demo.ui;

import java.awt.*;
import javax.swing.*;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import javafx.scene.shape.StrokeType;


public class SQFrame2 extends JFrame {
    private SparseGraph g;

    private void initGraph() {
        g = new SparseGraph();
        for (int i = 1; i < 10; i++) {
            g.addVertex(i);
            g.addEdge("Edge[1," + (i + 1) + "]", 1, i + 1);
            if (i > 1) {
                g.addEdge("Edge[" + i + "," + (i + 1) + "]", i, i + 1, EdgeType.DIRECTED);
            }
        }
        System.out.println("The graph g = " + g.toString());
    }
    public SQFrame2() {
        this.setTitle("Example");
        this.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        this.setBackground(Color.white);// 设置窗口背景颜色

        initGraph();

        //创建viewer 圆形布局结构(V,E节点和链路类型)
        VisualizationViewer<Integer, String> vv =
                new VisualizationViewer<Integer, String>(new CircleLayout(g));

        // 设置顶点文本标签
        vv.getRenderContext()
                .setVertexLabelTransformer(new ToStringLabeller());

        // 设置顶点颜色
        vv.getRenderContext()
                .setVertexFillPaintTransformer((p) -> {
            if (p == 1)
                return Color.green;
            else
                return Color.YELLOW;
        });

        // 设置边的文本标签
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        // 设置边的线型
         vv.getRenderContext().setEdgeStrokeTransformer(p->{
             return new BasicStroke(5f);
         });

        DefaultModalGraphMouse<Integer, String> gm = new DefaultModalGraphMouse<Integer, String>();
        gm.setMode(Mode.PICKING);
        vv.setGraphMouse(gm);
        // 将上述对象放置在一个Swing容器中并显示之
        getContentPane().add(vv);
        pack();
    }

    public static void main(String[] args) {
        SQFrame2 myframe = new SQFrame2();
        myframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
        myframe.setVisible(true);
    }
}

该开源的的源码下载:https://download.csdn.net/download/sunquan291/10589518

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值