graphpad分析教程(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.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);
}
}
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com