-3

I use the JUNG java library to manage graphs and I want to know how to color the connected nodes with the same color to distinguish the connected components.

For example, I want the nodes 2, 3 and 4 to have the same color and the node 1to have another color, knowing that the adjacency matrix is:

0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0

code to create graph (nodes have the same color):

 Graph<Integer, String> g;
/** Creates a new instance of SimpleGraphView */
public ReadFile(int nbsommet,int [] [] nodeMat) {
    // Graph<V, E> where V is the type of the vertices and E is the type of the edges
    // Note showing the use of a SparseGraph rather than a SparseMultigraph
    g = new SparseGraph<Integer, String>();
    // Add some vertices. From above we defined these to be type Integer.
    /*for (int i = 1; i <=nbsommet; i++) {
        g.addVertex((Integer)i);

    }*/
       for (int i = 1; i <=nbsommet; i++)
       {  g.addVertex((Integer)i);
           for (int j = 1; j<=nbsommet; j++) 
           {
               if((nodeMat[i][j]==1)&& (j>i))
               {   if(!(g.getVertices().contains(j)))
               { g.addVertex((Integer)j);}
                   g.addEdge(i+" "+j, i, j);
                   }

    }}}
//...
  ReadFile sgv = new ReadFile(nbsommet,nodeMatfinal); // This builds the graph
  //Design sgv1 = new Design(); 
    Layout<Integer, String> layout = new KKLayout (sgv.g);
    //TreeLayout  layout = new TreeLayout(sgv.g,100,100);
    layout.setSize(new Dimension(800,800));  
    BasicVisualizationServer<Integer, String> vv = new   BasicVisualizationServer<Integer, String>(layout);

    Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
        public Paint transform(Integer i) {
            return (Paint) Color.GREEN;
        }
    };  

    vv.setPreferredSize(new Dimension(850,850));
    vv.getRenderContext().setVertexLabelRenderer(new        DefaultVertexLabelRenderer(Color.green));
    vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.white));
    vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));

    vv.getRenderContext().setVertexFillPaintTransformer((Transformer<Integer, java.awt.Paint>) vertexPaint);
    vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.green, Color.yellow));

    vv.setBackground(Color.gray);
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);


    JFrame frame = new JFrame("Graph");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv); 
    frame.pack();
    frame.setVisible(true); 

So how change this code to have the connected components with different colors

  • 1
    @user2699285 Do you have a problem with using JUNG? What have you tried so far? Please include your code. –  Aug 20 '13 at 09:46

1 Answers1

1

The JUNG library does not let you set different renderers per Vertex, so instead you use one renderer and put some of the logic there. (I got that idea from this SO post).

You now have to determine which vertices should get which colors, and put that either in a method call or a Map<Integer, Color>. I will assume the latter:

final Map<Integer,Color> colorMapping = new HashMap<Integer, Color>();

// .....

Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() 
{
    public Paint transform(Integer i)
    {
        return colorMapping.get(i.intValue());
    }
};

Obviously this leaves the question how to fill colorMapping.
You need to determine which are the connected components from the adjacency matrix; this is a separate problem that has been handled in this StackOverflow post.
Once you have the separate subgraphs, all that is left to do is to assign colors to them, and fill colorMapping accordingly.


A few notes on your code sample:

  1. I had to comment this call out:

    vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.green, Color.yellow));

    It sets a new PaintTransformer that replaces the one that calls vertexPaint.

  2. Your graph initialization is 1-based, but the array is 0-based. You should change

    if((nodeMat[i][j]==1)&& (j>i))

    into

    if((nodeMat[i-1][j-1]==1)&& (j>i))

    or you will get an ArrayIndexOutOfBoundsException.

Community
  • 1
  • 1