grafos.JUNGraph.java Source code

Java tutorial

Introduction

Here is the source code for grafos.JUNGraph.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package grafos;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Stroke;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections15.Transformer;

/**
 *
 * @author stephany
 */

public class JUNGraph {

    Graph<Integer, String> g;

    public JUNGraph(List<Grafo> grafo) {

        g = new DirectedSparseMultigraph<>();
        Map<Integer, Vertex> extractVertice = NetworkDiscovery.extractVertice(grafo);
        for (Integer graphkey : extractVertice.keySet()) {
            g.addVertex(graphkey);
        }

        for (int i = 0; i < grafo.size(); i++) {
            Grafo grafoObj = grafo.get(i);

            String labelSpace = "           ";
            for (int k = 0; k <= i; k++) {
                labelSpace += " ";
            }

            String link = labelSpace + String.valueOf(grafoObj.peso);
            g.addEdge(link, grafoObj.origem, grafoObj.destino);

        }
    }

    /**
     * Cria visualizao grfica do grafo
     * @param grafo 
     * @return  
     */
    //public static void createGraphVisualization(List<Grafo> grafo, List<Vertex> path, String origem, String destino){
    public static BasicVisualizationServer<Integer, String> createGraphVisualization(List<Grafo> grafo) {

        JUNGraph sgv = new JUNGraph(grafo);

        Layout<Integer, String> layout = new CircleLayout(sgv.g);
        layout.setSize(new Dimension(500, 500));
        BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<>(layout);
        vv.setPreferredSize(new Dimension(500, 500));

        Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
            @Override
            public Paint transform(Integer i) {
                return Color.ORANGE;
            }
        };

        float dash[] = { 5.0f };
        final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash,
                0.0f);
        Transformer<String, Stroke> edgeStrokeTransformer = new Transformer<String, Stroke>() {
            @Override
            public Stroke transform(String s) {
                return edgeStroke;
            }
        };

        vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
        vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());

        vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);

        return vv;

    }

}