Package

Source Code of Main$VertexSize


import edu.uci.ics.jung.algorithms.layout.*;
import edu.uci.ics.jung.algorithms.scoring.util.VertexScoreTransformer;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedGraph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.visualization.decorators.AbstractEdgeShapeTransformer;
import edu.uci.ics.jung.visualization.decorators.AbstractVertexShapeTransformer;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.*;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import java.util.List;
import java.awt.Color;

/**
* Created by IntelliJ IDEA.
* User: whitepaper
* Date: 10.03.2011
* Time: 15:19:29
*/
public class Main {
    private static String delimeters = ",";
    private static int dimensionX =2400;
    private static int dimensionY =2400;
    static HashMap<String, AuthorNode> vertSet = new HashMap<String, AuthorNode>();

    static void connectAll(String[] verteces, UndirectedGraph<AuthorNode, RefLink> graph) {
        addAllVertices(verteces, graph);
        for (int i = 0; i < verteces.length; ++i) {
            for (int j = i + 1; j < verteces.length; ++j) {
                AuthorNode a = new AuthorNode(verteces[i]);
                AuthorNode b = new AuthorNode(verteces[j]);
                RefLink edge = graph.findEdge(a, b);

                if (edge == null) {
                    graph.addEdge(new RefLink(a, b), a, b);
                } else {
                    edge.add();
                }
            }
        }
    }

    static void addAllVertices(String[] verteces, UndirectedGraph graph) {
        for (String v : verteces) {
            if (vertSet.containsKey(v)) {
                vertSet.get(v).addPub();
            } else {
                AuthorNode newNode = new AuthorNode(v);
                graph.addVertex(newNode);
                vertSet.put(v, newNode);
            }

        }
    }

    static UndirectedGraph composeGraph(File inFile) throws Exception {
        UndirectedGraph graph = new UndirectedSparseGraph<AuthorNode, RefLink>();
        List<String> lines = IOUtils.readLines(new FileInputStream(inFile));
        for (String line : lines) {
           
            char[] spaceTest = new char[line.length()/2]     ;              
                     for (int i=0,j=0;i<line.length();i++) {
                         if((0x0040<line.codePointAt(i) & line.codePointAt(i)<0x007b)| line.codePointAt(i)== 0x002c | line.codePointAt(i)== 0x0020 ){
                             spaceTest[j]=line.charAt(i);
                             j++;
                         }
                       }
           String line2 = new String(spaceTest);
            String line3 = line2.trim();
       
           
            String[] tokens = line3.split(",\\s*");
            connectAll(tokens, graph);
        }
        return graph;
    }

    static void processContext(Graph<AuthorNode, RefLink> graph, RenderContext cont) {
       
        Transformer<AuthorNode, Integer> nodeToInt = new Transformer<AuthorNode, Integer>(){
            //
            public Integer transform(AuthorNode authorNode) {
                return authorNode.getPublications();
            }
        };
        SimpleScorer<AuthorNode> scorer = new SimpleScorer<AuthorNode>(50, 15, nodeToInt, graph.getVertices());
        Transformer<AuthorNode, Integer> scorerTrans = new VertexScoreTransformer<AuthorNode, Integer>(scorer);

        Transformer<RefLink, Integer> edgeToInt = new Transformer<RefLink, Integer>() {
            public Integer transform(RefLink refLink) {
                return refLink.getPubCount();
            }
        };
        final SimpleScorer <RefLink> edgeScorer = new SimpleScorer<RefLink>(12, 1, edgeToInt, graph.getEdges());
        Transformer<RefLink, Stroke> edgeStroke = new Transformer<RefLink, Stroke>() {
            public Stroke transform(RefLink refLink) {
                return new BasicStroke(edgeScorer.getEdgeScore(refLink));
            }
        };
       
        VertexSize vs = new VertexSize<AuthorNode, RefLink>(scorerTrans);
        cont.setVertexShapeTransformer(vs);
      
        cont.setEdgeDrawPaintTransformer(new Transformer <RefLink, Paint>() {
       
            public Paint transform(RefLink refLink) {
            return new Color(0,191,255);
            }
        });
        cont.setVertexLabelTransformer(new ToStringLabeller());
        cont.setEdgeShapeTransformer(new EdgeShape.Line<AuthorNode, RefLink>());
        cont.setEdgeStrokeTransformer(edgeStroke);
        cont.setVertexFontTransformer(new Transformer<AuthorNode, Font>() {
            public Font transform(AuthorNode authorNode) {
                return new Font("Verdana", Font.BOLD, 23);
           }
       });
       

        final SimpleScorer<AuthorNode> colorScorer = new SimpleScorer<AuthorNode>(100, 15, nodeToInt, graph.getVertices());
        cont.setVertexFillPaintTransformer(new Transformer<AuthorNode, Paint>() {
            public Paint transform(AuthorNode authorNode) {
                Color baseColor = Color.GREEN;
                float[] hsb = null;
                hsb = Color.RGBtoHSB(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), hsb);
                hsb[2] = (float) colorScorer.getVertexScore(authorNode) / 100;
                return Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
            }
        });
    }
    static void visualize(Graph<AuthorNode, RefLink> g) {
        Layout<AuthorNode, RefLink> layout = new CircleLayout(g);
        layout.setSize(new Dimension(2400, 2400));
        BasicVisualizationServer serv = new BasicVisualizationServer(layout);
        serv.setPreferredSize(new Dimension(2400, 2400));

        RenderContext context = serv.getRenderContext();
        processContext(g, context);


        JFrame frame = new JFrame("Simple Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(serv);
        frame.pack();
        frame.setVisible(true);
    }

    static Image produceImage(Graph g) {
        Layout<AuthorNode, RefLink> layout = new FRLayout(g);
        layout.setSize(new Dimension(dimensionX - 100, dimensionY - 100));
       
        //VisualizationImageServer serv = new VisualizationImageServer(layout, new Dimension(dimensionX, dimensionY));
        VisualizationImageServer serv = new VisualizationImageServer(layout, new Dimension(dimensionX, dimensionY));
//        serv.setPreferredSize(new Dimension(750, 750));

//        serv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        processContext(g, serv.getRenderContext())//What's up?
//        serv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        return serv.getImage(new Point2D.Double(dimensionX / 2, dimensionY / 2 ), new Dimension(dimensionX, dimensionY));
    }

    public static void main(String[] args) throws Exception {
        File inputFile = new File("resources", "in.txt");

        UndirectedGraph<AuthorNode, RefLink> g = composeGraph(inputFile);
        Image image = produceImage(g);

        File imageFile = new File("out.jpeg");
//        visualize(g);
        ImageIO.write((BufferedImage) image, "jpeg", imageFile);
    }

    private final static class VertexSize<V, E> extends AbstractVertexShapeTransformer<V>
            implements Transformer<V, Shape>{
        protected final Transformer<V, Integer> scores;
//        protected Graph<V, E> graph;

        public VertexSize(final Transformer<V, Integer> scores) {
//            this.graph = graph;
            this.scores = scores;

            setSizeTransformer(new Transformer<V, Integer>() {
                public Integer transform(V v) {
                    return scores.transform(v).intValue();
                }
            });

        }
        public Shape transform(V v) {
            return factory.getRectangle(v);
        }
    }




}


TOP

Related Classes of Main$VertexSize

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.