com.hfg.svg.SvgCircle 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.StringUtil;
import com.hfg.xml.XMLTag;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
//------------------------------------------------------------------------------
/**
* Object representation of an SVG (Scalable Vector Graphics) 'circle' 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 SvgCircle extends AbstractSvgNode implements SvgNode
{
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SvgCircle()
{
super(SVG.circle);
}
//---------------------------------------------------------------------------
public SvgCircle(Point2D inCenter, int inRadius)
{
super(SVG.circle);
setCenter(inCenter);
setR(inRadius);
}
//---------------------------------------------------------------------------
public SvgCircle(XMLTag inXMLTag)
{
this();
initFromXMLTag(inXMLTag);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
@Override
public SvgCircle setId(String inValue)
{
return (SvgCircle) super.setId(inValue);
}
//---------------------------------------------------------------------------
public SvgCircle setR(int inValue)
{
setAttribute(SvgAttr.r, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setR(float inValue)
{
setAttribute(SvgAttr.r, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setR(double inValue)
{
setAttribute(SvgAttr.r, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public int getR()
{
int r = 0;
String stringValue = getAttributeValue(SvgAttr.r);
if (StringUtil.isSet(stringValue))
{
r = (int) Float.parseFloat(stringValue);
}
return r;
}
//---------------------------------------------------------------------------
public SvgCircle setCx(int inValue)
{
setAttribute(SvgAttr.cx, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCx(float inValue)
{
setAttribute(SvgAttr.cx, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCx(double inValue)
{
setAttribute(SvgAttr.cx, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCy(int inValue)
{
setAttribute(SvgAttr.cy, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCy(float inValue)
{
setAttribute(SvgAttr.cy, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCy(double inValue)
{
setAttribute(SvgAttr.cy, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setCenter(Point2D inValue)
{
setCx((int) inValue.getX());
setCy((int) inValue.getY());
return this;
}
//---------------------------------------------------------------------------
/**
Specifies the upper left corner of the bounding rectangle.
*/
public SvgCircle setPosition(Point2D inValue)
{
setCx((int) inValue.getX() + getR());
setCy((int) inValue.getY() + getR());
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgCircle addStyle(String inValue)
{
return (SvgCircle) super.addStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgCircle setStyle(String inValue)
{
return (SvgCircle) super.setStyle(inValue);
}
//---------------------------------------------------------------------------
public SvgCircle setFill(Color inColor)
{
setAttribute(SvgAttr.fill, (inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setOpacity(int inPctOpacity)
{
setAttribute(SvgAttr.opacity, inPctOpacity);
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setFillOpacity(int inPctOpacity)
{
setAttribute(SvgAttr.fillOpacity, inPctOpacity);
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setStroke(Color inColor)
{
addStyle(SvgAttr.stroke + ":" + (inColor != null ? CssUtil.colorToCssValue(inColor) : SvgAttr.Value.none));
return this;
}
//---------------------------------------------------------------------------
public SvgCircle setStrokeWidth(int inValue)
{
setAttribute(SvgAttr.strokeWidth, inValue);
return this;
}
//---------------------------------------------------------------------------
@Override
public Rectangle getBoundsBox()
{
float cx = (getAttributeValue(SvgAttr.cx) != null ? Float.parseFloat(getAttributeValue(SvgAttr.cx)) : 0);
float cy = (getAttributeValue(SvgAttr.cy) != null ? Float.parseFloat(getAttributeValue(SvgAttr.cy)) : 0);
float radius = (getAttributeValue(SvgAttr.r) != null ? Float.parseFloat(getAttributeValue(SvgAttr.r)) : 0);
Ellipse2D ellipse = new Ellipse2D.Float(cx - radius, cy - radius, radius * 2, radius * 2);
Rectangle boundsBox = ellipse.getBounds();
adjustBoundsForTransform(boundsBox);
return boundsBox;
}
//--------------------------------------------------------------------------
@Override
public void draw(Graphics2D g2)
{
// Save settings
Graphics2DState origState = new Graphics2DState(g2);
float cx = (getAttributeValue(SvgAttr.cx) != null ? Float.parseFloat(getAttributeValue(SvgAttr.cx)) : 0);
float cy = (getAttributeValue(SvgAttr.cy) != null ? Float.parseFloat(getAttributeValue(SvgAttr.cy)) : 0);
float radius = (getAttributeValue(SvgAttr.r) != null ? Float.parseFloat(getAttributeValue(SvgAttr.r)) : 0);
applyTransforms(g2);
Ellipse2D ellipse = new Ellipse2D.Float(cx - radius, cy - radius, radius * 2, radius * 2);
// Fill the circle
Paint paint = getG2Paint();
if (paint != null)
{
g2.setPaint(paint);
g2.fill(ellipse);
}
// Outline the circle
paint = getG2StrokeColor();
if (paint != null)
{
Stroke stroke = getG2Stroke();
if (stroke != null)
{
g2.setStroke(stroke);
}
g2.setPaint(paint);
g2.draw(ellipse);
}
// Restore settings
origState.applyTo(g2);
}
}