Jung可视化中的顶点标签颜色

时间:2016-02-09 19:24:11

标签: java jung jung2

我已经用荣格创建了一个图表,我正在研究可视化选项。我试图改变顶点标签的颜色,不管是从一开始就选择/取消它们都要小心。

即。我想将顶点标签绘制为白色,将边缘标签留为黑色(默认)。

似乎找不到有效的样本,DefaultVertexLabelRenderer类应该可以帮助我猜,但我似乎无法让它工作。有任何想法吗?代码目前看起来像这样:

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);

}}

我试过

DefaultVertexLabelRenderer dvlr = new DefaultVertexLabelRenderer(Color.WHITE);
     dvlr.setForeground(Color.WHITE);
     vv.getRenderContext().setVertexLabelRenderer(dvlr);

无济于事。

1 个答案:

答案 0 :(得分:0)

一个选项是覆盖返回标签的渲染器组件的方法,并相应地设置前景色。这里实现为MCVE(当问题中已经提供时,可以更快地尝试并找到解决方案......)

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JComponent;
import javax.swing.JFrame;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;

public class JungVertexLabelColors 
{
    public static void main(String[] args) 
    {
        JFrame jf = new JFrame();
        final Graph<String, String> g = getGraph();
        final VisualizationViewer<String, String> vv = 
            new VisualizationViewer<String, String>(
                new FRLayout<String, String>(g));
        DefaultModalGraphMouse<Node, Double> graphMouse = 
            new DefaultModalGraphMouse<Node, Double>();
        vv.setGraphMouse(graphMouse); 


        final Color vertexLabelColor = Color.RED;
        DefaultVertexLabelRenderer vertexLabelRenderer = 
            new DefaultVertexLabelRenderer(vertexLabelColor)
        {
            @Override
            public <V> Component getVertexLabelRendererComponent(
                JComponent vv, Object value, Font font, 
                boolean isSelected, V vertex) 
            {
                super.getVertexLabelRendererComponent(
                    vv, value, font, isSelected, vertex);
                setForeground(vertexLabelColor);
                return this;
            }
        };
        vv.getRenderContext().setVertexLabelRenderer(vertexLabelRenderer);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());



        jf.getContentPane().add(vv);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

    public static Graph<String, String> getGraph() 
    {
        Graph<String, String> g = new DirectedSparseGraph<String, String>();
        g.addVertex("v0");
        g.addVertex("v1");
        g.addVertex("v2");
        g.addVertex("v3");
        g.addVertex("v4");
        g.addEdge("e0", "v0", "v1");
        g.addEdge("e1", "v1", "v2");
        g.addEdge("e2", "v2", "v3");
        g.addEdge("e3", "v3", "v4");
        g.addEdge("e4", "v4", "v0");
        g.addEdge("e5", "v1", "v3");
        g.addEdge("e6", "v2", "v4");
        return g;
    }    
}

(确实有点奇怪,似乎没有更简单的方法,但根据DefaultVertexLabelRenderer的实现,没有办法用渲染器本身影响默认标签颜色:它只需从VisualizationViewer获取前景色。(所以visualizationViewer.setForeground(Color.RED)会改变颜色,但这会影响所有标签...))