PaintPolygons.java

  1. package org.opentrafficsim.draw.core;

  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Path2D;

  5. import org.opentrafficsim.core.geometry.OTSLine3D;
  6. import org.opentrafficsim.core.geometry.OTSPoint3D;

  7. import org.opentrafficsim.core.geometry.DirectedPoint;

  8. /**
  9.  * Paint a (series of) filled polygon(s) defined as a Path2D.Double
  10.  * <p>
  11.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * $LastChangedDate: 2018-10-10 23:23:48 +0200 (Wed, 10 Oct 2018) $, @version $Revision: 4693 $, by $Author: averbraeck $,
  15.  * initial version 10 apr. 2015 <br>
  16.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  17.  */
  18. public final class PaintPolygons
  19. {
  20.     /** Do not instantiate this class. */
  21.     private PaintPolygons()
  22.     {
  23.         // Cannot be instantiated.
  24.     }

  25.     /** Dummy coordinate that forces the drawing operation to start a new path. */
  26.     public static final OTSPoint3D NEWPATH = new OTSPoint3D(Double.NaN, Double.NaN, Double.NaN);

  27.     /**
  28.      * Paint (fill) a polygon or a series of polygons.
  29.      * @param graphics Graphics2D; the graphics environment
  30.      * @param color Color; the color to use
  31.      * @param referencePoint DirectedPoint; the reference point
  32.      * @param line OTSLine3D; array of points
  33.      * @param fill boolean; fill or just contour
  34.      */
  35.     public static void paintMultiPolygon(final Graphics2D graphics, final Color color, final DirectedPoint referencePoint,
  36.             final OTSLine3D line, final boolean fill)
  37.     {
  38.         graphics.setColor(color);
  39.         Path2D.Double path = new Path2D.Double();
  40.         boolean withinPath = false;
  41.         for (OTSPoint3D point : line.getPoints())
  42.         {
  43.             if (NEWPATH.equals(point))
  44.             {
  45.                 if (withinPath)
  46.                 {
  47.                     path.closePath();
  48.                     if (fill)
  49.                     {
  50.                         graphics.fill(path);
  51.                     }
  52.                 }
  53.                 path = new Path2D.Double();
  54.                 withinPath = false;
  55.             }
  56.             else if (!withinPath)
  57.             {
  58.                 withinPath = true;
  59.                 path.moveTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  60.             }
  61.             else
  62.             {
  63.                 path.lineTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  64.             }
  65.         }
  66.         if (withinPath)
  67.         {
  68.             path.closePath();
  69.             if (fill)
  70.             {
  71.                 graphics.fill(path);
  72.             }
  73.             else
  74.             {
  75.                 graphics.draw(path);
  76.             }
  77.         }
  78.     }

  79. }