本文主要是介绍java中使用JUNG提供的API可视化图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JUNG是很强大的工具,可以可视化很多种类型的图,可以对如何进行可视化进行很多自定义的设置。笔者仅介绍最基础的用法,方便大家入门。下面给出一个例子:
假设你要建立的图是这个样子。
然后你建一个GraphPoet的类来保存这个图:
package helper;import edge.Edge;//笔者自己建立的保存Edge类,用来保存图的边
import edu.uci.ics.jung.algorithms.layout.CircleLayout;//JUNG提供的API
import edu.uci.ics.jung.algorithms.layout.Layout;//JUNG提供的API
import edu.uci.ics.jung.graph.SparseGraph;//JUNG提供的API
import edu.uci.ics.jung.graph.util.EdgeType;//JUNG提供的API
import edu.uci.ics.jung.visualization.BasicVisualizationServer;//JUNG提供的API
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;//JUNG提供的API
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;//JUNG提供的API
import graph.Graph; //笔者自己建立的保存图的类,里面用Set保存所有的边和所有的点
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Stroke;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import vertex.Vertex; //笔者自己建立的保存点的类public class GraphPoetView {SparseGraph<String, String> localGraph;//JUNGTransformer<String, Paint> vertexPaint;//JUNGTransformer<String, Stroke> edgeStrokeTransformer;//JUNGJFrame frame;/*** 构造函数.* @param graph : 自己建立好的graph*/public GraphPoetView(Graph graph) {createWindow();createGraph(graph);createTransformers();}private void createTransformers() {// Setup up a new vertex to paint transformer...this.vertexPaint = new Transformer<String, Paint>() {public Paint transform(String s) {return Color.GREEN;}};// Set up a new stroke Transformer for the edgesfinal Stroke edgeStroke = new BasicStroke();edgeStrokeTransformer = new Transformer<String, Stroke>() {public Stroke transform(String s) {return edgeStroke;}};}/*** 让graph以GUI的方式展示出来.*/public void showGraph() {// The Layout<V, E> is parameterized by the vertex and edge typesLayout<String, String> layout = new CircleLayout<String,String>(localGraph);// sets the initial size of the spacelayout.setSize(new Dimension(600, 600));// The BasicVisualizationServer<V,E> is parameterized by the edge typesBasicVisualizationServer<String, String> vv =new BasicVisualizationServer<String, String>(layout);// Sets the viewing area sizevv.setPreferredSize(new Dimension(650, 650)); // apply transformersvv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<String>());vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);frame.getContentPane().add(vv);frame.pack();frame.setVisible(true);}
//用你自己的Graph来建立可以被JUNG可视化的graphprivate void createGraph(Graph graph) {// Graph<V, E> where V is the type of the vertices and E is the type of// the edgeslocalGraph = new SparseGraph<String, String>();//可以可视化的graph// Add some verticesfor (Vertex v : graph.vertices()) {localGraph.addVertex(v.toString());}// Add some edges. default is for undirected edges.for (Edge edge : graph.edges()) {//addEdge有很多的重载方式。这里的方式为addEdge(Edge的标签,Edge的起始点的标签,Edge的终点的标签,Edge的类型) localGraph.addEdge(edge.toString(), edge.getVerticesOfEdge().get(0).toString(),edge.getVerticesOfEdge().get(1).toString(), EdgeType.DIRECTED);}public void createWindow() {frame = new JFrame("GraphPoetView");frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}
}
调用这个类来可视化的方法:
public void presentGraph() {//传递一个自己建立的graphGraphPoetView a = new GraphPoetView(this.graph);a.showGraph();}
更详细的JUNG的使用方法参见JUNG官网。
这篇关于java中使用JUNG提供的API可视化图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!