com.hfg.svg.SvgRect 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.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Stroke;
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.graphics.Graphics2DState;
import com.hfg.graphics.ColorUtil;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
//------------------------------------------------------------------------------
/**
* Object representation of an SVG (Scalable Vector Graphics) rect 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 SvgRect extends AbstractSvgNode implements SvgNode
{
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public SvgRect()
{
super(SVG.rect);
}
//---------------------------------------------------------------------------
public SvgRect(Rectangle inRect)
{
this();
setAttribute(SvgAttr.x, SVG.formatCoordinate(inRect.getX()));
setAttribute(SvgAttr.y, SVG.formatCoordinate(inRect.getY()));
setWidth(inRect.getWidth());
setHeight(inRect.getHeight());
// setStyle(DEFAULT_STYLE);
}
//---------------------------------------------------------------------------
public SvgRect(Rectangle2D inRect)
{
this();
setAttribute(SvgAttr.x, SVG.formatCoordinate(inRect.getX()));
setAttribute(SvgAttr.y, SVG.formatCoordinate(inRect.getY()));
setWidth(inRect.getWidth());
setHeight(inRect.getHeight());
// setStyle(DEFAULT_STYLE);
}
//---------------------------------------------------------------------------
public SvgRect(XMLTag inXMLTag)
{
this();
initFromXMLTag(inXMLTag);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public SvgRect setFill(Color inColor)
{
setAttribute(SvgAttr.fill, (inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none));
return this;
}
//---------------------------------------------------------------------------
/**
* Specifies a value for the opacity of this rectangle (both its fill and stroke).
* @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque).
* @return this object to allow for method chaining
*/
public SvgRect setOpacity(float inOpacity)
{
rangeCheckOpacityValue(inOpacity);
setAttribute(SvgAttr.opacity, inOpacity);
return this;
}
//---------------------------------------------------------------------------
/**
* Specifies a value for the fill opacity of this rectangle.
* @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque).
* @return this object to allow for method chaining
*/
public SvgRect setFillOpacity(float inOpacity)
{
rangeCheckOpacityValue(inOpacity);
setAttribute(SvgAttr.fillOpacity, inOpacity);
return this;
}
//---------------------------------------------------------------------------
/**
* Specifies a value for the stroke opacity of this rectangle.
* @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque).
* @return this object to allow for method chaining
*/
public SvgRect setStrokeOpacity(float inOpacity)
{
rangeCheckOpacityValue(inOpacity);
setAttribute(SvgAttr.strokeOpacity, inOpacity);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setStroke(Color inColor)
{
setAttribute(SvgAttr.stroke, inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setStrokeWidth(int inValue)
{
setAttribute(SvgAttr.strokeWidth, inValue);
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgRect addStyle(CharSequence inValue)
{
return (SvgRect) super.addStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgRect setStyle(CharSequence inValue)
{
return (SvgRect) super.setStyle(inValue);
}
//---------------------------------------------------------------------------
@Override
public SvgRect setTransform(String inValue)
{
return (SvgRect) super.setTransform(inValue);
}
//---------------------------------------------------------------------------
public SvgRect setOnMouseOver(String inValue)
{
setAttribute(SvgAttr.onmouseover, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setOnMouseOut(String inValue)
{
setAttribute(SvgAttr.onmouseout, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setOnMouseDown(String inValue)
{
setAttribute(SvgAttr.onmousedown, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setPosition(Point2D inValue)
{
setAttribute(SvgAttr.x, (int) inValue.getX());
setAttribute(SvgAttr.y, (int) inValue.getY());
return this;
}
//---------------------------------------------------------------------------
public Float getX()
{
String xAttr = getAttributeValue(SvgAttr.x);
return (StringUtil.isSet(xAttr) ? Float.parseFloat(xAttr) : null);
}
//---------------------------------------------------------------------------
public Float getY()
{
String yAttr = getAttributeValue(SvgAttr.y);
return (StringUtil.isSet(yAttr) ? Float.parseFloat(yAttr) : null);
}
//---------------------------------------------------------------------------
public SvgRect setRx(int inValue)
{
setAttribute(SvgAttr.rx, inValue);
return this;
}
//---------------------------------------------------------------------------
public Integer getRx()
{
String attr = getAttributeValue(SvgAttr.rx);
return (StringUtil.isSet(attr) ? (int) Float.parseFloat(attr) : null);
}
//---------------------------------------------------------------------------
public SvgRect setRy(int inValue)
{
setAttribute(SvgAttr.ry, inValue);
return this;
}
//---------------------------------------------------------------------------
public Integer getRy()
{
String attr = getAttributeValue(SvgAttr.ry);
return (StringUtil.isSet(attr) ? (int) Float.parseFloat(attr) : null);
}
//---------------------------------------------------------------------------
public SvgRect setWidth(int inValue)
{
setAttribute(SvgAttr.width, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setWidth(float inValue)
{
setAttribute(SvgAttr.width, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgRect setWidth(double inValue)
{
setAttribute(SvgAttr.width, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public Float getWidth()
{
String attr = getAttributeValue(SvgAttr.width);
return (StringUtil.isSet(attr) ? Float.parseFloat(attr) : null);
}
//---------------------------------------------------------------------------
public SvgRect setHeight(int inValue)
{
setAttribute(SvgAttr.height, inValue);
return this;
}
//---------------------------------------------------------------------------
public SvgRect setHeight(float inValue)
{
setAttribute(SvgAttr.height, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public SvgRect setHeight(double inValue)
{
setAttribute(SvgAttr.height, SVG.formatCoordinate(inValue));
return this;
}
//---------------------------------------------------------------------------
public Float getHeight()
{
String attr = getAttributeValue(SvgAttr.height);
return (StringUtil.isSet(attr) ? Float.parseFloat(attr) : null);
}
//---------------------------------------------------------------------------
@Override
public Rectangle2D getBoundsBox()
{
float minX = (getAttributeValue(SvgAttr.x) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x)) : 0);
float minY = (getAttributeValue(SvgAttr.y) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y)) : 0);
float maxX = minX + (getAttributeValue(SvgAttr.width) != null ? Float.parseFloat(getAttributeValue(SvgAttr.width)) : 0);
float maxY = minY + (getAttributeValue(SvgAttr.height) != null ? Float.parseFloat(getAttributeValue(SvgAttr.height)) : 0);
for (XMLizable node : getSubtags())
{
if (node instanceof SvgNode)
{
Rectangle2D 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();
}
}
Rectangle2D boundsBox = new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY).getBounds();
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);
// Java2D uses int instead of float
int x1 = getX().intValue();
int y1 = getY().intValue();
int width = getWidth().intValue();
int height = getHeight().intValue();
List cssDeclarations = getCSSDeclarations(inCSS);
Stroke stroke = getG2Stroke(cssDeclarations);
if (stroke != null) g2.setStroke(stroke);
Composite composite = getG2Composite();
if (composite != null)
{
g2.setComposite(composite);
}
applyTransforms(g2);
// Fill the rect.
Paint paint = getG2Paint(cssDeclarations);
if (paint != null)
{
g2.setPaint(paint);
g2.fillRect(x1, y1, width, height);
}
// Outline the rect.
paint = getG2StrokeColor(cssDeclarations);
if (paint != null)
{
g2.setPaint(paint);
g2.drawRect(x1, y1, width, height);
}
// Restore settings
origState.applyTo(g2);
}
}