Class GraticuleRenderer

java.lang.Object
edu.cnu.mdi.mapping.render.GraticuleRenderer

public final class GraticuleRenderer extends Object
Renders a map outline and latitude/longitude graticule lines for a given IMapProjection, with optional zoom-adaptive line spacing.

Adaptive spacing

In adaptive mode (the default), the graticule step is chosen from the geographic span currently visible in the viewport so that a roughly constant number of lines is shown regardless of zoom. The ideal step is snapped to a "nice" angular value drawn from NICE_STEPS_RAD (…30°, 15°, 10°, 5°, 2°, 1°, 30′, 15′, …) so that line positions and any labels remain on clean round values as the user zooms.

The visible span is measured empirically: points sampled within the projection's device-space clip bounds are inverse-projected back to (λ, φ) via MapContainer.localToLatLon(Point, Point2D.Double). Because this uses the projection's own inverse, it is correct for every projection — including the azimuthal ones (orthographic, Lambert) where a viewport corner may fall off the projected disk and invert to NaN. Such samples are skipped. Sampling the projection clip rather than the whole component also handles a fitted map with horizontal or vertical letterboxing.

Fixed spacing

When isAdaptive() is false, the renderer behaves exactly as before: it iterates the whole graticule using getLatitudeStepRad() and getLongitudeStepRad(). Those setters remain the source of the step in fixed mode and the fallback step in adaptive mode. Coordinate labels are available in both fixed and adaptive modes.

Rendering

Actual drawing is still fully delegated to the projection's IMapProjection.drawLatitudeLine(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer, double) / IMapProjection.drawLongitudeLine(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer, double), so graticule color, stroke, and curve shape continue to match the projection and its MapTheme. This class only decides which parallels and meridians to draw, and over what latitude/longitude extent.

  • Constructor Details

    • GraticuleRenderer

      public GraticuleRenderer(IMapProjection projection)
      Creates a graticule renderer backed by the given projection.
      Parameters:
      projection - the projection to use for outline and line drawing; must not be null
      Throws:
      IllegalArgumentException - if projection is null
  • Method Details

    • getProjection

      public IMapProjection getProjection()
      Returns the associated projection.
      Returns:
      the projection used by this renderer; never null
    • getLatitudeStepRad

      public double getLatitudeStepRad()
      Returns the latitude step size in radians (fixed mode / adaptive fallback).
      Returns:
      latitude step in radians (> 0)
    • setLatitudeStepRad

      public void setLatitudeStepRad(double latitudeStepRad)
      Sets the latitude step size in radians.
      Parameters:
      latitudeStepRad - step size in radians; must be > 0
      Throws:
      IllegalArgumentException - if latitudeStepRad ≤ 0
    • setLatitudeStepDeg

      public void setLatitudeStepDeg(double latitudeStepDeg)
      Convenience method that sets the latitude step size in degrees.
      Parameters:
      latitudeStepDeg - step size in degrees; must be > 0
      Throws:
      IllegalArgumentException - if latitudeStepDeg ≤ 0
    • getLongitudeStepRad

      public double getLongitudeStepRad()
      Returns the longitude step size in radians (fixed mode / adaptive fallback).
      Returns:
      longitude step in radians (> 0)
    • setLongitudeStepRad

      public void setLongitudeStepRad(double longitudeStepRad)
      Sets the longitude step size in radians.
      Parameters:
      longitudeStepRad - step size in radians; must be > 0
      Throws:
      IllegalArgumentException - if longitudeStepRad ≤ 0
    • setLongitudeStepDeg

      public void setLongitudeStepDeg(double longitudeStepDeg)
      Convenience method that sets the longitude step size in degrees.
      Parameters:
      longitudeStepDeg - step size in degrees; must be > 0
      Throws:
      IllegalArgumentException - if longitudeStepDeg ≤ 0
    • isDrawOutline

      public boolean isDrawOutline()
      Returns whether the map outline is drawn before the graticule lines.
      Returns:
      true if the outline will be drawn
    • setDrawOutline

      public void setDrawOutline(boolean drawOutline)
      Sets whether the map outline is drawn before the graticule lines.
      Parameters:
      drawOutline - true to draw the outline; false to suppress it
    • isAdaptive

      public boolean isAdaptive()
      Returns whether graticule spacing adapts to the current zoom level.
      Returns:
      true if adaptive spacing is enabled
    • setAdaptive

      public void setAdaptive(boolean adaptive)
      Enables or disables zoom-adaptive spacing. When disabled, the fixed getLatitudeStepRad() / getLongitudeStepRad() are used across the full graticule, reproducing the original behavior.
      Parameters:
      adaptive - true to adapt spacing to zoom; false for fixed spacing
    • isDrawLabels

      public boolean isDrawLabels()
      Returns whether coordinate labels are drawn along the viewport edges.
      Returns:
      true if edge labels are enabled
    • setDrawLabels

      public void setDrawLabels(boolean drawLabels)
      Enables or disables coordinate labels along the viewport edges (e.g. "39°N", "132°E"). Labels are placed where each parallel/meridian enters the viewport in both fixed and adaptive spacing modes. They require the inverse + viewport that MapContainer supplies.
      Parameters:
      drawLabels - true to draw edge labels
    • render

      public void render(Graphics2D g2, IContainer container)
      Renders the map outline (if enabled) followed by all graticule lines.

      In fixed mode, parallels are drawn for φ ∈ [-π/2, +π/2] and meridians for λ ∈ [-π, +π] at the configured step. In adaptive mode, the visible (λ, φ) extent and a nice step are computed from the viewport, and only lines intersecting that extent are drawn (padded by one step so lines never pop in at the edges).

      Parameters:
      g2 - graphics context to draw into; must not be null
      container - container providing the world-to-local transform and, when adaptive, the inverse and viewport; must not be null
    • copyStyleFrom

      public void copyStyleFrom(GraticuleRenderer source)
      Copies user-configurable settings from another graticule renderer.

      The projection itself is deliberately not copied.

      Parameters:
      source - source renderer
    • getLabelColor

      public Color getLabelColor()
      Returns the effective coordinate-label color.
      Returns:
      custom color when set; otherwise the theme label or graticule color
    • setLabelColor

      public void setLabelColor(Color color)
      Sets a custom coordinate-label color.
      Parameters:
      color - custom color, or null to use the theme
    • useThemeLabelColor

      public void useThemeLabelColor()
      Restores theme-controlled coordinate-label coloring.