JUNG: unpicked vertex label color

2019-07-18 01:47发布

I can make picked vertex labels red using vertexLabelRenderer declared like this:

DefaultVertexLabelRenderer vertexLabelRenderer=new DefaultVertexLabelRenderer(Color.RED);

How do I change color of unpicked vertex labels. Constructor (see below) only allows defining unpicked vertex labels. Also method setForeground() does not affect the font color.

Here is the JUNG 2.0.1 API document: http://jung.sourceforge.net/site/apidocs/edu/uci/ics/jung/visualization/renderers/DefaultVertexLabelRenderer.html

2条回答
太酷不给撩
2楼-- · 2019-07-18 02:13

For anyone still scratching their head here's two simple classes that can change an unpicked vertex/edge label color.

public class MyDefaultVertexLaberRenderer extends DefaultVertexLabelRenderer
{
    protected Color unpickedVertexLabelColor = Color.BLACK;

    public MyDefaultVertexLaberRenderer(Color unpickedVertexLabelColor, Color pickedVertexLabelColor)
    {
        super(pickedVertexLabelColor);
        this.unpickedVertexLabelColor = unpickedVertexLabelColor;
    }

    public <V> Component getVertexLabelRendererComponent(JComponent vv, Object value, Font font, boolean isSelected, V vertex)
    {
        super.setForeground(unpickedVertexLabelColor);
        if (isSelected) setForeground(pickedVertexLabelColor);
        super.setBackground(vv.getBackground());
        if (font != null)
        {
            setFont(font);
        }
        else
        {
            setFont(vv.getFont());
        }
        setIcon(null);
        setBorder(noFocusBorder);
        setValue(value);
        return this;
    }
}

public class MyDefaultEdgeLaberRenderer extends DefaultEdgeLabelRenderer
{
    protected Color unpickedEdgeLabelColor = Color.BLACK;

    public MyDefaultEdgeLaberRenderer(Color unpickedEdgeLabelColor, Color pickedEdgeLabelColor)
    {
        super(pickedEdgeLabelColor);
        this.unpickedEdgeLabelColor = unpickedEdgeLabelColor;
    }

    public <E> Component getEdgeLabelRendererComponent(JComponent vv, Object value, Font font, boolean isSelected, E edge)
    {
        super.setForeground(unpickedEdgeLabelColor);
        if (isSelected) setForeground(pickedEdgeLabelColor);
        super.setBackground(vv.getBackground());

        if (font != null)
        {
            setFont(font);
        }
        else
        {
            setFont(vv.getFont());
        }
        setIcon(null);
        setBorder(noFocusBorder);
        setValue(value);
        return this;
    }
}
查看更多
来,给爷笑一个
3楼-- · 2019-07-18 02:17

Here is another way to set the color of unpicked vertex labels (to red):

vv.setForegroundColor(Color.red);

The vertex (and edge) labels use a JLabel, and they grab the foreground color from the visualization viewer.

登录 后发表回答