1 /* 2 * @(#)HelloWorld.java 3.3 23-APR-04 3 * 4 * Copyright (c) 2001-2004, Gaudenz Alder All rights reserved. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 package org.jgraph.example; 22 23 import java.awt.Color; 24 import java.awt.geom.Rectangle2D; 25 26 import javax.swing.BorderFactory; 27 import javax.swing.JFrame; 28 import javax.swing.JScrollPane; 29 30 import org.jgraph.JGraph; 31 import org.jgraph.graph.DefaultEdge; 32 import org.jgraph.graph.DefaultGraphCell; 33 import org.jgraph.graph.DefaultGraphModel; 34 import org.jgraph.graph.GraphConstants; 35 import org.jgraph.graph.GraphModel; 36 37 public class HelloWorld { 38 main(String[] args)39 public static void main(String[] args) { 40 // Switch off D3D because of Sun XOR painting bug 41 // See http://www.jgraph.com/forum/viewtopic.php?t=4066 42 System.setProperty("sun.java2d.d3d", "false"); 43 44 // Construct Model and Graph 45 GraphModel model = new DefaultGraphModel(); 46 JGraph graph = new JGraph(model); 47 48 // Control-drag should clone selection 49 graph.setCloneable(true); 50 51 // Enable edit without final RETURN keystroke 52 graph.setInvokesStopCellEditing(true); 53 54 // When over a cell, jump to its default port (we only have one, anyway) 55 graph.setJumpToDefaultPort(true); 56 57 // Insert all three cells in one call, so we need an array to store them 58 DefaultGraphCell[] cells = new DefaultGraphCell[3]; 59 60 // Create Hello Vertex 61 cells[0] = createVertex("Hello", 20, 20, 40, 20, null, false); 62 63 // Create World Vertex 64 cells[1] = createVertex("World", 140, 140, 40, 20, Color.ORANGE, true); 65 66 // Create Edge 67 DefaultEdge edge = new DefaultEdge(); 68 // Fetch the ports from the new vertices, and connect them with the edge 69 edge.setSource(cells[0].getChildAt(0)); 70 edge.setTarget(cells[1].getChildAt(0)); 71 cells[2] = edge; 72 73 // Set Arrow Style for edge 74 int arrow = GraphConstants.ARROW_CLASSIC; 75 GraphConstants.setLineEnd(edge.getAttributes(), arrow); 76 GraphConstants.setEndFill(edge.getAttributes(), true); 77 78 // Insert the cells via the cache, so they get selected 79 graph.getGraphLayoutCache().insert(cells); 80 81 // Show in Frame 82 JFrame frame = new JFrame(); 83 frame.getContentPane().add(new JScrollPane(graph)); 84 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 85 frame.pack(); 86 frame.setVisible(true); 87 } 88 createVertex(String name, double x, double y, double w, double h, Color bg, boolean raised)89 public static DefaultGraphCell createVertex(String name, double x, 90 double y, double w, double h, Color bg, boolean raised) { 91 92 // Create vertex with the given name 93 DefaultGraphCell cell = new DefaultGraphCell(name); 94 95 // Set bounds 96 GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double( 97 x, y, w, h)); 98 99 // Set fill color 100 if (bg != null) { 101 GraphConstants.setGradientColor(cell.getAttributes(), bg); 102 GraphConstants.setOpaque(cell.getAttributes(), true); 103 } 104 105 // Set raised border 106 if (raised) 107 GraphConstants.setBorder(cell.getAttributes(), BorderFactory 108 .createRaisedBevelBorder()); 109 else 110 // Set black border 111 GraphConstants.setBorderColor(cell.getAttributes(), Color.black); 112 113 // Add a Floating Port 114 cell.addPort(); 115 116 return cell; 117 } 118 119 }