我正在使用JGraphT库,特别是 JGraphXAdapter 来使用 JGraphX 显示图形。我想改变一些边缘的颜色(不是全部)。我的问题是,如何实现?(我知道如何更改所有边缘的默认样式,但不知道如何更改单个边缘。)请参阅下面的代码。
我正在使用 JGraphT 0.9.1 包中的 jgrapht-ext-0.9.1-uber.jar。
package org.jgrapht.demo;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JFrame;
import org.jgrapht.ListenableGraph;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.ListenableDirectedGraph;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
public class FiveSort extends JFrame {
private static final long serialVersionUID = 1L;
private JGraphXAdapter<Node, DefaultEdge> jgxAdapter;
private static final int ARRAY_SIZE = 25;
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Node {
String label;
int value;
Node(String label, int value) {
this.label = label;
this.value = value;
}
@Override
public String toString() {
return label + ": " + value;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new FiveSort();
}
});
}
public FiveSort() {
initGUI();
setLocationRelativeTo(null);
setTitle("Five Sort");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void initGUI() {
ListenableGraph<Node, DefaultEdge> g =
new ListenableDirectedGraph<Node, DefaultEdge>(DefaultEdge.class);
jgxAdapter = new JGraphXAdapter<Node, DefaultEdge>(g);
jgxAdapter.getStylesheet().getDefaultEdgeStyle().put(mxConstants.STYLE_NOLABEL, "1");
add(new mxGraphComponent(jgxAdapter));
ArrayList<Integer> shuffle = new ArrayList<Integer>();
for (int i = 0; i < ARRAY_SIZE; ++i) {
shuffle.add(i);
}
Collections.shuffle(shuffle);
Node[] n = new Node[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; ++i) {
n[i] = new Node(ALPHABET.substring(i, i+1), shuffle.get(i));
g.addVertex(n[i]);
for (int j = 0; j < i; ++j) {
g.addEdge(n[i], n[j]);
g.addEdge(n[j], n[i]);
}
}
mxCircleLayout layout = new mxCircleLayout(jgxAdapter);
layout.execute(jgxAdapter.getDefaultParent());
for (int i = 1; i < ARRAY_SIZE; ++i) {
g.removeEdge(n[0],n[i]);
// instead of removing the edge below I would like to change its color
g.removeEdge(n[i],n[0]);
}
}
}