Skip to content

Learning JUNG (2) – Adding labels

Last updated on 2012-12-11

That was very easy. First I changed the VisualizationImageServer for a VisualizationViewer. The name of the variable was also changed from vs to vv. The new class is the one used in all the JUNG examples, therefore it is probably better (talk about programming by coincidence). After doing this, I added the following lines after the creation of the VisualizationViewer:

Transformer transformer = new Transformer() {
  @Override public String transform(String arg0) { return arg0; }
};
vv.getRenderContext().setVertexLabelTransformer(transformer);
transformer = new Transformer() {
  @Override public String transform(String arg0) { return arg0; }
};
vv.getRenderContext().setEdgeLabelTransformer(transformer);

Transformer is an apache commons collections library that simply defines a one-way transformation between objects of two types (why isn’t this part of the standard Java libraries???). After defining the Transformer, it is added to the visualization engine using the specified functions. While my example is pretty simple, you can create more complex transformations the same way, and the edge and vertex transformations are independent.

Coming next (hopefully without much delay): changing the vertex’s shape.

Published inProgramming

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Musings of a Strange Loop

Subscribe now to keep reading and get access to the full archive.

Continue reading