title: How to change color of an element in Java Swing?
body: <p>I have a program to visualize a weighted adjacency matrix into a graph. I wish to change the color of 2 nodes alone. </p>
  
  <p><a href="https://i.stack.imgur.com/b4cr1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b4cr1.jpg" alt="enter image description here"></a></p>
  
  <p>In this graph, I wish to change the color of node 1 and 2.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package graphvisualisation;
  
  import java.awt.Dimension;
  import java.util.Hashtable;
  import java.util.LinkedList;
  
  import javax.swing.JFrame;
  
  import org.apache.commons.collections15.Transformer;
  
  import edu.uci.ics.jung.algorithms.layout.CircleLayout;
  import edu.uci.ics.jung.graph.Graph;
  import edu.uci.ics.jung.graph.UndirectedSparseGraph;
  import edu.uci.ics.jung.graph.util.EdgeType;
  import edu.uci.ics.jung.visualization.BasicVisualizationServer;
  import java.awt.Font;
  
  
  public class GraphVisualisation
  {
  static int edgeCount_Directed = 0;   // This works with the inner MyEdge class
  class MyNode 
  {
    //static int edgeCount = 0;   // This works with the inner MyEdge class
    String id;
    public MyNode(String id) 
    {
    this.id = id;
    }
    public String toString() 
    {    
    return "V"+id;  
    } 
    public String Node_Property()
    {
     String node_prop = id;
     return(node_prop);
    }
  }
  
  class MyLink 
  {
  
      int weight;
     String Label;
     int id;
  
  
     public MyLink(int weight)
     {
      this.id = edgeCount_Directed++;
      this.weight = weight;
     } 
  
     public String toString() 
     {
      return "E"+id;
     }
     public String Link_Property()
     {
      String Link_prop = Label;
      return(Link_prop);
     }
     public String Link_Property_wt()
     {
      String Link_prop_wt = ""+weight;
      return(Link_prop_wt);
     }
  }
  //used to construct graph and call graph algorithm used in JUNG
  
  public void Visualize_Directed_Graph(LinkedList&lt;String&gt; Distinct_nodes, LinkedList&lt;String&gt; source_vertex, LinkedList&lt;String&gt; target_vertex, LinkedList&lt;Integer&gt; Edge_Weight)
  {
  //CREATING weighted directed graph
          Graph&lt;MyNode, MyLink&gt; g = new UndirectedSparseGraph&lt;GraphVisualisation.MyNode, GraphVisualisation.MyLink&gt;();
          //create node objects
          Hashtable&lt;String, MyNode&gt; Graph_Nodes = new Hashtable&lt;String, GraphVisualisation.MyNode&gt;();
          LinkedList&lt;MyNode&gt; Source_Node = new LinkedList&lt;GraphVisualisation.MyNode&gt;();
          LinkedList&lt;MyNode&gt; Target_Node = new LinkedList&lt;GraphVisualisation.MyNode&gt;();
          LinkedList&lt;MyNode&gt; Graph_Nodes_Only = new LinkedList&lt;GraphVisualisation.MyNode&gt;();
          //LinkedList&lt;MyLink&gt; Graph_Links = new LinkedList&lt;Graph_Algos.MyLink&gt;();
          //create graph nodes
          for(int i=0;i&lt;Distinct_nodes.size();i++)
          {
          String node_name = Distinct_nodes.get(i);
          MyNode data = new MyNode(node_name);
          Graph_Nodes.put(node_name, data);
          Graph_Nodes_Only.add(data);
          }
          //Now convert all source and target nodes into objects
          for(int t=0;t&lt;source_vertex.size();t++)
          {
          Source_Node.add(Graph_Nodes.get(source_vertex.get(t)));
          Target_Node.add(Graph_Nodes.get(target_vertex.get(t)));
          }
          //Now add nodes and edges to the graph
          for(int i=0;i&lt;Edge_Weight.size();i++)
          {
          g.addEdge(new MyLink(Edge_Weight.get(i)),Source_Node.get(i), Target_Node.get(i), EdgeType.UNDIRECTED);
          }
  
          //-------------
  
            CircleLayout&lt;MyNode, MyLink&gt; layout1 = new CircleLayout&lt;MyNode,MyLink&gt;(g);
  
             layout1.setSize(new Dimension(300, 300));
             BasicVisualizationServer&lt;MyNode, MyLink&gt; viz = new BasicVisualizationServer&lt;MyNode,MyLink&gt;(layout1);
  
             viz.setPreferredSize(new Dimension(350, 350));
  
             Transformer&lt;MyNode, String&gt; vertexLabelTransformer = new Transformer&lt;MyNode,String&gt;() {
               public String transform(MyNode vertex) {
                 return (String) vertex.Node_Property();
               }
             };
       //Sets the vertex label font
       Transformer&lt;MyNode,Font&gt; vertexFont = new Transformer&lt;MyNode,Font&gt;() {
           public Font transform(MyNode node) {
               Font font = new Font("Sans Serif", Font.BOLD, 30 );
               return font;
           }
       };
  
             Transformer&lt;MyLink, String&gt; edgeLabelTransformer = new Transformer&lt;MyLink, String&gt;() {
               public String transform(MyLink edge) {
                 return  edge.Link_Property_wt();
               }
             };
  
             Transformer&lt;MyLink,Font&gt; edgeFont = new Transformer&lt;MyLink,Font&gt;() {
           public Font transform(MyLink node) {
               Font font = new Font("Sans Serif", Font.PLAIN, 25);
               return font;
           }
             };
             viz.getRenderContext().setEdgeLabelTransformer(edgeLabelTransformer);
             viz.getRenderContext().setVertexLabelTransformer(vertexLabelTransformer);
             viz.getRenderContext().setVertexFontTransformer(vertexFont);
             viz.getRenderContext().setEdgeFontTransformer(edgeFont);
  
             JFrame frame = new JFrame("Graph Visualisation");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(viz);
             frame.pack();
             frame.setVisible(true);
  
  }
  
       public static void main(String[] args)
       {
  
  
      GraphVisualisation GA1 = new GraphVisualisation();
  
       LinkedList&lt;String&gt; Distinct_Vertex = new LinkedList&lt;String&gt;();//used to enter vertexes
       LinkedList&lt;String&gt; Source_Vertex = new LinkedList&lt;String&gt;();
       LinkedList&lt;String&gt; Target_Vertex = new LinkedList&lt;String&gt;();
       //LinkedList&lt;Double&gt; Edge_Weight = new LinkedList&lt;Double&gt;();//used to enter edge weight
       LinkedList&lt;Integer&gt; Edge_Weight = new LinkedList&lt;Integer&gt;();//used to enter edge weight
  
       int[][] adj_matrix={{0,4,0,0,10},{4,0,6,2,0},{0,6,0,4,0},{0,2,4,0,0},{10,0,0,0,0}};
  
       int i,j;
       //add disinct vertex
       for(i=0;i&lt;adj_matrix.length;i++)
       {
           Distinct_Vertex.add(Integer.toString(i+1));
       }
  
  
       //add edges
       for(i=0;i&lt;adj_matrix.length;i++)
       {
           for(j=0;j&lt;adj_matrix.length;j++)
           {
               if(adj_matrix[i][j]&gt;0)
               {
               Source_Vertex.add(Integer.toString(i+1)); Target_Vertex.add(Integer.toString(j+1)); Edge_Weight.add((int)adj_matrix[i][j]);
               }
           }
       }
  
       GA1.Visualize_Directed_Graph(Distinct_Vertex, Source_Vertex, Target_Vertex, Edge_Weight);
  
       }
  }
  </code></pre>
  
  <p>The program is made using JUNG library. The adjacency matrix is the input and the graph is obtained as the output in a JFrame. </p>