Search code examples
javaswingjung2

JUNG:How to add different color edge in the same graph


I am developing one application using Jung2 which will show the connection type between two point,there is two different type of connection are available.I want to show each type of connection using different color.For that i want to add two different colored edge between two vertex from A to B it will be black and B to A it will be green.Can any one help me with an example...

enter image description here


Solution

  • You have to define a Transformer that receives an edge and returns a Paint - in this case, the Paint can simply be a Color. This Transformer has to be assigned to the RenderContext as

    • ArrowFillPaintTransformer
    • ArrowDrawPaintTransformer
    • EdgeDrawPaintTransformer

    The decision of whether the edge has to be painted in green or in black is based on the vertices in this example (The statement if (s.equals("v1") && d.equals("v0")) means that it is the edge from "v1" to "v0").

    import java.awt.Color;
    import java.awt.Paint;
    
    import javax.swing.JFrame;
    
    import org.apache.commons.collections15.Transformer;
    
    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;
    
    public class JUNGEdgeColorsTest 
    {
    
        public static void main(String[] args) 
        {
            JFrame jf = new JFrame();
            final Graph<String, String> g = getGraph();
            VisualizationViewer<String, String> vv = 
                new VisualizationViewer<String, String>(new FRLayout<String, String>(g));
    
            Transformer<String, Paint> colorTransformer = new Transformer<String, Paint>()
            {
                @Override
                public Paint transform(String e)
                {
                    final String s = g.getSource(e);
                    final String d = g.getDest(e);
                    if (s.equals("v1") && d.equals("v0"))
                    {
                        return Color.GREEN;
                    }
                    return Color.BLACK;
                }
            };
            vv.getRenderContext().setArrowFillPaintTransformer(colorTransformer);
            vv.getRenderContext().setArrowDrawPaintTransformer(colorTransformer);
            vv.getRenderContext().setEdgeDrawPaintTransformer(colorTransformer);
            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.addEdge("e0", "v0", "v1");
            g.addEdge("e1", "v1", "v0");
            return g;
        }
    }