1 19 package org.lucane.applications.whiteboard.graph.shapes; 20 21 import java.awt.Graphics ; 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 import java.util.Date ; 25 import java.util.Hashtable ; 26 import java.util.Map ; 27 28 import org.jgraph.graph.AttributeMap; 29 import org.jgraph.graph.DefaultPort; 30 import org.jgraph.graph.GraphConstants; 31 import org.lucane.applications.whiteboard.graph.MyGraph; 32 import org.lucane.applications.whiteboard.graph.cells.DiamondCell; 33 34 class Diamond implements Shape 35 { 36 public void paint(Graphics g, Point start, Point end) 37 { 38 int size = Math.abs(start.x - end.x); 39 40 int minX = Math.min(start.x, end.x); 41 int minY = Math.min(start.y, end.y); 42 int middleX = minX + size/2; 43 int middleY = minY + size/2; 44 45 int[] x = new int[] {minX, middleX, minX+size, middleX}; 46 int[] y = new int[] {middleY, minY, middleY, minY+size}; 47 48 g.drawPolygon(x, y, x.length); 49 } 50 51 public void addToGraph(MyGraph graph, Point start, Point end) 52 { 53 DiamondCell cell = new DiamondCell(""); 54 AttributeMap attrs = new AttributeMap(); 55 attrs.put("timestamp", new Date ()); 56 57 GraphConstants.setBounds(attrs, getBounds(start, end)); 58 GraphConstants.setLineWidth(attrs, graph.getLineWidth()); 59 GraphConstants.setBorderColor(attrs, graph.getCellBorder()); 60 if(graph.getCellBackground()!=null) 61 { 62 GraphConstants.setBackground(attrs, graph.getCellBackground()); 63 GraphConstants.setOpaque(attrs, true); 64 } 65 66 DefaultPort hp = new DefaultPort(); 67 cell.add(hp); 68 69 Object [] cells = new Object [] { cell }; 70 71 Map attributes = new Hashtable (); 72 attributes.put(cell, attrs); 73 graph.getModel().insert(cells, attributes, null, null, null); 74 } 75 76 private Rectangle getBounds(Point start, Point end) 77 { 78 int size = Math.abs(start.x - end.x); 79 80 int minX = Math.min(start.x, end.x); 81 int minY = Math.min(start.y, end.y); 82 83 return new Rectangle(minX, minY, size, size); 84 } 85 } | Popular Tags |