本文主要是介绍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;elsereturn 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
这篇关于jung实践-拓扑图形绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!