最近在研究涉及到网络中的算路问题,自然会涉及到图相关的知识。经验表明好的数据结构往往比算法本身更为重要。
JUNG (Java Universal Network/Graph Framework) 是一个通用的可扩展的,用来创建图表的类库。一个用Java来建模、分析和做可视化图表的框架。官网:.html
先看下示例绘制图形:
使用的依赖有:
<dependency><groupId>net.sf.jung</groupId><artifactId>jung-graph-impl</artifactId><version>2.1.1</version></dependency><!-- .sf.jung/jung-visualization --><dependency><groupId>net.sf.jung</groupId><artifactId>jung-visualization</artifactId><version>2.1.1</version></dependency><!-- .sf.jung/jung-algorithms --><dependency><groupId>net.sf.jung</groupId><artifactId>jung-algorithms</artifactId><version>2.1.1</version></dependency><!-- .llections/collections-generic --><dependency><groupId>llections</groupId><artifactId>collections-generic</artifactId><version>4.01</version></dependency>
相关内容参考注释:
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.aph.SparseGraph;
import edu.uci.aph.util.EdgeType;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.l.DefaultModalGraphMouse;
import edu.uci.ics.l.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);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);}
}
该开源的的源码下载:
本文发布于:2024-01-30 19:45:48,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170661515022390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |