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

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

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

import com.hfg.css.CSS;
import com.hfg.css.CSSDeclaration;
import com.hfg.graphics.Graphics2DState;
import com.hfg.graphics.ColorUtil;
import com.hfg.xml.XMLTag;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.List;

//------------------------------------------------------------------------------
/**
 * Object representation of an SVG (Scalable Vector Graphics) 'ellipse' 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 SvgEllipse extends AbstractSvgNode implements SvgNode { //########################################################################## // CONSTRUCTORS //########################################################################## //--------------------------------------------------------------------------- public SvgEllipse() { super(SVG.ellipse); } //--------------------------------------------------------------------------- public SvgEllipse(XMLTag inXMLTag) { this(); initFromXMLTag(inXMLTag); } //########################################################################## // PUBLIC METHODS //########################################################################## //--------------------------------------------------------------------------- public SvgEllipse setId(String inValue) { setAttribute(SvgAttr.id, inValue); return this; } //--------------------------------------------------------------------------- public SvgEllipse setRx(int inValue) { setAttribute(SvgAttr.rx, inValue); return this; } //--------------------------------------------------------------------------- public SvgEllipse setRy(int inValue) { setAttribute(SvgAttr.ry, inValue); return this; } //--------------------------------------------------------------------------- public SvgEllipse setCx(int inValue) { setAttribute(SvgAttr.cx, inValue); return this; } //--------------------------------------------------------------------------- public SvgEllipse setCy(int inValue) { setAttribute(SvgAttr.cy, inValue); return this; } //-------------------------------------------------------------------------- @Override public SvgEllipse addStyle(CharSequence inValue) { return (SvgEllipse) super.addStyle(inValue); } //--------------------------------------------------------------------------- @Override public SvgEllipse setStyle(CharSequence inValue) { return (SvgEllipse) super.setStyle(inValue); } //--------------------------------------------------------------------------- public SvgEllipse setFill(Color inColor) { setAttribute(SvgAttr.fill, (inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none)); return this; } //--------------------------------------------------------------------------- public SvgEllipse setOpacity(int inPctOpacity) { setAttribute(SvgAttr.opacity, inPctOpacity); return this; } //--------------------------------------------------------------------------- public SvgEllipse setFillOpacity(int inPctOpacity) { setAttribute(SvgAttr.fillOpacity, inPctOpacity); return this; } //--------------------------------------------------------------------------- public SvgEllipse setStroke(Color inColor) { setAttribute(SvgAttr.stroke, inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none); return this; } //--------------------------------------------------------------------------- public SvgEllipse setStrokeWidth(int inValue) { setAttribute(SvgAttr.strokeWidth, inValue); return this; } //--------------------------------------------------------------------------- @Override public Rectangle getBoundsBox() { int cx = (getAttributeValue(SvgAttr.cx) != null ? Integer.parseInt(getAttributeValue(SvgAttr.cx)) : 0); int cy = (getAttributeValue(SvgAttr.cy) != null ? Integer.parseInt(getAttributeValue(SvgAttr.cy)) : 0); int rx = (getAttributeValue(SvgAttr.rx) != null ? Integer.parseInt(getAttributeValue(SvgAttr.rx)) : 0); int ry = (getAttributeValue(SvgAttr.ry) != null ? Integer.parseInt(getAttributeValue(SvgAttr.ry)) : 0); Ellipse2D ellipse = new Ellipse2D.Float(cx - rx, cy - ry, rx * 2, ry * 2); Rectangle boundsBox = ellipse.getBounds(); adjustBoundsForTransform(boundsBox); return boundsBox; } //-------------------------------------------------------------------------- @Override public void draw(Graphics2D g2, CSS inCSS) { // Save settings Graphics2DState origState = new Graphics2DState(g2); int cx = Integer.parseInt(getAttributeValue(SvgAttr.cx)); int cy = Integer.parseInt(getAttributeValue(SvgAttr.cy)); int rx = Integer.parseInt(getAttributeValue(SvgAttr.rx)); int ry = Integer.parseInt(getAttributeValue(SvgAttr.ry)); List cssDeclarations = getCSSDeclarations(inCSS); applyTransforms(g2); Ellipse2D ellipse = new Ellipse2D.Float(cx - rx, cy - ry, rx * 2, ry * 2); // Fill the ellipse. Paint paint = getG2Paint(cssDeclarations); if (paint != null) { g2.setPaint(paint); g2.fill(ellipse); } // Outline the ellipse. paint = getG2StrokeColor(cssDeclarations); if (paint != null) { Stroke stroke = getG2Stroke(cssDeclarations); if (stroke != null) { g2.setStroke(stroke); } g2.setPaint(paint); g2.draw(ellipse); } // Restore settings origState.applyTo(g2); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy