All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.svg.SvgPolyline Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.svg;

import com.hfg.graphics.Graphics2DState;
import com.hfg.graphics.ColorUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLTag;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

//------------------------------------------------------------------------------
/**
 * Object representation of an SVG (Scalable Vector Graphics) polyline tag.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class SvgPolyline extends AbstractSvgNode implements SvgNode
{
   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SvgPolyline()
   {
      super(SVG.polyline);
   }

   //---------------------------------------------------------------------------
   public SvgPolyline(List inPoints)
   {
      this();
      setPoints(inPoints);
   }

   //---------------------------------------------------------------------------
   public SvgPolyline(XMLTag inXMLTag)
   {
      this();
      initFromXMLTag(inXMLTag);
   }


   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SvgPolyline setFill(Color inColor)
   {
      setAttribute(SvgAttr.fill, (inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none));
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgPolyline setOpacity(int inPctOpacity)
   {
      setAttribute(SvgAttr.opacity, inPctOpacity);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgPolyline setFillOpacity(int inPctOpacity)
   {
      setAttribute(SvgAttr.fillOpacity, inPctOpacity);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgPolyline setStroke(Color inColor)
   {
      setAttribute(SvgAttr.stroke, inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none);
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgPolyline setStrokeWidth(int inValue)
   {
      setAttribute(SvgAttr.strokeWidth, inValue);
      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public SvgPolyline addStyle(String inValue)
   {
      return (SvgPolyline) super.addStyle(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgPolyline setStyle(String inValue)
   {
      return (SvgPolyline) super.setStyle(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgPolyline setTransform(String inValue)
   {
      return (SvgPolyline) super.setTransform(inValue);
   }



   //---------------------------------------------------------------------------
   public SvgPolyline setPoints(List inPoints)
   {
      StringBuilderPlus points = new StringBuilderPlus().setDelimiter(" ");
      if (CollectionUtil.hasValues(inPoints))
      {
         for (Point2D point : inPoints)
         {
            points.delimitedAppend((int) point.getX() + "," + (int) point.getY());
         }
      }

      return setPoints(points.toString());
   }

   //---------------------------------------------------------------------------
   public SvgPolyline setPoints(String inValue)
   {
      setAttribute(SvgAttr.points, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getPoints()
   {
      return getAttributeValue(SvgAttr.points);
   }

   //---------------------------------------------------------------------------
   public List getPointList()
   {
      List points = null;
      String pointsString = getPoints();
      if (StringUtil.isSet(pointsString))
      {
         String[] pieces = pointsString.trim().split("[\\s,]+");
         if (pieces.length%2 != 0)
         {
            throw new RuntimeException("Odd number of numbers in the polyline's points attribute!");
         }

         points = new ArrayList<>();
         int index = 0;
         while (index < pieces.length)
         {
            points.add(new Point2D.Float(Float.parseFloat(pieces[index++]), Float.parseFloat(pieces[index++])));
         }
      }

      return points;
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return toXML();
   }

   //---------------------------------------------------------------------------
   @Override
   public Rectangle getBoundsBox()
   {
      Rectangle boundsBox = generatePolygon().getBounds();
      adjustBoundsForTransform(boundsBox);

      return boundsBox;
   }

   //--------------------------------------------------------------------------
   @Override
   public void draw(Graphics2D g2)
   {
      // Save settings
      Graphics2DState origState = new Graphics2DState(g2);

      applyTransforms(g2);

      Polygon polygon = generatePolygon();

      // Fill the polygon
      Paint paint = getG2Paint();
      if (paint != null)
      {
         g2.setPaint(paint);
         g2.fill(polygon);
      }

      // Outline the polygon
      paint = getG2StrokeColor();
      if (paint != null)
      {
         g2.setPaint(paint);

         Stroke stroke = getG2Stroke();
         if (stroke != null)
         {
            g2.setStroke(stroke);
         }

         g2.draw(polygon);
      }

      // Restore settings
      origState.applyTo(g2);
   }

   //--------------------------------------------------------------------------
   private Polygon generatePolygon()
   {
      Polygon polygon = new Polygon();

      List pointList = getPointList();
      if (CollectionUtil.hasValues(pointList))
      {
         for (Point2D.Float point : pointList)
         {
            polygon.addPoint((int) point.getX(), (int) point.getY());
         }
      }

      return polygon;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy