001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on April 21, 2005, 10:43 AM 035 */ 036 037package com.kitfox.svg.app.beans; 038 039import com.kitfox.svg.*; 040import java.awt.*; 041import java.awt.geom.*; 042import java.net.*; 043import javax.swing.*; 044 045/** 046 * 047 * @author kitfox 048 */ 049public class SVGPanel extends JPanel 050{ 051 public static final long serialVersionUID = 1; 052 053 054 SVGUniverse svgUniverse = SVGCache.getSVGUniverse(); 055 056 private boolean antiAlias; 057 058// private String svgPath; 059 URI svgURI; 060 061 private boolean scaleToFit; 062 AffineTransform scaleXform = new AffineTransform(); 063 064 /** Creates new form SVGIcon */ 065 public SVGPanel() 066 { 067 initComponents(); 068 } 069 070 public int getSVGHeight() 071 { 072 if (scaleToFit) 073 { 074 return getPreferredSize().height; 075 } 076 077 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 078 if (diagram == null) 079 { 080 return 0; 081 } 082 return (int)diagram.getHeight(); 083 } 084 085 public int getSVGWidth() 086 { 087 if (scaleToFit) 088 { 089 return getPreferredSize().width; 090 } 091 092 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 093 if (diagram == null) 094 { 095 return 0; 096 } 097 return (int)diagram.getWidth(); 098 } 099 100 public void paintComponent(Graphics gg) 101 { 102 super.paintComponent(gg); 103 104 Graphics2D g = (Graphics2D)gg.create(); 105 paintComponent(g); 106 g.dispose(); 107 } 108 109 private void paintComponent(Graphics2D g) 110 { 111 Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 112 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); 113 114 115 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 116 if (diagram == null) 117 { 118 return; 119 } 120 121 if (!scaleToFit) 122 { 123 try 124 { 125 diagram.render(g); 126 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint); 127 } 128 catch (SVGException e) 129 { 130 throw new RuntimeException(e); 131 } 132 return; 133 } 134 135 Dimension dim = getSize(); 136 final int width = dim.width; 137 final int height = dim.height; 138 139 final Rectangle2D.Double rect = new Rectangle2D.Double(); 140 diagram.getViewRect(rect); 141 142 scaleXform.setToScale(width / rect.width, height / rect.height); 143 144 AffineTransform oldXform = g.getTransform(); 145 g.transform(scaleXform); 146 147 try 148 { 149 diagram.render(g); 150 } 151 catch (SVGException e) 152 { 153 throw new RuntimeException(e); 154 } 155 156 g.setTransform(oldXform); 157 158 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint); 159 } 160 161 public SVGUniverse getSvgUniverse() 162 { 163 return svgUniverse; 164 } 165 166 public void setSvgUniverse(SVGUniverse svgUniverse) 167 { 168 SVGUniverse old = this.svgUniverse; 169 this.svgUniverse = svgUniverse; 170 firePropertyChange("svgUniverse", old, svgUniverse); 171 } 172 173 public URI getSvgURI() 174 { 175 return svgURI; 176 } 177 178 public void setSvgURI(URI svgURI) 179 { 180 URI old = this.svgURI; 181 this.svgURI = svgURI; 182 firePropertyChange("svgURI", old, svgURI); 183 } 184 185 /** 186 * Most resources your component will want to access will be resources on your classpath. 187 * This method will interpret the passed string as a path in the classpath and use 188 * Class.getResource() to determine the URI of the SVG. 189 */ 190 public void setSvgResourcePath(String resourcePath) throws SVGException 191 { 192 URI old = this.svgURI; 193 194 try 195 { 196 svgURI = new URI(getClass().getResource(resourcePath).toString()); 197//System.err.println("SVGPanel: new URI " + svgURI + " from path " + resourcePath); 198 199 firePropertyChange("svgURI", old, svgURI); 200 201 repaint(); 202 } 203 catch (Exception e) 204 { 205 throw new SVGException("Could not resolve path " + resourcePath, e); 206// svgURI = old; 207 } 208 } 209 210 public boolean isScaleToFit() 211 { 212 return scaleToFit; 213 } 214 215 public void setScaleToFit(boolean scaleToFit) 216 { 217 boolean old = this.scaleToFit; 218 this.scaleToFit = scaleToFit; 219 firePropertyChange("scaleToFit", old, scaleToFit); 220 } 221 222 /** 223 * @return true if antiAliasing is turned on. 224 * @deprecated 225 */ 226 public boolean getUseAntiAlias() 227 { 228 return getAntiAlias(); 229 } 230 231 /** 232 * @param antiAlias true to use antiAliasing. 233 * @deprecated 234 */ 235 public void setUseAntiAlias(boolean antiAlias) 236 { 237 setAntiAlias(antiAlias); 238 } 239 240 /** 241 * @return true if antiAliasing is turned on. 242 */ 243 public boolean getAntiAlias() 244 { 245 return antiAlias; 246 } 247 248 /** 249 * @param antiAlias true to use antiAliasing. 250 */ 251 public void setAntiAlias(boolean antiAlias) 252 { 253 boolean old = this.antiAlias; 254 this.antiAlias = antiAlias; 255 firePropertyChange("antiAlias", old, antiAlias); 256 } 257 258 /** This method is called from within the constructor to 259 * initialize the form. 260 * WARNING: Do NOT modify this code. The content of this method is 261 * always regenerated by the Form Editor. 262 */ 263 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents 264 private void initComponents() 265 { 266 267 setLayout(new java.awt.BorderLayout()); 268 269 } 270 // </editor-fold>//GEN-END:initComponents 271 272 273 // Variables declaration - do not modify//GEN-BEGIN:variables 274 // End of variables declaration//GEN-END:variables 275 276}