Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/gui/Grid.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9418ab7b7f3f |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) | |
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
6 | |
7 This program is free software: you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation, either version 3 of the License, or | |
10 (at your option) any later version. | |
11 | |
12 This program is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 package uk.ac.qmul.eecs.ccmi.gui; | |
22 | |
23 import java.awt.Color; | |
24 import java.awt.Graphics2D; | |
25 import java.awt.Stroke; | |
26 import java.awt.geom.Line2D; | |
27 import java.awt.geom.Point2D; | |
28 import java.awt.geom.Rectangle2D; | |
29 import java.awt.geom.RectangularShape; | |
30 | |
31 /** | |
32 A grid to which nodes can be "snapped". The | |
33 snapping operation moves a point to the nearest grid point. | |
34 */ | |
35 public class Grid | |
36 { | |
37 /** | |
38 Constructs a grid with no grid points. | |
39 */ | |
40 public Grid() | |
41 { | |
42 setGrid(0, 0); | |
43 } | |
44 | |
45 /** | |
46 Sets the grid point distances in x- and y-direction | |
47 @param x the grid point distance in x-direction | |
48 @param y the grid point distance in y-direction | |
49 */ | |
50 public void setGrid(double x, double y) | |
51 { | |
52 gridx = x; | |
53 gridy = y; | |
54 } | |
55 | |
56 /** | |
57 Draws this grid inside a rectangle. | |
58 @param g2 the graphics context | |
59 @param bounds the bounding rectangle | |
60 */ | |
61 public void draw(Graphics2D g2, Rectangle2D bounds) | |
62 { | |
63 Color PALE_BLUE = new Color(0.9F, 0.8F, 0.9F); | |
64 Color oldColor = g2.getColor(); | |
65 g2.setColor(PALE_BLUE); | |
66 Stroke oldStroke = g2.getStroke(); | |
67 for (double x = bounds.getX(); x < bounds.getMaxX(); x += gridx) | |
68 g2.draw(new Line2D.Double(x, bounds.getY(), x, bounds.getMaxY())); | |
69 for (double y = bounds.getY(); y < bounds.getMaxY(); y += gridy) | |
70 g2.draw(new Line2D.Double(bounds.getX(), y, bounds.getMaxX(), y)); | |
71 g2.setStroke(oldStroke); | |
72 g2.setColor(oldColor); | |
73 } | |
74 | |
75 /** | |
76 Snaps a point to the nearest grid point | |
77 @param p the point to snap. After the call, the | |
78 coordinates of p are changed so that p falls on the grid. | |
79 */ | |
80 public void snap(Point2D p) | |
81 { | |
82 double x; | |
83 if (gridx == 0) | |
84 x = p.getX(); | |
85 else | |
86 x = Math.round(p.getX() / gridx) * gridx; | |
87 double y; | |
88 if (gridy == 0) | |
89 y = p.getY(); | |
90 else | |
91 y = Math.round(p.getY() / gridy) * gridy; | |
92 | |
93 p.setLocation(x, y); | |
94 } | |
95 | |
96 /** | |
97 Snaps a rectangle to the nearest grid points | |
98 @param r the rectangle to snap. After the call, the | |
99 coordinates of r are changed so that all of its corners | |
100 falls on the grid. | |
101 */ | |
102 public void snap(RectangularShape r) | |
103 { | |
104 double x; | |
105 double w; | |
106 w = r.getWidth(); | |
107 if (gridx == 0) | |
108 { | |
109 x = r.getX(); | |
110 } | |
111 else | |
112 { | |
113 x = Math.round(r.getX() / gridx) * gridx; | |
114 // w = Math.ceil(r.getWidth() / (2 * gridx)) * (2 * gridx); | |
115 } | |
116 double y; | |
117 double h; | |
118 h = r.getHeight(); | |
119 if (gridy == 0) | |
120 { | |
121 y = r.getY(); | |
122 } | |
123 else | |
124 { | |
125 y = Math.round(r.getY() / gridy) * gridy; | |
126 // h = Math.ceil(r.getHeight() / (2 * gridy)) * (2 * gridy); | |
127 } | |
128 | |
129 r.setFrame(x, y, w, h); | |
130 } | |
131 | |
132 private double gridx; | |
133 private double gridy; | |
134 } |