PaintLine.java

  1. package org.opentrafficsim.core.network.animation;

  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.Stroke;
  6. import java.awt.geom.Path2D;

  7. import org.opentrafficsim.core.geometry.OTSLine3D;
  8. import org.opentrafficsim.core.geometry.OTSPoint3D;

  9. import nl.tudelft.simulation.language.d3.DirectedPoint;

  10. /**
  11.  * Paint a line as a Path2D.Double
  12.  * <p>
  13.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * <p>
  16.  * $LastChangedDate: 2016-01-15 09:03:55 +0100 (Fri, 15 Jan 2016) $, @version $Revision: 1698 $, by $Author: averbraeck $,
  17.  * initial version 10 apr. 2015 <br>
  18.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  19.  */
  20. public final class PaintLine
  21. {
  22.     /** Do not instantiate this class. */
  23.     private PaintLine()
  24.     {
  25.         // Cannot be instantiated.
  26.     }

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

  29.     /**
  30.      * Paint line.
  31.      * @param graphics Graphics2D; the graphics environment
  32.      * @param color Color; the color to use
  33.      * @param width the width to use
  34.      * @param referencePoint DirectedPoint; the reference point
  35.      * @param line array of points
  36.      */
  37.     public static void paintLine(final Graphics2D graphics, final Color color, final double width,
  38.         final DirectedPoint referencePoint, final OTSLine3D line)
  39.     {
  40.         graphics.setColor(color);
  41.         Stroke oldStroke = graphics.getStroke();
  42.         graphics.setStroke(new BasicStroke((float) width));
  43.         Path2D.Double path = new Path2D.Double();
  44.         OTSPoint3D point = line.getFirst();
  45.         path.moveTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  46.         for (int i = 1; i < line.getPoints().length; i++)
  47.         {
  48.             point = line.getPoints()[i];
  49.             path.lineTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  50.         }
  51.         graphics.draw(path);
  52.         graphics.setStroke(oldStroke);
  53.     }

  54. }