Learning JUNG (2) – Adding labels

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>