Mercurial > hg > ccmieditor
view 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 |
line wrap: on
line source
/* CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package uk.ac.qmul.eecs.ccmi.gui; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RectangularShape; /** A grid to which nodes can be "snapped". The snapping operation moves a point to the nearest grid point. */ public class Grid { /** Constructs a grid with no grid points. */ public Grid() { setGrid(0, 0); } /** Sets the grid point distances in x- and y-direction @param x the grid point distance in x-direction @param y the grid point distance in y-direction */ public void setGrid(double x, double y) { gridx = x; gridy = y; } /** Draws this grid inside a rectangle. @param g2 the graphics context @param bounds the bounding rectangle */ public void draw(Graphics2D g2, Rectangle2D bounds) { Color PALE_BLUE = new Color(0.9F, 0.8F, 0.9F); Color oldColor = g2.getColor(); g2.setColor(PALE_BLUE); Stroke oldStroke = g2.getStroke(); for (double x = bounds.getX(); x < bounds.getMaxX(); x += gridx) g2.draw(new Line2D.Double(x, bounds.getY(), x, bounds.getMaxY())); for (double y = bounds.getY(); y < bounds.getMaxY(); y += gridy) g2.draw(new Line2D.Double(bounds.getX(), y, bounds.getMaxX(), y)); g2.setStroke(oldStroke); g2.setColor(oldColor); } /** Snaps a point to the nearest grid point @param p the point to snap. After the call, the coordinates of p are changed so that p falls on the grid. */ public void snap(Point2D p) { double x; if (gridx == 0) x = p.getX(); else x = Math.round(p.getX() / gridx) * gridx; double y; if (gridy == 0) y = p.getY(); else y = Math.round(p.getY() / gridy) * gridy; p.setLocation(x, y); } /** Snaps a rectangle to the nearest grid points @param r the rectangle to snap. After the call, the coordinates of r are changed so that all of its corners falls on the grid. */ public void snap(RectangularShape r) { double x; double w; w = r.getWidth(); if (gridx == 0) { x = r.getX(); } else { x = Math.round(r.getX() / gridx) * gridx; // w = Math.ceil(r.getWidth() / (2 * gridx)) * (2 * gridx); } double y; double h; h = r.getHeight(); if (gridy == 0) { y = r.getY(); } else { y = Math.round(r.getY() / gridy) * gridy; // h = Math.ceil(r.getHeight() / (2 * gridy)) * (2 * gridy); } r.setFrame(x, y, w, h); } private double gridx; private double gridy; }