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 February 20, 2004, 12:29 PM 035 */ 036 037package com.kitfox.svg; 038 039import javax.swing.*; 040import java.awt.*; 041import java.awt.geom.*; 042import java.util.logging.Level; 043import java.util.logging.Logger; 044 045/** 046 * @author Mark McKay 047 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 048 */ 049public class SVGDisplayPanel extends javax.swing.JPanel implements Scrollable 050{ 051 public static final long serialVersionUID = 1; 052 053 SVGDiagram diagram = null; 054 float scale = 1f; 055 Color bgColor = null; 056 057 /** Creates new form SVGDisplayPanel */ 058 public SVGDisplayPanel() 059 { 060 initComponents(); 061 } 062 063 public SVGDiagram getDiagram() 064 { 065 return diagram; 066 } 067 068 public void setDiagram(SVGDiagram diagram) 069 { 070 this.diagram = diagram; 071 diagram.setDeviceViewport(getBounds()); 072 073 setDimension(); 074 } 075 076 public void setScale(float scale) 077 { 078 this.scale = scale; 079 setDimension(); 080 } 081 082 public void setBgColor(Color col) 083 { 084 bgColor = col; 085 } 086 087 private void setDimension() 088 { 089 if (diagram == null) 090 { 091 setPreferredSize(new Dimension(1, 1)); 092 revalidate(); 093 return; 094 } 095 096 final Rectangle2D.Float rect = new Rectangle2D.Float(); 097 diagram.getViewRect(rect); 098 099 int w = (int)(rect.width * scale); 100 int h = (int)(rect.height * scale); 101 102 setPreferredSize(new Dimension(w, h)); 103 revalidate(); 104 } 105 106 /** 107 * Update this image to reflect the passed time 108 */ 109 public void updateTime(double curTime) throws SVGException 110 { 111 if (diagram == null) return; 112 113 diagram.updateTime(curTime); 114 } 115 116 public void paintComponent(Graphics gg) 117 { 118 Graphics2D g = (Graphics2D)gg; 119 120 if (bgColor != null) 121 { 122 Dimension dim = getSize(); 123 g.setColor(bgColor); 124 g.fillRect(0, 0, dim.width, dim.height); 125 } 126 127 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 128 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 129 if (diagram != null) 130 { 131 try 132 { 133 diagram.render(g); 134 } 135 catch (SVGException e) 136 { 137 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 138 "Could not render diagram", e); 139 } 140 } 141 } 142 143 /** This method is called from within the constructor to 144 * initialize the form. 145 * WARNING: Do NOT modify this code. The content of this method is 146 * always regenerated by the Form Editor. 147 */ 148 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents 149 private void initComponents() 150 { 151 152 setLayout(new java.awt.BorderLayout()); 153 154 addComponentListener(new java.awt.event.ComponentAdapter() 155 { 156 public void componentResized(java.awt.event.ComponentEvent evt) 157 { 158 formComponentResized(evt); 159 } 160 }); 161 162 }// </editor-fold>//GEN-END:initComponents 163 164 private void formComponentResized(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentResized 165 {//GEN-HEADEREND:event_formComponentResized 166 if (diagram != null) 167 { 168 diagram.setDeviceViewport(getBounds()); 169 setDimension(); 170 } 171 172 }//GEN-LAST:event_formComponentResized 173 174 public Dimension getPreferredScrollableViewportSize() 175 { 176 return getPreferredSize(); 177 } 178 179 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) 180 { 181 if (orientation == SwingConstants.HORIZONTAL) 182 { 183 return visibleRect.width; 184 } 185 else return visibleRect.height; 186 } 187 188 public boolean getScrollableTracksViewportHeight() 189 { 190 return false; 191 } 192 193 public boolean getScrollableTracksViewportWidth() 194 { 195 return false; 196 } 197 198 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) 199 { 200 return getScrollableBlockIncrement(visibleRect, orientation, direction) / 16; 201 } 202 203 204 // Variables declaration - do not modify//GEN-BEGIN:variables 205 // End of variables declaration//GEN-END:variables 206 207}