13

How do you change the size of a specific vertex in Jung Visualization Library?

I'm reading the documentation but I'm not extremely familiar with java and I can't find any good examples on line.

Cajunluke
  • 3,103
  • 28
  • 28
KWJ2104
  • 1,959
  • 6
  • 38
  • 53
  • I looked into JUNG previously, and I would not recommend it if you're not familiar with Java - the documentation is terrible, and the userbase is nonexistent. That said, what version are you using? If possible, could you point to the specific jars? The code has changed quite a bit. – Allen Z. Dec 10 '11 at 20:50
  • Allen, I get an awful lot of questions for JUNG for a library with a nonexistent userbase. :) If there are specific aspects of the documentation that you'd like to see improved, please post something to the web forum or to the jung-support list. – Joshua O'Madadhain Dec 14 '11 at 18:03

2 Answers2

29

It took me a while, but here is a readable, fully-commented program that changes the vertex size and color in a graph. Enjoy!

public class SimpleGraphView {
    public SimpleGraphView() {
        // Create a graph with Integer vertices and String edges
        Graph<Integer, String> g = new SparseGraph<Integer, String>();
        for(int i = 0; i < 5; i++) g.addVertex(i);
        g.addEdge("Edge", 1, 2);
        g.addEdge("Another Edge", 1, 4);

        // Layout implements the graph drawing logic
        Layout<Integer, String> layout = new CircleLayout<Integer, String>(g);
        layout.setSize(new Dimension(300,300));

        // VisualizationServer actually displays the graph
        BasicVisualizationServer<Integer,String> vv = new BasicVisualizationServer<Integer,String>(layout);
        vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size

        // Transformer maps the vertex number to a vertex property
        Transformer<Integer,Paint> vertexColor = new Transformer<Integer,Paint>() {
            public Paint transform(Integer i) {
                if(i == 1) return Color.GREEN;
                return Color.RED;
            }
        };
        Transformer<Integer,Shape> vertexSize = new Transformer<Integer,Shape>(){
            public Shape transform(Integer i){
                Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
                // in this case, the vertex is twice as large
                if(i == 2) return AffineTransform.getScaleInstance(2, 2).createTransformedShape(circle);
                else return circle;
            }
        };
        vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);
        vv.getRenderContext().setVertexShapeTransformer(vertexSize);

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

    public static void main(String[] args) {
        new SimpleGraphView();
    }
}
Allen Z.
  • 1,560
  • 1
  • 11
  • 19
3

PluggableRendererDemo, shown here, illustrates how to change the size, shape and aspect of vertices.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    You can find the latest source code of this example here: https://github.com/jrtom/jung/blob/master/jung-samples/src/main/java/edu/uci/ics/jung/samples/PluggableRendererDemo.java – amoebe Oct 28 '16 at 12:25