1package com.mxgraph.examples.swing; 2 3import javax.swing.BorderFactory; 4import javax.swing.CellRendererPane; 5import javax.swing.JFrame; 6import javax.swing.JLabel; 7import javax.swing.border.BevelBorder; 8 9import com.mxgraph.canvas.mxICanvas; 10import com.mxgraph.canvas.mxImageCanvas; 11import com.mxgraph.swing.mxGraphComponent; 12import com.mxgraph.swing.handler.mxRubberband; 13import com.mxgraph.swing.view.mxInteractiveCanvas; 14import com.mxgraph.view.mxCellState; 15import com.mxgraph.view.mxGraph; 16 17public class CustomCanvas extends JFrame 18{ 19 20 /** 21 * 22 */ 23 private static final long serialVersionUID = -844106998814982739L; 24 25 public CustomCanvas() 26 { 27 super("Custom Canvas"); 28 29 // Demonstrates the use of a Swing component for rendering vertices. 30 // Note: Use the heavyweight feature to allow for event handling in 31 // the Swing component that is used for rendering the vertex. 32 33 mxGraph graph = new mxGraph() 34 { 35 public void drawStateWithLabel(mxICanvas canvas, mxCellState state, 36 String label) 37 { 38 // Indirection for wrapped swing canvas inside image canvas (used for creating 39 // the preview image when cells are dragged) 40 if (getModel().isVertex(state.getCell()) 41 && canvas instanceof mxImageCanvas 42 && ((mxImageCanvas) canvas).getGraphicsCanvas() instanceof SwingCanvas) 43 { 44 ((SwingCanvas) ((mxImageCanvas) canvas).getGraphicsCanvas()) 45 .drawVertex(state, label); 46 } 47 // Redirection of drawing vertices in SwingCanvas 48 else if (getModel().isVertex(state.getCell()) 49 && canvas instanceof SwingCanvas) 50 { 51 ((SwingCanvas) canvas).drawVertex(state, label); 52 } 53 else 54 { 55 super.drawStateWithLabel(canvas, state, label); 56 } 57 } 58 }; 59 60 Object parent = graph.getDefaultParent(); 61 62 graph.getModel().beginUpdate(); 63 try 64 { 65 66 Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 67 30); 68 Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 69 80, 30); 70 graph.insertEdge(parent, null, "Edge", v1, v2); 71 } 72 finally 73 { 74 graph.getModel().endUpdate(); 75 } 76 77 mxGraphComponent graphComponent = new mxGraphComponent(graph) 78 { 79 /** 80 * 81 */ 82 private static final long serialVersionUID = 4683716829748931448L; 83 84 public mxInteractiveCanvas createCanvas() 85 { 86 return new SwingCanvas(this); 87 } 88 }; 89 90 getContentPane().add(graphComponent); 91 92 // Adds rubberband selection 93 new mxRubberband(graphComponent); 94 } 95 96 public class SwingCanvas extends mxInteractiveCanvas 97 { 98 protected CellRendererPane rendererPane = new CellRendererPane(); 99 100 protected JLabel vertexRenderer = new JLabel(); 101 102 protected mxGraphComponent graphComponent; 103 104 public SwingCanvas(mxGraphComponent graphComponent) 105 { 106 this.graphComponent = graphComponent; 107 108 vertexRenderer.setBorder(BorderFactory 109 .createBevelBorder(BevelBorder.RAISED)); 110 vertexRenderer.setHorizontalAlignment(JLabel.CENTER); 111 vertexRenderer.setBackground(graphComponent.getBackground() 112 .darker()); 113 vertexRenderer.setOpaque(true); 114 } 115 116 public void drawVertex(mxCellState state, String label) 117 { 118 vertexRenderer.setText(label); 119 // TODO: Configure other properties... 120 121 rendererPane.paintComponent(g, vertexRenderer, graphComponent, 122 (int) state.getX() + translate.x, (int) state.getY() 123 + translate.y, (int) state.getWidth(), (int) state 124 .getHeight(), true); 125 } 126 127 } 128 129 public static void main(String[] args) 130 { 131 CustomCanvas frame = new CustomCanvas(); 132 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 133 frame.setSize(400, 320); 134 frame.setVisible(true); 135 } 136 137} 138