Search code examples
javagraph-visualizationjgraphtjgraph

Jgrapht edge color


I would like to change the color of my edges using Jgrapht so that I can show the correspondence in my whole graph. Let us assume there is an edge between nodes 1 and 2. I will highlight these nodes in (say) red color. I will find nodes 1 and 2 in the whole graph and show them in the red color and the remaining nodes in some other color. I see the file that creates the visualization is SimpleTouchgraphApplet.java In this file, I am adding statements like,

setBackground(Color.green);
setForeground(Color.cyan);

I added the above two statements in the init section. I also added the below statements in my main function.

Color defaultBackColor = new Color(0x01, 0x11, 0x44);
JFrame frame = new JFrame();
frame.setBackground(defaultBackColor);
frame.setForeground(Color.CYAN);

However, I do not see any change in the foreground or background color. Please let me know where am going wrong.


Solution

  • I found that the edge coloring can be added in the TouchGraphConverter.java file.

    The following function constructs the nodes.

    public Node convertToTouchGraph(
        Graph<V, E> graph,
        TGPanel tgPanel,
        boolean selfReferencesAllowed)
        throws TGException
    {
        List<V> jgtNodes = new ArrayList<V>(graph.vertexSet());
        Node [] tgNodes = new Node[jgtNodes.size()];
    
        // add all the nodes...
        for (int i = 0; i < jgtNodes.size(); i++) {
            Node n;
            if (jgtNodes.get(i) instanceof Node) {
                // if our JGraphT object was a touchGraph node, add it unaltered
                n = (Node) jgtNodes.get(i);
            } else {
                // create a TG Node with a "label" and "id" equals to the
                // objects toString() value
                n = new Node(jgtNodes.get(i).toString());
    
            }
    
            // store this for edge-related creation below
            tgNodes[i] = n;
            tgPanel.addNode(n);
            tgNodes[i].setBackColor(Color.RED);
    

    The last line, tgNodes[i].setBackColor(Color.RED) will make the node color to red in the generated graph.