1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | *
|
---|
34 | * Created on February 20, 2004, 12:29 PM
|
---|
35 | */
|
---|
36 |
|
---|
37 | package com.kitfox.svg;
|
---|
38 |
|
---|
39 | import javax.swing.*;
|
---|
40 | import java.awt.*;
|
---|
41 | import java.awt.geom.*;
|
---|
42 | import java.util.logging.Level;
|
---|
43 | import java.util.logging.Logger;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @author Mark McKay
|
---|
47 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
48 | */
|
---|
49 | public class SVGDisplayPanel extends javax.swing.JPanel implements Scrollable
|
---|
50 | {
|
---|
51 | public static final long serialVersionUID = 1;
|
---|
52 |
|
---|
53 | SVGDiagram diagram = null;
|
---|
54 | float scale = 1f;
|
---|
55 | Color bgColor = null;
|
---|
56 |
|
---|
57 | /** Creates new form SVGDisplayPanel */
|
---|
58 | public SVGDisplayPanel()
|
---|
59 | {
|
---|
60 | initComponents();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public SVGDiagram getDiagram()
|
---|
64 | {
|
---|
65 | return diagram;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void setDiagram(SVGDiagram diagram)
|
---|
69 | {
|
---|
70 | this.diagram = diagram;
|
---|
71 | diagram.setDeviceViewport(getBounds());
|
---|
72 |
|
---|
73 | setDimension();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void setScale(float scale)
|
---|
77 | {
|
---|
78 | this.scale = scale;
|
---|
79 | setDimension();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void setBgColor(Color col)
|
---|
83 | {
|
---|
84 | bgColor = col;
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void setDimension()
|
---|
88 | {
|
---|
89 | if (diagram == null)
|
---|
90 | {
|
---|
91 | setPreferredSize(new Dimension(1, 1));
|
---|
92 | revalidate();
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | final Rectangle2D.Float rect = new Rectangle2D.Float();
|
---|
97 | diagram.getViewRect(rect);
|
---|
98 |
|
---|
99 | 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 | }
|
---|