annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Grid.java @ 1:e3935c01cde2 tip

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