KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > graph > shapes > Diamond


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.graph.shapes;
20
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Map JavaDoc;
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 JavaDoc g, Point JavaDoc start, Point JavaDoc 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 JavaDoc start, Point JavaDoc end)
52     {
53         DiamondCell cell = new DiamondCell("");
54         AttributeMap attrs = new AttributeMap();
55         attrs.put("timestamp", new Date JavaDoc());
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 JavaDoc[] cells = new Object JavaDoc[] { cell };
70
71         Map JavaDoc attributes = new Hashtable JavaDoc();
72         attributes.put(cell, attrs);
73         graph.getModel().insert(cells, attributes, null, null, null);
74     }
75     
76     private Rectangle getBounds(Point JavaDoc start, Point JavaDoc 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