我有一个程序可以将加权邻接矩阵可视化为图形。我希望仅更改2个节点的颜色。
在此图中,我希望更改节点1和2的颜色。
代码如下:
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<String> Distinct_nodes, LinkedList<String> source_vertex, LinkedList<String> target_vertex, LinkedList<Integer> Edge_Weight)
{
//CREATING weighted directed graph
Graph<MyNode, MyLink> g = new UndirectedSparseGraph<GraphVisualisation.MyNode, GraphVisualisation.MyLink>();
//create node objects
Hashtable<String, MyNode> Graph_Nodes = new Hashtable<String, GraphVisualisation.MyNode>();
LinkedList<MyNode> Source_Node = new LinkedList<GraphVisualisation.MyNode>();
LinkedList<MyNode> Target_Node = new LinkedList<GraphVisualisation.MyNode>();
LinkedList<MyNode> Graph_Nodes_Only = new LinkedList<GraphVisualisation.MyNode>();
//LinkedList<MyLink> Graph_Links = new LinkedList<Graph_Algos.MyLink>();
//create graph nodes
for(int i=0;i<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<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<Edge_Weight.size();i++)
{
g.addEdge(new MyLink(Edge_Weight.get(i)),Source_Node.get(i), Target_Node.get(i), EdgeType.UNDIRECTED);
}
//-------------
CircleLayout<MyNode, MyLink> layout1 = new CircleLayout<MyNode,MyLink>(g);
layout1.setSize(new Dimension(300, 300));
BasicVisualizationServer<MyNode, MyLink> viz = new BasicVisualizationServer<MyNode,MyLink>(layout1);
viz.setPreferredSize(new Dimension(350, 350));
Transformer<MyNode, String> vertexLabelTransformer = new Transformer<MyNode,String>() {
public String transform(MyNode vertex) {
return (String) vertex.Node_Property();
}
};
//Sets the vertex label font
Transformer<MyNode,Font> vertexFont = new Transformer<MyNode,Font>() {
public Font transform(MyNode node) {
Font font = new Font("Sans Serif", Font.BOLD, 30 );
return font;
}
};
Transformer<MyLink, String> edgeLabelTransformer = new Transformer<MyLink, String>() {
public String transform(MyLink edge) {
return edge.Link_Property_wt();
}
};
Transformer<MyLink,Font> edgeFont = new Transformer<MyLink,Font>() {
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<String> Distinct_Vertex = new LinkedList<String>();//used to enter vertexes
LinkedList<String> Source_Vertex = new LinkedList<String>();
LinkedList<String> Target_Vertex = new LinkedList<String>();
//LinkedList<Double> Edge_Weight = new LinkedList<Double>();//used to enter edge weight
LinkedList<Integer> Edge_Weight = new LinkedList<Integer>();//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<adj_matrix.length;i++)
{
Distinct_Vertex.add(Integer.toString(i+1));
}
//add edges
for(i=0;i<adj_matrix.length;i++)
{
for(j=0;j<adj_matrix.length;j++)
{
if(adj_matrix[i][j]>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);
}
}
该程序是使用JUNG库制作的。邻接矩阵是输入,而图则作为JFrame中的输出获得。
答案 0 :(得分:0)
您应该可以执行以下操作:
Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
public Paint transform(Integer i) {
if (i == wantedVertex) {
return Color.WHITE; // Example of color from java.awt.Color
}
else {
return Color.BLACK;
}
}
};
viz.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
The sample program in this article演示了另一种改变顶点颜色(以及改变形状)的方法。