使用JGraphXAdapter更改边缘的颜色

时间:2015-08-09 06:22:27

标签: java jgrapht jgraphx

我正在使用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]);
    }
  }

}

1 个答案:

答案 0 :(得分:2)

我找到了问题的答案。像这样:

// set up a mapping between JGraphT edges and JGraphX cells
HashMap<DefaultEdge,com.mxgraph.model.mxICell> edgeToCellMap
  = jgxAdapter.getEdgeToCellMap();
// we can also set up a mapping between JGraphT vertices and JGraphX cells
HashMap<Node,com.mxgraph.model.mxICell> vertexToCellMap
  = jgxAdapter.getVertexToCellMap();

...

Object[] edgeCellArray = new Object[ARRAY_SIZE-1];

for (int i = 1; i < ARRAY_SIZE; ++i) {
  g.removeEdge(n[0],n[i]);
  edgeCellArray[i-1] = (Object)(edgeToCellMap.get(g.getEdge(n[i], n[0])));
}

// the following two lines are for learning the format of style strings
Map<String,Object> edgeCellStyle = jgxAdapter.getCellStyle(edgeCellArray[0]);
System.out.println(edgeCellStyle);

jgxAdapter.setCellStyle("strokeColor=#CCCC00", edgeCellArray);

// we can do something similar for vertices    
Object[] vertexCellArray = new Object[ARRAY_SIZE/2];

for (int i = 0; i < ARRAY_SIZE/2; ++i) {
  vertexCellArray[i] = (Object)(vertexToCellMap.get(n[i]));
}

// the following two lines are for learning the format of style strings    
Map<String,Object> vertexCellStyle = jgxAdapter.getCellStyle(vertexCellArray[0]);
System.out.println(vertexCellStyle);

jgxAdapter.setCellStyle("fillColor=#00CC00", vertexCellArray);

库中某处可能有一组字符串常量,包括“strokeColor”和“fillColor”,但我还没有找到它们。