Class CountryRenderer
GeoJsonCountryLoader.CountryFeature instances
onto an IContainer using a supplied IMapProjection and its
associated MapTheme.
Rendering pipeline
- Iterate over all country features.
- For each polygon ring, split it at any antimeridian crossing (detected
via
IMapProjection.crossesSeam(double, double)) into at most twoWorldPolygonobjects in world (projection-space) coordinates. - Draw each polygon via
WorldGraphicsUtils.drawWorldPolygon(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer, edu.cnu.mdi.graphics.world.WorldPolygon, java.awt.Color, java.awt.Color, float), which converts world coordinates to screen space using the container's transform. - Cache the resulting polygons for later hit-testing by
pickCountry(java.awt.Point, edu.cnu.mdi.container.IContainer).
Seam splitting
Cylindrical projections (Mercator, Mollweide) have a seam at the edge of the
map. Polygon edges that cross the seam would otherwise be drawn as a long
horizontal line across the entire map. The renderer detects such crossings
via IMapProjection.crossesSeam(double, double) and routes subsequent
points into an "other side" polygon so each half renders correctly.
Hit-testing and cache dependency
pickCountry(Point, IContainer) uses a cache of WorldPolygon
objects that is built during render(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer). This means pickCountry
must be called after the first render(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer) call, and will return
null for all countries if called before any render. In practice this
is safe because a mouse-over event cannot occur before the first paint, but
callers should be aware of the dependency. The cache is fully rebuilt on
every render call so it always reflects the most recent projection
state.
Border stroke
The border color and stroke are taken from the theme via
MapTheme.getBorderColor() and MapTheme.getBorderStroke().
These are passed through to WorldGraphicsUtils.drawWorldPolygon(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer, edu.cnu.mdi.graphics.world.WorldPolygon, java.awt.Color, java.awt.Color, float) so
that theme switching is fully respected.
-
Constructor Summary
ConstructorsConstructorDescriptionCountryRenderer(List<GeoJsonCountryLoader.CountryFeature> countryFeatures, IMapProjection projection) Creates a renderer for the given country features and projection. -
Method Summary
Modifier and TypeMethodDescriptionvoidcopyStyleFrom(CountryRenderer source) Copies user-configurable rendering properties from another country renderer.Returns the effective country-boundary color.floatReturns the effective country-boundary width.Returns the effective country-fill color.pickCountry(Point mouseLocal, IContainer container) Returns the first country whose projected polygon contains the given mouse position, ornullif no country is hit.voidrender(Graphics2D g2, IContainer container) Renders country fills and borders.voidrenderBorders(Graphics2D g2, IContainer container) Renders country political boundaries without filling the polygons.voidrenderFill(Graphics2D g2, IContainer container) Renders country polygon fills without drawing political boundaries.voidsetBorderColor(Color color) Sets a custom country-boundary color.voidsetBorderWidth(float width) Sets a custom country-boundary width.voidsetDrawBorders(boolean drawBorders) Enables or disables drawing country political borders.voidsetFillColor(Color color) Sets a custom country-fill color.voidsetFillLand(boolean fillLand) Enables or disables filling country polygons with the land color from the activeMapTheme.voidsetUseAntialias(boolean useAntialias) Enables or disables antialiased rendering.booleanReturns whether the fill color comes from the map theme.voidRestores theme-controlled country-boundary styling.voidRestores the map theme's land color.
-
Constructor Details
-
CountryRenderer
public CountryRenderer(List<GeoJsonCountryLoader.CountryFeature> countryFeatures, IMapProjection projection) Creates a renderer for the given country features and projection.The theme is obtained from
projection.getTheme()at render time, so theme changes applied to the projection are reflected automatically without reconstructing the renderer.- Parameters:
countryFeatures- list of country features to render; must not benullprojection- the map projection; must not benull
-
-
Method Details
-
setFillLand
public void setFillLand(boolean fillLand) Enables or disables filling country polygons with the land color from the activeMapTheme.- Parameters:
fillLand-trueto fill land areas;falseto draw borders only
-
setDrawBorders
public void setDrawBorders(boolean drawBorders) Enables or disables drawing country political borders.- Parameters:
drawBorders-trueto draw borders;falseto skip
-
setUseAntialias
public void setUseAntialias(boolean useAntialias) Enables or disables antialiased rendering.- Parameters:
useAntialias-trueto enable antialiasing
-
render
Renders country fills and borders.This method preserves the original public behavior. Use
renderFill(Graphics2D, IContainer)orrenderBorders(Graphics2D, IContainer)when fills and borders are hosted by separate MDI layers.- Parameters:
g2- graphics contextcontainer- rendering container
-
renderFill
Renders country polygon fills without drawing political boundaries.- Parameters:
g2- graphics contextcontainer- rendering container
-
renderBorders
Renders country political boundaries without filling the polygons.- Parameters:
g2- graphics contextcontainer- rendering container
-
pickCountry
Returns the first country whose projected polygon contains the given mouse position, ornullif no country is hit.Requires a prior render call. The hit-test uses the
WorldPolygoncache that is built duringrender(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer). Ifrenderhas not been called yet (or has been called on a differentIContainer), this method will always returnnull.- Parameters:
mouseLocal- mouse position in the container's local coordinate space; must not benullcontainer- container providing the local-to-world transform; must not benull- Returns:
- the hit country, or
null
-
copyStyleFrom
Copies user-configurable rendering properties from another country renderer.Geographic data, projection state, and the projected polygon cache are deliberately not copied.
- Parameters:
source- renderer whose style settings should be copied
-
getFillColor
Returns the effective country-fill color.- Returns:
- custom color when set; otherwise the active theme land color
-
getBorderColor
Returns the effective country-boundary color.- Returns:
- custom color when set; otherwise the theme border color
-
setBorderColor
Sets a custom country-boundary color.- Parameters:
color- custom color, or null to use the map theme
-
getBorderWidth
public float getBorderWidth()Returns the effective country-boundary width.- Returns:
- line width in pixels
-
setBorderWidth
public void setBorderWidth(float width) Sets a custom country-boundary width.- Parameters:
width- positive line width
-
useThemeBorderStyle
public void useThemeBorderStyle()Restores theme-controlled country-boundary styling. -
usesThemeFillColor
public boolean usesThemeFillColor()Returns whether the fill color comes from the map theme.- Returns:
truewhen no custom fill color is installed
-
setFillColor
Sets a custom country-fill color.- Parameters:
color- custom color, ornullto use the map theme
-
useThemeFillColor
public void useThemeFillColor()Restores the map theme's land color.
-