001package com.hfg.svg; 002 003import com.hfg.graphics.Graphics2DState; 004import com.hfg.graphics.ColorUtil; 005import com.hfg.xml.XMLTag; 006 007import java.awt.*; 008import java.awt.geom.Ellipse2D; 009 010//------------------------------------------------------------------------------ 011/** 012 * Object representation of an SVG (Scalable Vector Graphics) 'ellipse' tag. 013 * <div> 014 * @author J. Alex Taylor, hairyfatguy.com 015 * </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class SvgEllipse extends AbstractSvgNode implements SvgNode 039{ 040 041 //########################################################################## 042 // CONSTRUCTORS 043 //########################################################################## 044 045 //--------------------------------------------------------------------------- 046 public SvgEllipse() 047 { 048 super(SVG.ellipse); 049 } 050 051 //--------------------------------------------------------------------------- 052 public SvgEllipse(XMLTag inXMLTag) 053 { 054 this(); 055 initFromXMLTag(inXMLTag); 056 } 057 058 //########################################################################## 059 // PUBLIC METHODS 060 //########################################################################## 061 062 063 //--------------------------------------------------------------------------- 064 public SvgEllipse setId(String inValue) 065 { 066 setAttribute(SvgAttr.id, inValue); 067 return this; 068 } 069 070 //--------------------------------------------------------------------------- 071 public SvgEllipse setRx(int inValue) 072 { 073 setAttribute(SvgAttr.rx, inValue); 074 return this; 075 } 076 077 //--------------------------------------------------------------------------- 078 public SvgEllipse setRy(int inValue) 079 { 080 setAttribute(SvgAttr.ry, inValue); 081 return this; 082 } 083 084 //--------------------------------------------------------------------------- 085 public SvgEllipse setCx(int inValue) 086 { 087 setAttribute(SvgAttr.cx, inValue); 088 return this; 089 } 090 091 //--------------------------------------------------------------------------- 092 public SvgEllipse setCy(int inValue) 093 { 094 setAttribute(SvgAttr.cy, inValue); 095 return this; 096 } 097 098 //-------------------------------------------------------------------------- 099 @Override 100 public SvgEllipse addStyle(String inValue) 101 { 102 return (SvgEllipse) super.addStyle(inValue); 103 } 104 105 //--------------------------------------------------------------------------- 106 @Override 107 public SvgEllipse setStyle(String inValue) 108 { 109 return (SvgEllipse) super.setStyle(inValue); 110 } 111 112 //--------------------------------------------------------------------------- 113 public SvgEllipse setFill(Color inColor) 114 { 115 setAttribute(SvgAttr.fill, (inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none)); 116 return this; 117 } 118 119 //--------------------------------------------------------------------------- 120 public SvgEllipse setOpacity(int inPctOpacity) 121 { 122 setAttribute(SvgAttr.opacity, inPctOpacity); 123 return this; 124 } 125 126 //--------------------------------------------------------------------------- 127 public SvgEllipse setFillOpacity(int inPctOpacity) 128 { 129 setAttribute(SvgAttr.fillOpacity, inPctOpacity); 130 return this; 131 } 132 133 //--------------------------------------------------------------------------- 134 public SvgEllipse setStroke(Color inColor) 135 { 136 setAttribute(SvgAttr.stroke, inColor != null ? "#" + ColorUtil.colorToHex(inColor) : SvgAttr.Value.none); 137 return this; 138 } 139 140 //--------------------------------------------------------------------------- 141 public SvgEllipse setStrokeWidth(int inValue) 142 { 143 setAttribute(SvgAttr.strokeWidth, inValue); 144 return this; 145 } 146 147 //--------------------------------------------------------------------------- 148 @Override 149 public Rectangle getBoundsBox() 150 { 151 int cx = (getAttributeValue(SvgAttr.cx) != null ? Integer.parseInt(getAttributeValue(SvgAttr.cx)) : 0); 152 int cy = (getAttributeValue(SvgAttr.cy) != null ? Integer.parseInt(getAttributeValue(SvgAttr.cy)) : 0); 153 int rx = (getAttributeValue(SvgAttr.rx) != null ? Integer.parseInt(getAttributeValue(SvgAttr.rx)) : 0); 154 int ry = (getAttributeValue(SvgAttr.ry) != null ? Integer.parseInt(getAttributeValue(SvgAttr.ry)) : 0); 155 156 Ellipse2D ellipse = new Ellipse2D.Float(cx - rx, cy - ry, rx * 2, ry * 2); 157 Rectangle boundsBox = ellipse.getBounds(); 158 adjustBoundsForTransform(boundsBox); 159 160 return boundsBox; 161 } 162 163 //-------------------------------------------------------------------------- 164 @Override 165 public void draw(Graphics2D g2) 166 { 167 // Save settings 168 Graphics2DState origState = new Graphics2DState(g2); 169 170 int cx = Integer.parseInt(getAttributeValue(SvgAttr.cx)); 171 int cy = Integer.parseInt(getAttributeValue(SvgAttr.cy)); 172 int rx = Integer.parseInt(getAttributeValue(SvgAttr.rx)); 173 int ry = Integer.parseInt(getAttributeValue(SvgAttr.ry)); 174 175 applyTransforms(g2); 176 177 Ellipse2D ellipse = new Ellipse2D.Float(cx - rx, cy - ry, rx * 2, ry * 2); 178 179 // Fill the ellipse. 180 Paint paint = getG2Paint(); 181 if (paint != null) 182 { 183 g2.setPaint(paint); 184 g2.fill(ellipse); 185 } 186 187 // Outline the ellipse. 188 paint = getG2StrokeColor(); 189 if (paint != null) 190 { 191 Stroke stroke = getG2Stroke(); 192 if (stroke != null) 193 { 194 g2.setStroke(stroke); 195 } 196 197 g2.setPaint(paint); 198 g2.draw(ellipse); 199 } 200 201 // Restore settings 202 origState.applyTo(g2); 203 } 204 205}