Class CountryRenderer

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

public class CountryRenderer extends Object
Renders a collection of GeoJsonCountryLoader.CountryFeature instances onto an IContainer using a supplied IMapProjection and its associated MapTheme.

Rendering pipeline

  1. Iterate over all country features.
  2. For each polygon ring, split it at any antimeridian crossing (detected via IMapProjection.crossesSeam(double, double)) into at most two WorldPolygon objects in world (projection-space) coordinates.
  3. 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.
  4. 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 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 be null
      projection - the map projection; must not be null
  • Method Details

    • setFillLand

      public void setFillLand(boolean fillLand)
      Enables or disables filling country polygons with the land color from the active MapTheme.
      Parameters:
      fillLand - true to fill land areas; false to draw borders only
    • setDrawBorders

      public void setDrawBorders(boolean drawBorders)
      Enables or disables drawing country political borders.
      Parameters:
      drawBorders - true to draw borders; false to skip
    • setUseAntialias

      public void setUseAntialias(boolean useAntialias)
      Enables or disables antialiased rendering.
      Parameters:
      useAntialias - true to enable antialiasing
    • render

      public void render(Graphics2D g2, IContainer container)
      Renders country fills and borders.

      This method preserves the original public behavior. Use renderFill(Graphics2D, IContainer) or renderBorders(Graphics2D, IContainer) when fills and borders are hosted by separate MDI layers.

      Parameters:
      g2 - graphics context
      container - rendering container
    • renderFill

      public void renderFill(Graphics2D g2, IContainer container)
      Renders country polygon fills without drawing political boundaries.
      Parameters:
      g2 - graphics context
      container - rendering container
    • renderBorders

      public void renderBorders(Graphics2D g2, IContainer container)
      Renders country political boundaries without filling the polygons.
      Parameters:
      g2 - graphics context
      container - rendering container
    • pickCountry

      public GeoJsonCountryLoader.CountryFeature pickCountry(Point mouseLocal, IContainer container)
      Returns the first country whose projected polygon contains the given mouse position, or null if no country is hit.

      Requires a prior render call. The hit-test uses the WorldPolygon cache that is built during render(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer). If render has not been called yet (or has been called on a different IContainer), this method will always return null.

      Parameters:
      mouseLocal - mouse position in the container's local coordinate space; must not be null
      container - container providing the local-to-world transform; must not be null
      Returns:
      the hit country, or null
    • copyStyleFrom

      public void copyStyleFrom(CountryRenderer source)
      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

      public Color getFillColor()
      Returns the effective country-fill color.
      Returns:
      custom color when set; otherwise the active theme land color
    • getBorderColor

      public Color getBorderColor()
      Returns the effective country-boundary color.
      Returns:
      custom color when set; otherwise the theme border color
    • setBorderColor

      public void setBorderColor(Color color)
      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:
      true when no custom fill color is installed
    • setFillColor

      public void setFillColor(Color color)
      Sets a custom country-fill color.
      Parameters:
      color - custom color, or null to use the map theme
    • useThemeFillColor

      public void useThemeFillColor()
      Restores the map theme's land color.