001package com.hfg.svg; 002 003import java.awt.*; 004import java.awt.geom.Point2D; 005import java.awt.geom.Rectangle2D; 006 007import com.hfg.css.CssUtil; 008import com.hfg.graphics.Graphics2DState; 009import com.hfg.util.collection.CollectionUtil; 010import com.hfg.xml.XMLAttribute; 011import com.hfg.xml.XMLTag; 012import com.hfg.xml.XMLizable; 013 014//------------------------------------------------------------------------------ 015/** 016 * Object representation of an SVG (Scalable Vector Graphics) line tag. 017 * 018 * @author J. Alex Taylor, hairyfatguy.com 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class SvgLine extends AbstractSvgNode implements SvgNode 042{ 043 044 //########################################################################### 045 // CONSTRUCTORS 046 //########################################################################### 047 048 //--------------------------------------------------------------------------- 049 private SvgLine() 050 { 051 super(SVG.line); 052 } 053 054 //--------------------------------------------------------------------------- 055 public SvgLine(Point2D inStart, Point2D inEnd) 056 { 057 this(); 058 setAttribute(SvgAttr.x1, SVG.formatCoordinate(inStart.getX())); 059 setAttribute(SvgAttr.y1, SVG.formatCoordinate(inStart.getY())); 060 setAttribute(SvgAttr.x2, SVG.formatCoordinate(inEnd.getX())); 061 setAttribute(SvgAttr.y2, SVG.formatCoordinate(inEnd.getY())); 062 } 063 064 //--------------------------------------------------------------------------- 065 public SvgLine(Point inStart, Point inEnd) 066 { 067 this(); 068 setAttribute(SvgAttr.x1, (int) inStart.getX()); 069 setAttribute(SvgAttr.y1, (int) inStart.getY()); 070 setAttribute(SvgAttr.x2, (int) inEnd.getX()); 071 setAttribute(SvgAttr.y2, (int) inEnd.getY()); 072 } 073 074 //--------------------------------------------------------------------------- 075 public SvgLine(XMLTag inXMLTag) 076 { 077 this(); 078 079 inXMLTag.verifyTagName(getTagName()); 080 081 if (CollectionUtil.hasValues(inXMLTag.getAttributes())) 082 { 083 for (XMLAttribute attr : inXMLTag.getAttributes()) 084 { 085 setAttribute(attr.clone()); 086 } 087 } 088 } 089 090 091 //########################################################################### 092 // PUBLIC METHODS 093 //########################################################################### 094 095 //--------------------------------------------------------------------------- 096 public SvgLine setTitle(String inValue) 097 { 098 setAttribute(SvgAttr.title, inValue); 099 return this; 100 } 101 102 //--------------------------------------------------------------------------- 103 public SvgLine setStroke(Color inColor) 104 { 105 addStyle(SvgAttr.stroke + ":" + CssUtil.colorToCssValue(inColor)); 106 return this; 107 } 108 109 //--------------------------------------------------------------------------- 110 public SvgLine setStrokeWidth(int inValue) 111 { 112 addStyle(SvgAttr.strokeWidth + ":" + inValue); 113 return this; 114 } 115 116 //-------------------------------------------------------------------------- 117 @Override 118 public SvgLine addStyle(String inValue) 119 { 120 return (SvgLine) super.addStyle(inValue); 121 } 122 123 //--------------------------------------------------------------------------- 124 @Override 125 public SvgLine setStyle(String inValue) 126 { 127 return (SvgLine) super.setStyle(inValue); 128 } 129 130 //--------------------------------------------------------------------------- 131 @Override 132 public SvgLine setTransform(String inValue) 133 { 134 return (SvgLine) super.setTransform(inValue); 135 } 136 137 //--------------------------------------------------------------------------- 138 public SvgLine setOpacity(int inPctOpacity) 139 { 140 addStyle("stroke-opacity:" + inPctOpacity + "%"); 141 return this; 142 } 143 144 //--------------------------------------------------------------------------- 145 public int getX1() 146 { 147 return Integer.parseInt(getAttributeValue(SvgAttr.x1)); 148 } 149 150 //--------------------------------------------------------------------------- 151 public int getY1() 152 { 153 return Integer.parseInt(getAttributeValue(SvgAttr.y1)); 154 } 155 156 //--------------------------------------------------------------------------- 157 public int getX2() 158 { 159 return Integer.parseInt(getAttributeValue(SvgAttr.x2)); 160 } 161 162 //--------------------------------------------------------------------------- 163 public int getY2() 164 { 165 return Integer.parseInt(getAttributeValue(SvgAttr.y2)); 166 } 167 168 169 //--------------------------------------------------------------------------- 170 @Override 171 public Rectangle2D getBoundsBox() 172 { 173 double minX = (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0); 174 double minY = (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0); 175 double maxX = (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0); 176 double maxY = (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0); 177 178 for (XMLizable node : getSubtags()) 179 { 180 if (node instanceof SvgNode) 181 { 182 Rectangle2D rect = ((SvgNode)node).getBoundsBox(); 183 if (rect.getX() < minX) minX = rect.getX(); 184 if (rect.getY() < minY) minY = rect.getY(); 185 if (rect.getMaxX() > maxX) maxX = rect.getMaxX(); 186 if (rect.getMaxY() > maxY) maxY = rect.getMaxY(); 187 } 188 } 189 190 Rectangle2D boundsBox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); 191 adjustBoundsForTransform(boundsBox); 192 193 return boundsBox; 194 } 195 196 //-------------------------------------------------------------------------- 197 @Override 198 public String toString() 199 { 200 return toXML(); 201 } 202 203 //-------------------------------------------------------------------------- 204 @Override 205 public void draw(Graphics2D g2) 206 { 207 // Save settings 208 Graphics2DState origState = new Graphics2DState(g2); 209 210 Paint paint = getG2StrokeColor(); 211 if (paint != null) g2.setPaint(paint); 212 Stroke stroke = getG2Stroke(); 213 if (stroke != null) g2.setStroke(stroke); 214 215 applyTransforms(g2); 216 217 int x1 = (int) (getAttributeValue(SvgAttr.x1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x1)) : 0); 218 int y1 = (int) (getAttributeValue(SvgAttr.y1) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y1)) : 0); 219 int x2 = (int) (getAttributeValue(SvgAttr.x2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.x2)) : 0); 220 int y2 = (int) (getAttributeValue(SvgAttr.y2) != null ? Float.parseFloat(getAttributeValue(SvgAttr.y2)) : 0); 221 222 g2.drawLine(x1, y1, x2, y2); 223 224 225 // Restore settings 226 origState.applyTo(g2); 227 } 228 229}