1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.ParentMap;
public class MySampleCode extends JFrame {
private GraphModel model;
private JGraph graph;
private DefaultGraphCell[] cells = new DefaultGraphCell[2];
public static void main(String[] args) {
MySampleCode testPC = new MySampleCode();
testPC.createGraph();
}
public void createGraph() {
model = new DefaultGraphModel();
GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory(), true);
graph = new JGraph(model, view);
cells[0] = createVertex("0", 100, 20, 400, 100, Color.GREEN);
cells[1] = createVertex("1", 110, 25, 50, 20, Color.BLUE);
ParentMap pm = new ParentMap();
pm.addEntry(cells[0], null);
pm.addEntry(cells[1], cells[0]);
graph.getGraphLayoutCache().insert(cells, null, null, pm);
getContentPane().add(graph);
setTitle("Static Graph");
setSize(800, 600);
setVisible(true);
int nodeCount = 1;
for (int i = 0; i < cells.length; i++) {
Object cellObject = cells[i];
if (cellObject instanceof DefaultGraphCell && !(cellObject instanceof DefaultEdge)
&& !(cellObject instanceof DefaultPort)) {
System.out.println("Node " + nodeCount + ": " + (graph.getModel().getChildCount(cellObject) - 1));
nodeCount++;
}
}
}
public DefaultGraphCell createVertex(String name, double x, double y, double w, double h, Color bg) {
DefaultGraphCell cell = new DefaultGraphCell(name);
GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x, y, w, h));
if (bg != null) {
GraphConstants.setGradientColor(cell.getAttributes(), bg);
GraphConstants.setOpaque(cell.getAttributes(), true);
}
GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
cell.addPort();
return cell;
}
public DefaultEdge createEdge(Object source, Object target, int arrowStyle, String label) {
DefaultEdge edge = new DefaultEdge(label);
edge.setSource(source);
edge.setTarget(target);
GraphConstants.setLineEnd(edge.getAttributes(), arrowStyle);
GraphConstants.setEndFill(edge.getAttributes(), true);
return edge;
}
} |
Partager