source: josm/trunk/src/com/kitfox/svg/SVGDisplayPanel.java@ 5369

Last change on this file since 5369 was 4256, checked in by bastiK, 13 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

File size: 5.3 KB
Line 
1/*
2 * SVGDisplayPanel.java
3 *
4 *
5 * The Salamander Project - 2D and 3D graphics libraries in Java
6 * Copyright (C) 2004 Mark McKay
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
23 * projects can be found at http://www.kitfox.com
24 *
25 * Created on February 20, 2004, 12:29 PM
26 */
27
28package com.kitfox.svg;
29
30import javax.swing.*;
31import java.awt.*;
32import java.awt.geom.*;
33
34/**
35 * @author Mark McKay
36 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
37 */
38public class SVGDisplayPanel extends javax.swing.JPanel implements Scrollable
39{
40 public static final long serialVersionUID = 1;
41
42 SVGDiagram diagram = null;
43 float scale = 1f;
44 Color bgColor = null;
45
46 /** Creates new form SVGDisplayPanel */
47 public SVGDisplayPanel()
48 {
49 initComponents();
50 }
51
52 public SVGDiagram getDiagram()
53 {
54 return diagram;
55 }
56
57 public void setDiagram(SVGDiagram diagram)
58 {
59 this.diagram = diagram;
60 diagram.setDeviceViewport(getBounds());
61
62 setDimension();
63 }
64
65 public void setScale(float scale)
66 {
67 this.scale = scale;
68 setDimension();
69 }
70
71 public void setBgColor(Color col)
72 {
73 bgColor = col;
74 }
75
76 private void setDimension()
77 {
78 if (diagram == null)
79 {
80 setPreferredSize(new Dimension(1, 1));
81 revalidate();
82 return;
83 }
84
85 final Rectangle2D.Float rect = new Rectangle2D.Float();
86 diagram.getViewRect(rect);
87
88 int w = (int)(rect.width * scale);
89 int h = (int)(rect.height * scale);
90
91 setPreferredSize(new Dimension(w, h));
92 revalidate();
93 }
94
95 /**
96 * Update this image to reflect the passed time
97 */
98 public void updateTime(double curTime) throws SVGException
99 {
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}
Note: See TracBrowser for help on using the repository browser.