JHotDraw 7.2

org.jhotdraw.draw
Class GridConstrainer

java.lang.Object
  extended by org.jhotdraw.beans.AbstractBean
      extended by org.jhotdraw.draw.AbstractConstrainer
          extended by org.jhotdraw.draw.GridConstrainer
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Constrainer

public class GridConstrainer
extends AbstractConstrainer

Constrains a point such that it falls on a grid.

Version:
4.0 2007-12-17 Huw Jones, Werner Randelshofer: Adapted due to changes in Constrainer interface.
3.1 2007-09-15 Werner Randelshofer: Added constructor which allows to control the visiblity of the grid.
3.0 2007-08-01 Werner Randelshofer: Reworked.
2.1.1 2006-07-05 Werner Randelshofer: Fixed drawing bug.
2.1 2006-07-03 Werner Randelshofer: Method isVisible added.
2.0 2006-01-14 Werner Randelshofer: Changed to support double precision coordinates.
1.0 2004-03-17 Werner Randelshofer: Created.
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.jhotdraw.draw.AbstractConstrainer
changeEvent, listenerList
 
Fields inherited from class org.jhotdraw.beans.AbstractBean
propertySupport
 
Constructor Summary
GridConstrainer()
          Creates a new instance with a grid of 1x1.
GridConstrainer(double width, double height)
          Creates a new instance with the specified grid size, and by 11.25° (in degrees) for rotations.
GridConstrainer(double width, double height, boolean visible)
          Creates a new instance with the specified grid size.
GridConstrainer(double width, double height, double theta, boolean visible)
          Creates a new instance with the specified grid size.
 
Method Summary
 double constrainAngle(double angle)
          Constrains the given angle (in radians).
 java.awt.geom.Point2D.Double constrainPoint(java.awt.geom.Point2D.Double p)
          Constrains a point to the closest grid point in any direction.
protected  java.awt.geom.Point2D.Double constrainPoint(java.awt.geom.Point2D.Double p, TranslationDirection dir)
          Constrains the placement of a point towards a direction.
 java.awt.geom.Rectangle2D.Double constrainRectangle(java.awt.geom.Rectangle2D.Double r)
          Constrains the placement of a rectangle towards the closest constrainment in any direction.
protected  java.awt.geom.Rectangle2D.Double constrainRectangle(java.awt.geom.Rectangle2D.Double r, TranslationDirection dir)
          Constrains the placement of a rectangle towards a direction.
 void draw(java.awt.Graphics2D g, DrawingView view)
          Draws the constrainer grid for the specified drawing view.
 double getHeight()
           
 int getMajorGridSpacing()
          Spacing between major grid lines.
 double getTheta()
           
 double getWidth()
           
 boolean isVisible()
           
 double rotateAngle(double angle, RotationDirection dir)
          Moves the given angle (in radians) to the closest constrained orientation in the specified direction.
 void setHeight(double newValue)
           
 void setMajorGridSpacing(int newValue)
          Spacing between major grid lines.
 void setTheta(double newValue)
           
 void setVisible(boolean newValue)
           
 void setWidth(double newValue)
           
 java.lang.String toString()
           
 java.awt.geom.Point2D.Double translatePoint(java.awt.geom.Point2D.Double p, TranslationDirection dir)
          Moves a point to the closest grid point in a direction.
 java.awt.geom.Rectangle2D.Double translateRectangle(java.awt.geom.Rectangle2D.Double r, TranslationDirection dir)
          Moves a rectangle to the closest constrained location in the specified direction.
 
Methods inherited from class org.jhotdraw.draw.AbstractConstrainer
addChangeListener, clone, fireStateChanged, removeChangeListener
 
Methods inherited from class org.jhotdraw.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, firePropertyChange, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GridConstrainer

public GridConstrainer()
Creates a new instance with a grid of 1x1.


GridConstrainer

public GridConstrainer(double width,
                       double height)
Creates a new instance with the specified grid size, and by 11.25° (in degrees) for rotations. The grid is visible.

Parameters:
width - The width of a grid cell.
height - The height of a grid cell.

GridConstrainer

public GridConstrainer(double width,
                       double height,
                       boolean visible)
Creates a new instance with the specified grid size. and by 11.25° (in degrees) for rotations.

Parameters:
width - The width of a grid cell.
height - The height of a grid cell.
visible - Wether the grid is visible or not.

GridConstrainer

public GridConstrainer(double width,
                       double height,
                       double theta,
                       boolean visible)
Creates a new instance with the specified grid size.

Parameters:
width - The width of a grid cell.
height - The height of a grid cell.
theta - The theta for rotations in radians.
visible - Wether the grid is visible or not.
Method Detail

getWidth

public double getWidth()

getHeight

public double getHeight()

getTheta

public double getTheta()

setWidth

public void setWidth(double newValue)

setHeight

public void setHeight(double newValue)

setTheta

public void setTheta(double newValue)

constrainPoint

public java.awt.geom.Point2D.Double constrainPoint(java.awt.geom.Point2D.Double p)
Constrains a point to the closest grid point in any direction.

Parameters:
p - A point on the drawing.
Returns:
Returns the constrained point.

constrainPoint

protected java.awt.geom.Point2D.Double constrainPoint(java.awt.geom.Point2D.Double p,
                                                      TranslationDirection dir)
Constrains the placement of a point towards a direction.

This method changes the point which is passed as a parameter.

Parameters:
p - A point on the drawing.
dir - A direction.
Returns:
Returns the constrained point.

translatePoint

public java.awt.geom.Point2D.Double translatePoint(java.awt.geom.Point2D.Double p,
                                                   TranslationDirection dir)
Moves a point to the closest grid point in a direction.

Parameters:
p - A point on the drawing.
dir - A direction.
Returns:
Returns the constrained point.

constrainRectangle

public java.awt.geom.Rectangle2D.Double constrainRectangle(java.awt.geom.Rectangle2D.Double r)
Description copied from interface: Constrainer
Constrains the placement of a rectangle towards the closest constrainment in any direction.

This method changes the location of the rectangle which is passed as a parameter. This method does not change the size of the rectangle.

Parameters:
r - A rectangle on the drawing.
Returns:
Returns the constrained rectangle.

constrainRectangle

protected java.awt.geom.Rectangle2D.Double constrainRectangle(java.awt.geom.Rectangle2D.Double r,
                                                              TranslationDirection dir)
Constrains the placement of a rectangle towards a direction.

This method changes the location of the rectangle which is passed as a parameter. This method does not change the size of the rectangle.

Parameters:
r - A rectangle on the drawing.
dir - A direction.
Returns:
Returns the constrained rectangle.

translateRectangle

public java.awt.geom.Rectangle2D.Double translateRectangle(java.awt.geom.Rectangle2D.Double r,
                                                           TranslationDirection dir)
Description copied from interface: Constrainer
Moves a rectangle to the closest constrained location in the specified direction.

This method changes the location of the rectangle which is passed as a parameter. This method only performs a translation - it does not change the size of the rectangle.

Parameters:
r - A rectangle on the drawing.
dir - A direction.
Returns:
Returns the constrained rectangle.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

isVisible

public boolean isVisible()

setVisible

public void setVisible(boolean newValue)

getMajorGridSpacing

public int getMajorGridSpacing()
Spacing between major grid lines.


setMajorGridSpacing

public void setMajorGridSpacing(int newValue)
Spacing between major grid lines.


draw

public void draw(java.awt.Graphics2D g,
                 DrawingView view)
Description copied from interface: Constrainer
Draws the constrainer grid for the specified drawing view.


constrainAngle

public double constrainAngle(double angle)
Description copied from interface: Constrainer
Constrains the given angle (in radians). This method changes the angle which is passed as a parameter.

Parameters:
angle - The angle (in radians).
Returns:
The closest constrained angle (in radians).

rotateAngle

public double rotateAngle(double angle,
                          RotationDirection dir)
Description copied from interface: Constrainer
Moves the given angle (in radians) to the closest constrained orientation in the specified direction.

Parameters:
angle - The angle (in radians).
Returns:
The closest constrained angle (in radians) in the specified direction.

Copyright 1996-2009 (c) by the authors and contributors of the JHotDraw project.
Some rights reserved.