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 com.hfg.css.CssUtil;
import com.hfg.graphics.Graphics2DState;
import com.hfg.graphics.ColorUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
import java.awt.*;
import java.awt.geom.Point2D;
//------------------------------------------------------------------------------
/**
* 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
{
private static Color sDefaultStroke = Color.BLACK;
private static int sDefaultStrokeWidth = 1;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private SvgLine()
{
super(SVG.line);
setStroke(sDefaultStroke);
setStrokeWidth(sDefaultStrokeWidth);
}
//---------------------------------------------------------------------------
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(String inValue)
{
return (SvgLine) super.addStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgLine setStyle(String inValue)
{
return (SvgLine) super.setStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgLine setTransform(String inValue)
{
return (SvgLine) super.setTransform(inValue);
}
//---------------------------------------------------------------------------
public int getX1()
{
return Integer.parseInt(getAttributeValue(SvgAttr.x1));
}
//---------------------------------------------------------------------------
public int getY1()
{
return Integer.parseInt(getAttributeValue(SvgAttr.y1));
}
//---------------------------------------------------------------------------
public int getX2()
{
return Integer.parseInt(getAttributeValue(SvgAttr.x2));
}
//---------------------------------------------------------------------------
public int getY2()
{
return Integer.parseInt(getAttributeValue(SvgAttr.y2));
}
//---------------------------------------------------------------------------
@Override
public Rectangle getBoundsBox()
{
int minX = (int) (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0);
int minY = (int) (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0);
int maxX = (int) (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0);
int maxY = (int) (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0);
for (XMLizable node : getSubtags())
{
if (node instanceof SvgNode)
{
Rectangle rect = ((SvgNode)node).getBoundsBox();
if (rect.getX() < minX) minX = (int) rect.getX();
if (rect.getY() < minY) minY = (int) rect.getY();
if (rect.getMaxX() > maxX) maxX = (int) rect.getMaxX();
if (rect.getMaxY() > maxY) maxY = (int) rect.getMaxY();
}
}
Rectangle boundsBox = new Rectangle(minX, minY, maxX - minX, maxY - minY);
adjustBoundsForTransform(boundsBox);
return boundsBox;
}
//--------------------------------------------------------------------------
@Override
public String toString()
{
return toXML();
}
//--------------------------------------------------------------------------
@Override
public void draw(Graphics2D g2)
{
// Save settings
Graphics2DState origState = new Graphics2DState(g2);
Paint paint = getG2StrokeColor();
if (paint != null) g2.setPaint(paint);
Stroke stroke = getG2Stroke();
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);
}
}