com.hfg.svg.SvgLine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.svg;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import com.hfg.css.CSS;
import com.hfg.css.CSSDeclaration;
import com.hfg.css.CssUtil;
import com.hfg.graphics.Graphics2DState;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
//------------------------------------------------------------------------------
/**
* Object representation of an SVG (Scalable Vector Graphics) line 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 SvgLine extends AbstractSvgNode implements SvgNode
{
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private SvgLine()
{
super(SVG.line);
}
//---------------------------------------------------------------------------
public SvgLine(Point2D inStart, Point2D inEnd)
{
this();
setAttribute(SvgAttr.x1, SVG.formatCoordinate(inStart.getX()));
setAttribute(SvgAttr.y1, SVG.formatCoordinate(inStart.getY()));
setAttribute(SvgAttr.x2, SVG.formatCoordinate(inEnd.getX()));
setAttribute(SvgAttr.y2, SVG.formatCoordinate(inEnd.getY()));
}
//---------------------------------------------------------------------------
public SvgLine(Point inStart, Point inEnd)
{
this();
setAttribute(SvgAttr.x1, (int) inStart.getX());
setAttribute(SvgAttr.y1, (int) inStart.getY());
setAttribute(SvgAttr.x2, (int) inEnd.getX());
setAttribute(SvgAttr.y2, (int) inEnd.getY());
}
//---------------------------------------------------------------------------
public SvgLine(XMLTag inXMLTag)
{
this();
inXMLTag.verifyTagName(getTagName());
if (CollectionUtil.hasValues(inXMLTag.getAttributes()))
{
for (XMLAttribute attr : inXMLTag.getAttributes())
{
setAttribute(attr.clone());
}
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public SvgLine setTitle(String inValue)
{
setAttribute(SvgAttr.title, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgLine setStroke(Color inColor)
{
addStyle(SvgAttr.stroke + ":" + CssUtil.colorToCssValue(inColor));
return this;
}
//---------------------------------------------------------------------------
public SvgLine setStrokeWidth(int inValue)
{
addStyle(SvgAttr.strokeWidth + ":" + inValue);
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgLine addStyle(CharSequence inValue)
{
return (SvgLine) super.addStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgLine setStyle(CharSequence inValue)
{
return (SvgLine) super.setStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgLine setTransform(String inValue)
{
return (SvgLine) super.setTransform(inValue);
}
//---------------------------------------------------------------------------
public SvgLine setOpacity(int inPctOpacity)
{
addStyle("stroke-opacity:" + inPctOpacity + "%");
return this;
}
//---------------------------------------------------------------------------
public SvgLine setX1(int inValue)
{
setAttribute(SvgAttr.x1, inValue);
return this;
}
//---------------------------------------------------------------------------
public int getX1()
{
return Integer.parseInt(getAttributeValue(SvgAttr.x1));
}
//---------------------------------------------------------------------------
public SvgLine setY1(int inValue)
{
setAttribute(SvgAttr.y1, inValue);
return this;
}
//---------------------------------------------------------------------------
public int getY1()
{
return Integer.parseInt(getAttributeValue(SvgAttr.y1));
}
//---------------------------------------------------------------------------
public SvgLine setX2(int inValue)
{
setAttribute(SvgAttr.x2, inValue);
return this;
}
//---------------------------------------------------------------------------
public int getX2()
{
return Integer.parseInt(getAttributeValue(SvgAttr.x2));
}
//---------------------------------------------------------------------------
public SvgLine setY2(int inValue)
{
setAttribute(SvgAttr.y2, inValue);
return this;
}
//---------------------------------------------------------------------------
public int getY2()
{
return Integer.parseInt(getAttributeValue(SvgAttr.y2));
}
//---------------------------------------------------------------------------
@Override
public Rectangle2D getBoundsBox()
{
double minX = (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0);
double minY = (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0);
double maxX = (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0);
double maxY = (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0);
for (XMLizable node : getSubtags())
{
if (node instanceof SvgNode)
{
Rectangle2D rect = ((SvgNode)node).getBoundsBox();
if (rect.getX() < minX) minX = rect.getX();
if (rect.getY() < minY) minY = rect.getY();
if (rect.getMaxX() > maxX) maxX = rect.getMaxX();
if (rect.getMaxY() > maxY) maxY = rect.getMaxY();
}
}
Rectangle2D boundsBox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
adjustBoundsForTransform(boundsBox);
return boundsBox;
}
//--------------------------------------------------------------------------
@Override
public String toString()
{
return toXML();
}
//--------------------------------------------------------------------------
@Override
public void draw(Graphics2D g2, CSS inCSS)
{
// Save settings
Graphics2DState origState = new Graphics2DState(g2);
List cssDeclarations = getCSSDeclarations(inCSS);
Paint paint = getG2StrokeColor(cssDeclarations);
if (paint != null) g2.setPaint(paint);
Stroke stroke = getG2Stroke(cssDeclarations);
if (stroke != null) g2.setStroke(stroke);
applyTransforms(g2);
int x1 = (int) (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0);
int y1 = (int) (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0);
int x2 = (int) (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0);
int y2 = (int) (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0);
g2.drawLine(x1, y1, x2, y2);
// Restore settings
origState.applyTo(g2);
}
//--------------------------------------------------------------------------
public Float length()
{
int x1 = (int) (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0);
int y1 = (int) (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0);
int x2 = (int) (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0);
int y2 = (int) (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0);
return (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
}