I've created a graph with Jung and I'm looking into visualization options. I'm trying to change the color of the vertex labels, don't care if they are picked/unpicked, just from the get-go.
I.e. I want to paint the vertex labels white, leave edge labels black (default).
Can't seem to find a sample that works, the DefaultVertexLabelRenderer class should help I guess but I can't seem to get it to work. Any ideas? The code currently looks like this:
public static void main(String[] args) {
Graph<Node, Double> g = MakeGraph.makeManual(); //graph created somewhere
Layout<Node, Double> layout = new CircleLayout<Node, Double>(g);
layout.setSize(new Dimension(300,300));
VisualizationViewer<Node, Double> vv = new VisualizationViewer<Node, Double>(layout);
vv.setPreferredSize(new Dimension(500,500));
// Paints the vertex (as a shape)
Transformer<Node,Paint> vertexPaint = new Transformer<Node,Paint>() {
public Paint transform(Node node) {
if(node.getNodeType().equals(NodeType.SLA)) return new Color(228,108,10);//Color.ORANGE;
else return new Color(79,129,189);//Color.BLUE;
}
};
//Wanna do this: paint the label text
Transformer<Node,Paint> vertexLabelPaint = new Transformer<Node,Paint>() {
public Paint transform(Node node) {
return Color.WHITE;
}
};
//Sets the vertex label font
Transformer<Node,Font> vertexFont = new Transformer<Node,Font>() {
public Font transform(Node node) {
Font font = new Font("Calibri", Font.BOLD, 14);
return font;
}
};
//Sets the vertex shape's size, w.r.t. to the size of the text in the label
Transformer<Node,Shape> vertexSize = new Transformer<Node,Shape>(){
public Shape transform(Node node){
Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
Font font = new Font("Calibri", Font.BOLD, 14);
int textwidth = (int)(font.getStringBounds(node.toString(), frc).getWidth());
return AffineTransform.getScaleInstance(textwidth/14, 1).createTransformedShape(circle);
}
};
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.WHITE));
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint); //fill
vv.getRenderContext().setVertexDrawPaintTransformer(vertexPaint); //outline
vv.getRenderContext().setVertexFontTransformer(vertexFont);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>());
//COLOR ME WHITE! ^^
vv.getRenderContext().setVertexShapeTransformer(vertexSize);
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Double>());
//LEAVE ME BLACK ^^
DefaultModalGraphMouse<Node, Double> gm = new DefaultModalGraphMouse<Node, Double>();
// gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
vv.setGraphMouse(gm);
JFrame frame = new JFrame("Test Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}}
I tried
DefaultVertexLabelRenderer dvlr = new DefaultVertexLabelRenderer(Color.WHITE);
dvlr.setForeground(Color.WHITE);
vv.getRenderContext().setVertexLabelRenderer(dvlr);
to no avail.
One option is to override the method that returns the renderer component for the labels, and sets the foreground color accordingly. Here this is implemented as a MCVE (which, when it is already provided in the question, makes it much easier to quickly try it out and find a solution...)
(It's indeed a bit strange that there seems to be no easier way, but according to the implementation of
DefaultVertexLabelRenderer
, there is no way to influence the default label color with the renderer itself: It simply takes the foreground color from theVisualizationViewer
. (SovisualizationViewer.setForeground(Color.RED)
would change the color, but this would affect all labels...))