最近在学着使用开源的JGraph,为了能添加一个自己的cell,看了一堆帖子。这些帖子有一个特点,千篇一律,你转我的,我转你的,感觉太没意思了吧,始终只能画出锥形。后来还是直接搜索英文的了,搜到了The JGraph Tutorial。其中给了一个很好的例子,我略作了一些修改,功能是添加了一个自己的EllipseCell,画出了椭圆或者圆,这个取决于代码中的GraphConstants.setBounds(cells[1].getAttributes(),
new Rectangle2D.Double(140,140,40,20))。(代码中没有进行注释,想进一步了解的可以下载英
文手册进行学习,这里给出一个我上传的网址 http://download.csdn.net/source/2742630,同时记得下载jar包,否则无法运行):
JGtest.java文件内容如下:
import java.awt.Color;
import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JScrollPane; import org.jgraph.JGraph; import org.jgraph.graph.CellViewRenderer; 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.EdgeView; import org.jgraph.graph.GraphConstants; import org.jgraph.graph.GraphLayoutCache; import org.jgraph.graph.GraphModel; import org.jgraph.graph.VertexRenderer; import org.jgraph.graph.VertexView; public class JGtest { public static void main(String[] args) { GraphModel model = new DefaultGraphModel(); GraphLayoutCache view = new GraphLayoutCache(model, new MyView()); JGraph graph = new JGraph(model, view); DefaultGraphCell[] cells = new DefaultGraphCell[3]; cells[0] = new EllipseCell(new String("Hello New")); //GraphConstants.setAutoSize(cells[0].getAttributes(), true); GraphConstants.setBounds(cells[0].getAttributes(), new Rectangle2D.Double(140,20,50,50)); GraphConstants.setGradientColor(cells[0].getAttributes(),Color.black); GraphConstants.setOpaque(cells[0].getAttributes(), true); DefaultPort port0 = new DefaultPort(); cells[0].add(port0); cells[1] = new DefaultGraphCell(new String("World")); GraphConstants.setBounds(cells[1].getAttributes(), new Rectangle2D.Double(140,140,40,20)); GraphConstants.setGradientColor(cells[1].getAttributes(),Color.red); GraphConstants.setOpaque(cells[1].getAttributes(), true); DefaultPort port1 = new DefaultPort(); cells[1].add(port1); DefaultEdge edge = new DefaultEdge(); edge.setSource(cells[0].getChildAt(0)); edge.setTarget(cells[1].getChildAt(0)); cells[2] = edge; int arrow = GraphConstants.ARROW_CLASSIC; GraphConstants.setLineEnd(edge.getAttributes(), arrow); GraphConstants.setEndFill(edge.getAttributes(), true); graph.getGraphLayoutCache().insert(cells); JFrame frame = new JFrame(); frame.setSize(1000, 500); frame.setTitle("Activity Diagram"); frame.getContentPane().add(new JScrollPane(graph)); frame.setBounds(50, 50, 700, 800); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } }
EllipseCell.java文件内容:
import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Point2D; import org.jgraph.graph.CellViewRenderer; import org.jgraph.graph.DefaultCellViewFactory; import org.jgraph.graph.DefaultGraphCell; import org.jgraph.graph.EdgeView; import org.jgraph.graph.VertexRenderer; import org.jgraph.graph.VertexView; //Define EllipseCell public class EllipseCell extends DefaultGraphCell { // Empty Constructor public EllipseCell() { this(null); } // Construct Cell for Userobject public EllipseCell(Object userObject) { super(userObject); } } //Define the View for an EllipseCell class EllipseView extends VertexView { static EllipseRenderer renderer = new EllipseRenderer(); // Constructor for Superclass public EllipseView(Object cell) { super(cell); } // Returns Perimeter Point for Ellipses public Point2D getPerimeterPoint(EdgeView edge, Point2D source, Point2D p) { if (getRenderer() instanceof EllipseRenderer) return ((EllipseRenderer) getRenderer()).getPerimeterPoint(this, source, p); return super.getPerimeterPoint(edge, source, p); } // Returns the Renderer for this View public CellViewRenderer getRenderer() { return renderer; } // Define the Renderer for an EllipseView static class EllipseRenderer extends VertexRenderer { public void paint(Graphics g) { try { if (gradientColor != null && !preview && isOpaque()) { setOpaque(false); Graphics2D g2d = (Graphics2D) g; g2d.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), getHeight(), gradientColor, true)); g2d.drawOval(0, 0, getWidth(), getHeight()); g2d.fillOval(0, 0, getWidth(), getHeight()); //System.out.println(getWidth()+" "+getHeight()); //g2d.fillRect(0, 0, getWidth(), getHeight()); } super.paint(g); paintSelectionBorder(g); } catch (IllegalArgumentException e) { // JDK Bug: Zero length string passed to TextLayout constructor } } } } class MyView extends DefaultCellViewFactory { // Overrides JGraph.createVertexView protected VertexView createVertexView(Object v) { // Return an EllipseView for EllipseCells if (v instanceof EllipseCell) return new EllipseView(v); else if(v instanceof DiamondCell) return new DiamondView(v); // Else Call Superclass return super.createVertexView(v); } }
由于本人水平有限,目前对这个jar包还在研究中,功能很强大,呵呵。