001    /*
002     * SVGDisplayPanel.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on February 20, 2004, 12:29 PM
026     */
027    
028    package com.kitfox.svg;
029    
030    import javax.swing.*;
031    import java.awt.*;
032    import java.awt.geom.*;
033    
034    /**
035     * @author Mark McKay
036     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
037     */
038    public class SVGDisplayPanel extends javax.swing.JPanel implements Scrollable
039    {
040        public static final long serialVersionUID = 1;
041        
042        SVGDiagram diagram = null;
043        float scale = 1f;
044        Color bgColor = null;
045    
046        /** Creates new form SVGDisplayPanel */
047        public SVGDisplayPanel()
048        {
049            initComponents();
050        }
051    
052        public SVGDiagram getDiagram()
053        {
054            return diagram;
055        }
056        
057        public void setDiagram(SVGDiagram diagram)
058        {
059            this.diagram = diagram;
060            diagram.setDeviceViewport(getBounds());
061            
062            setDimension();
063        }
064    
065        public void setScale(float scale)
066        {
067            this.scale = scale;
068            setDimension();
069        }
070    
071        public void setBgColor(Color col)
072        {
073            bgColor = col;
074        }
075    
076        private void setDimension()
077        {
078            if (diagram == null)
079            {
080                setPreferredSize(new Dimension(1, 1));
081                revalidate();
082                return;
083            }
084    
085            final Rectangle2D.Float rect = new Rectangle2D.Float();
086            diagram.getViewRect(rect);
087    
088            int w = (int)(rect.width * scale);
089            int h = (int)(rect.height * scale);
090    
091            setPreferredSize(new Dimension(w, h));
092            revalidate();
093        }
094    
095        /**
096         * Update this image to reflect the passed time
097         */
098        public void updateTime(double curTime) throws SVGException
099        {
100            if (diagram == null) return;
101            
102            diagram.updateTime(curTime);
103        }
104        
105        public void paintComponent(Graphics gg)
106        {
107            Graphics2D g = (Graphics2D)gg;
108    
109            if (bgColor != null)
110            {
111                Dimension dim = getSize();
112                g.setColor(bgColor);
113                g.fillRect(0, 0, dim.width, dim.height);
114            }
115    
116            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
117            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
118            if (diagram != null) 
119            {
120                try
121                {
122                    diagram.render(g);
123                }
124                catch (SVGException e)
125                {
126                    e.printStackTrace();
127                }
128            }
129        }
130    
131        /** This method is called from within the constructor to
132         * initialize the form.
133         * WARNING: Do NOT modify this code. The content of this method is
134         * always regenerated by the Form Editor.
135         */
136        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
137        private void initComponents()
138        {
139    
140            setLayout(new java.awt.BorderLayout());
141    
142            addComponentListener(new java.awt.event.ComponentAdapter()
143            {
144                public void componentResized(java.awt.event.ComponentEvent evt)
145                {
146                    formComponentResized(evt);
147                }
148            });
149    
150        }// </editor-fold>//GEN-END:initComponents
151    
152        private void formComponentResized(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentResized
153        {//GEN-HEADEREND:event_formComponentResized
154            if (diagram != null)
155            {
156                diagram.setDeviceViewport(getBounds());
157                setDimension();
158            }
159    
160        }//GEN-LAST:event_formComponentResized
161    
162        public Dimension getPreferredScrollableViewportSize()
163        {
164            return getPreferredSize();
165        }
166    
167        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
168        {
169            if (orientation == SwingConstants.HORIZONTAL)
170            {
171                return visibleRect.width;
172            }
173            else return visibleRect.height;
174        }
175    
176        public boolean getScrollableTracksViewportHeight()
177        {
178            return false;
179        }
180    
181        public boolean getScrollableTracksViewportWidth()
182        {
183            return false;
184        }
185    
186        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
187        {
188            return getScrollableBlockIncrement(visibleRect, orientation, direction) / 16;
189        }
190    
191    
192        // Variables declaration - do not modify//GEN-BEGIN:variables
193        // End of variables declaration//GEN-END:variables
194    
195    }