Class GraticuleRenderer
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 Summary
ConstructorsConstructorDescriptionGraticuleRenderer(IMapProjection projection) Creates a graticule renderer backed by the given projection. -
Method Summary
Modifier and TypeMethodDescriptionvoidcopyStyleFrom(GraticuleRenderer source) Copies user-configurable settings from another graticule renderer.Returns the effective coordinate-label color.doubleReturns the latitude step size in radians (fixed mode / adaptive fallback).doubleReturns the longitude step size in radians (fixed mode / adaptive fallback).Returns the associated projection.booleanReturns whether graticule spacing adapts to the current zoom level.booleanReturns whether coordinate labels are drawn along the viewport edges.booleanReturns whether the map outline is drawn before the graticule lines.voidrender(Graphics2D g2, IContainer container) Renders the map outline (if enabled) followed by all graticule lines.voidsetAdaptive(boolean adaptive) Enables or disables zoom-adaptive spacing.voidsetDrawLabels(boolean drawLabels) Enables or disables coordinate labels along the viewport edges (e.g.voidsetDrawOutline(boolean drawOutline) Sets whether the map outline is drawn before the graticule lines.voidsetLabelColor(Color color) Sets a custom coordinate-label color.voidsetLatitudeStepDeg(double latitudeStepDeg) Convenience method that sets the latitude step size in degrees.voidsetLatitudeStepRad(double latitudeStepRad) Sets the latitude step size in radians.voidsetLongitudeStepDeg(double longitudeStepDeg) Convenience method that sets the longitude step size in degrees.voidsetLongitudeStepRad(double longitudeStepRad) Sets the longitude step size in radians.voidRestores theme-controlled coordinate-label coloring.
-
Constructor Details
-
GraticuleRenderer
Creates a graticule renderer backed by the given projection.- Parameters:
projection- the projection to use for outline and line drawing; must not benull- Throws:
IllegalArgumentException- ifprojectionisnull
-
-
Method Details
-
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- iflatitudeStepRad≤ 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- iflatitudeStepDeg≤ 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- iflongitudeStepRad≤ 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- iflongitudeStepDeg≤ 0
-
isDrawOutline
public boolean isDrawOutline()Returns whether the map outline is drawn before the graticule lines.- Returns:
trueif the outline will be drawn
-
setDrawOutline
public void setDrawOutline(boolean drawOutline) Sets whether the map outline is drawn before the graticule lines.- Parameters:
drawOutline-trueto draw the outline;falseto suppress it
-
isAdaptive
public boolean isAdaptive()Returns whether graticule spacing adapts to the current zoom level.- Returns:
trueif adaptive spacing is enabled
-
setAdaptive
public void setAdaptive(boolean adaptive) Enables or disables zoom-adaptive spacing. When disabled, the fixedgetLatitudeStepRad()/getLongitudeStepRad()are used across the full graticule, reproducing the original behavior.- Parameters:
adaptive-trueto adapt spacing to zoom;falsefor fixed spacing
-
isDrawLabels
public boolean isDrawLabels()Returns whether coordinate labels are drawn along the viewport edges.- Returns:
trueif 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 thatMapContainersupplies.- Parameters:
drawLabels-trueto draw edge labels
-
render
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 benullcontainer- container providing the world-to-local transform and, when adaptive, the inverse and viewport; must not benull
-
copyStyleFrom
Copies user-configurable settings from another graticule renderer.The projection itself is deliberately not copied.
- Parameters:
source- source renderer
-
getLabelColor
Returns the effective coordinate-label color.- Returns:
- custom color when set; otherwise the theme label or graticule color
-
setLabelColor
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.
-