Class CartesianGrid2D

All Implemented Interfaces:
Graph.Graphic, NamedObjectOps

public class CartesianGrid2D extends AnimationObject2D
Class to create a Cartesian (rectilinear) grid. A grid consists of a series of evenly spaced horizontal and vertical lines, with two lines intersection at the point (0.0, 0.0) in graph coordinate space. There are three types of lines:
  • Axis lines are vertical and horizontal lines that go through the point (0.0, 0.0) in graph coordinate space.
  • Coarse-grid lines are lines spaced by a specified spacing, which is the same in the X and Y directions.
  • Fine-grid lines are lines at locations that subdivide the coarse grid so that two adjacent and parallel course-grid lines will be separated by (N-1) fine-grid lines, where N is the subspacing divisor. When N is 1, a fine grid is not shown.
Each of these three types of lines have their own color, with the color choice for axis lines overriding the choice for coarse-grid lines.

Unlike many animation objects, a CartesianGrid2D has an infinite size, but only the portion that is visible within a graph's dimensions will be drawn. To add a grid to a graph, use the graph's add method.

  • Constructor Details

  • Method Details

    • getSpacing

      public double getSpacing()
      Get the grid spacing. A value of 0.0 indicates that a default value will be used.
      Returns:
      the grid spacing in GCS units
    • getSubspacing

      public int getSubspacing()
      Get the grid subspacing divisor.
      Returns:
      the grid subspacing divisor
    • setSpacing

      public void setSpacing(double spacing)
      Set the grid spacing.
      Parameters:
      spacing - the grid spacing in GCS units; 0.0 for a default
    • setSubspacing

      public void setSubspacing(int subspacing)
      Set the grid subspacing divisor. The value determines how a coarse grid is partitioned into a fine grid. Each box in the coarse grid is partitioned into subspacing boxes along each axis. For example,
      • If subspacing is 1, coarse-grid boxes are not partitioed.
      • If subspacing is 2, coarse-grid boxes are halved in each direction, creating 4 fine-grid boxes per coarse-grid box.
      • If subspacing is 5, coarse-grid boxes are split into fifths with a total of 25 boxes per coarse-grid box.
      The coarse grid and fine grid have boarders consisting of solid lines with different colors, but the same width.
      Parameters:
      subspacing - the subspacing divisor
    • getSpacingColor

      public Color getSpacingColor()
      Get the color for the coarse-grid lines.
      Returns:
      the coarse-grid line color
    • getSubspacingColor

      public Color getSubspacingColor()
      Get the color for the fine-grid lines.
      Returns:
      the fine-grid line color
    • getAxisColor

      public Color getAxisColor()
      Get the color for the axis. A point is on the axis if in graph cordinate space either x = 0 or y = 0. The width of a line representing an axis is the same as for other lines in the grid.
      Returns:
      the axis-line color
    • setColors

      public void setColors(Color axisColor, Color spacingColor, Color subspacingColor)
      Set the colors for grid lines. When a point is drawn, the axis color is used if the point is on an axis, otherwise the spacing color is drawn if the point is on the coarse grid. If the point is not on the coarse grid (which includes the axes), and is on the fine grid, the subspacing color is used.
      Parameters:
      axisColor - the color for the axis
      spacingColor - the color for the coarse grid
      subspacingColor - the color for the fine grid
    • getStrokeWidth

      public double getStrokeWidth()
      Get the width of the stroke used to draw grid lines.
      Returns:
      the stroke width
    • setStrokeWidth

      public void setStrokeWidth(double strokeWidth)
      Set the width of the stroke used to draw grid lines.
      Parameters:
      strokeWidth - the stroke width; 0.0 or negative for the default
    • addTo

      public void addTo(Graph g, Graphics2D g2d, Graphics2D g2dGCS)
      Description copied from interface: Graph.Graphic
      Add this object to a graph. The drawing operation is assumed to be a complex one that might involve fills, etc.

      Any modifications to g2d or g2dGCS made by an implementation of this method should be undone before this method returns. For classes provided by the org.bzdev.anim2d package, such modifications must be undone before this method returns.

      Two graphics contexts are provided as arguments. g2d will typically be used as the first argument to the graph's draw or fill methods. In this case, the shape of an object is described in graph coordinate space but the widths of strokes drawn by fill operations, gradient paint, etc., have user-space units. The result is that line widths as they appear in an image are not sensitive to the mapping from graph coordinate space to user space. One should use the draw and fill methods specified by Graph, using g2d as their first argument. By contrast, if a line width, etc., should be in graph-coordinate space units, one can use the g2dGCS argument directly. This will rarely be done when plotting a graph but is useful in animations.

      Parameters:
      g - the graph on which this object should be drawn
      g2d - the user-space Graphics2D to use to draw the Graphic
      g2dGCS - the graph-coordinate space Graphics2D to use to draw the Graphic
    • clone

      protected Object clone() throws CloneNotSupportedException
      Creates and returns a copy of this object. This method will throw the exception CloneNotSupportedException if the object is interned.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException - a clone could not be created
      See Also:
    • isInterned

      public boolean isInterned()
      Determine if an object is interned in a object namer's tables.
      Specified by:
      isInterned in interface NamedObjectOps
      Returns:
      true if the object is interned; false if not
    • getObjectNamer

      protected Simulation getObjectNamer()
      Get the object namer for a named object.
      Returns:
      the object namer for this named object
    • getName

      public final String getName()
      Get an object's name.
      Specified by:
      getName in interface NamedObjectOps
      Returns:
      the name of the object
    • canDelete

      public boolean canDelete()
      Determine if this named object can be deleted. A named object can be deleted if the method delete has not been called and if the object is not in a state that prevents the object from being deleted. Subclasses that override this method must call canDelete() for their superclasses and return false if the superclass' canDelete method returns false. The default method returns true if delete() has not been called and returned true.
      Specified by:
      canDelete in interface NamedObjectOps
      Returns:
      true if this object can be deleted; false otherwise
    • delete

      public final boolean delete()
      Delete an object. An object can only be deleted once. If this method returns true, the object (if interned) will have been removed from the object namer tables.

      The implementations provided by DefaultNamedObect and generated because of a @NamedObject annotation provide a protected method named onDelete. A subclass that overrides onDelete() must call the onDelete method of its superclass after it's onDelete method has been called and any cleanup actions performed. In some cases, this may happen at a later time (e.g., if a thread is used for some of the cleanup operations or if it is otherwise necessary to wait).

      Specified by:
      delete in interface NamedObjectOps
      Returns:
      true if the deletion request was accepted; false otherwise
    • isDeleted

      public final boolean isDeleted()
      Determine if an object has been deleted. An object is deleted if the method delete() has been called and returned true.
      Specified by:
      isDeleted in interface NamedObjectOps
      Returns:
      true if deleted; false otherwise
    • deletePending

      public final boolean deletePending()
      Determine if an object is being deleted. An deletion is pending if the method delete() has been called and returned true but the deletion has not been completed.
      Specified by:
      deletePending in interface NamedObjectOps
      Returns:
      true if deletion is pending; false otherwise