Class Graphs.CartesianGrid

java.lang.Object
org.bzdev.graphs.Graphs.CartesianGrid
All Implemented Interfaces:
Graph.Graphic
Enclosing class:
Graphs

public static class Graphs.CartesianGrid extends Object implements Graph.Graphic
Class to create a Cartesian 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. While any choice is allowed, as a general rule, the color should be lighter as the feature becomes more fine-grained: the axis color should be more noticeable than the spacing color, which should in tern be more noticeable than the subspacing color.
  • Field Details

    • SPACING_COLOR_CSS

      public static final String SPACING_COLOR_CSS
      The default color for the course-grained grid as a CSS specification.
      See Also:
    • SUBSPACING_COLOR_CSS

      public static final String SUBSPACING_COLOR_CSS
      The default color for the fine-grained grid as a CSS specification.
      See Also:
    • AXIS_COLOR_CSS

      public static final String AXIS_COLOR_CSS
      The default color for the axes passing through (0.0, 0.0) as a CSS specification.
      See Also:
    • SPACING_COLOR

      public static final Color SPACING_COLOR
      The default color for the coarse-grained grid.
    • SUBSPACING_COLOR

      public static final Color SUBSPACING_COLOR
      The default color for the fine-grained grid.
    • AXIS_COLOR

      public static final Color AXIS_COLOR
      The default color for the axes passing through (0.0, 0.0)
    • STROKE_WIDTH

      public static final double STROKE_WIDTH
      The default stroke width for a Cartesian grid.
      See Also:
  • Constructor Details

    • CartesianGrid

      public CartesianGrid()
      Constructor.
    • CartesianGrid

      public CartesianGrid(double spacing, int subspacing)
      Constructor.
      Parameters:
      spacing - the grid spacing in GCS (Graph Coordinate Space) units; 0.0 for a default
      subspacing - the subspacing divisor
  • Method Details

    • getSpacing

      public double getSpacing()
      Get the grid spacing. This defines the spacing for course-grid lines. A value of 0.0 indicates that a default value will be used. The default assumes that roughly 10 increments should appear before the lower and upper values defined for the graph in either the X or Y direction, whichever is shortest.
      Returns:
      the grid spacing in GCS (Graph Coordinate Space) 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 (Graph Coordinate Space) units; 0.0 or a negative value 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 partitioned.
      • 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; 0 or a negative value will be replaced with 1
    • 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 coordinate 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. If the axis color is the same as the spacing color, the axis will not be visually discernible.
      Parameters:
      axisColor - the color for the axis; null for a default
      spacingColor - the color for the coarse grid; null for a default
      subspacingColor - the color for the fine grid; null for a default
    • 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.

      Specified by:
      addTo in interface Graph.Graphic
      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