0
votes

Does anyone know if you are able to set the colour of the text for a vertex label in JUNG.

I'm using the Visualisation Viewer and can seem to be able to set the colour for everything else.

vv =  new VisualizationViewer<String,Integer>(treeLayout, new Dimension(410,557));
Transformer<String,Paint> vertexPaint = new Transformer<String,Paint>() {
   public Paint transform(String b) {
      return Color.orange;
   }
};

vv.setBackground(Color.white);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
//vv.getRenderContext().setVertexFontTransformer(vertexFont);


// add a listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.WHITE));
3
I tried that but it didn't work @demongolem - user3469624
Define "didn't work". - Joshua O'Madadhain
The text of the label did not change colour @JoshuaO'Madadhain - user3469624

3 Answers

1
votes

The DefaultVertexLabelRenderer and the DefaultEdgeLabelRenderer extend JLabel (it is similar to the way cell renderers work in JTable and JTree). By default, it uses the foreground color of the VisualizationViewer to draw the label text.

vv.setForegroundColor(Color.red);

will make all of your labels red. This approach is less expensive than making all of the labels parse HTML.

Sorry that the solution is so obscure.

Additionally, since the default renderers extend JLabel, the use of html is the same as it is for JLabel. There are good online resources to show examples of using html with javax.swing. What's missing is documentation to make the connection between using html in JUNG and using html in javax.swing.

0
votes

You can use HTML in the label to specify the color; an example is here: https://stackoverflow.com/a/2017576/664856

0
votes

In your case,

vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.RED));

should work (if you wanted selected vertex to be Red). I tested it myself. This applies to the selected vertex.

Upon inspection of code, I would have to believe that the link I provided does correctly work for those vertices which are not selected, but I did not actually try implementing that link.