annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Grid.java @ 8:ea7885bd9bff tip

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