[Jung] Controlling the Appearance of Vertices and Edges in Jung

In Jung, if you want to control of the vertices or edges, you need to provide the appropriate Transformer to the RenderContext in the BasicVisualizationServer. From the Transformer‘s documentation it is simply an interface that is “implemented by classes that transform one object into another“. In Jung the Transformer “transforms” a vertex or edge to a colour, shape, text or something else that controls their appearance.

For example, consider controlling the colour of the vertices. From the documentation for RenderContext, the colour of a vertex is controlled by setting a Transformer via:

void setVertexFillPaintTransformer(
    org.apache.commons.collections15.Transformer<V,Paint> 
    vertexFillPaintTransformer)

Notice that the argument is a Transformer with the type arguments V and Paint. To determine what this means, we need to look at the Transformer interface. The interface declaration for Transformer is:

public interface Transformer<I,O>;

The significance of the I and O type arguments is revealed by looking at the documentation for the only method that is defined by the interface:

O transform(I input)

If you had looked at its documentation, you would have read that the method transforms the input, of type I, to an object, of type O (which is also the object that is returned).

Now, Jung uses V to represent vertices and E to represent edges in generic type arguments. So, the transformer for setVertexFillPaintTransformer “transforms” a vertex into a Paint object, which will determine the the colour of the vertex. Jung allows you to return different Paint for different vertices. This is useful for making the vertices appear different. For example, if the vertices were an Integer, we could make the vertices one of three different with the following transformer:

Transformer<Integer, Paint> vertexPaint = 
    new Transformer<Integer, Paint>() {
        private final Color[] palette = { Color.GREEN, 
            Color.WHITE, Color.RED };

        public Paint transform(Integer i) {
            return palette[i.intValue() % palette.length];
        }
    };

The procedure for controlling other visual paramters of vertices and edges are very similar. As another quick example, consider controlling the shape of the vertices. Looking at the documentation for RenderContext, you would find the method setVertexShapeTransformer, which has the following signature:

void setVertexShapeTransformer(
    org.apache.commons.collections15.Transformer<V,Shape> 
    vertexShapeTransformer)

So, the method requires a Transformer that “transforms” a vertex to a Shape. Again, if the vertices were Integers, we could cycle the vertices through three different shapes with the following transformer:

Transformer<Integer, Shape> vertexShape = 
    new Transformer<Integer, Shape>() {
        private final Shape[] styles = { 
            new Rectangle(-20, -10, 40, 20),
            new Ellipse2D.Double(-25, -10, 50, 20),
            new Arc2D.Double(-30, -15, 60, 30, 30, 30, 
                Arc2D.PIE) };

        @Override
        public Shape transform(Integer i) {
            return styles[i.intValue() % styles.length];
        }
    };  

A complete example, using these two transformers, that can be executed can be downloaded from here. The following graph was produced using these two transformers:

Transformers changing the appearance of the vertices.

Advertisements

3 Responses to [Jung] Controlling the Appearance of Vertices and Edges in Jung

  1. l says:

    The first JUNG tutorial which was helpful. Thank you.

    PS: the example link is broken

  2. How to change the size of the vertex ?.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: