|
Size: 11973
Comment:
|
← Revision 6 as of 2012-06-22 14:07:43 ⇥
Size: 11979
Comment: display java code with syntax highlighting
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 3: | Line 3: |
| {{{ | {{{#!java |
A full working example of an SVG About box
1 /*
2 555 Timer Design Tool
3 Copyright (C) 2000-2005 Andrew J. Armstrong
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Author:
20 Andrew J. Armstrong <andrew_armstrong(ad)unwired(dod)com(dod)au>
21
22 Created using IntelliJ IDEA 4.5 <www.jetbrains.com>
23 */
24 package au.com.hpo.c555.panel;
25
26 import org.apache.batik.bridge.ExternalResourceSecurity;
27 import org.apache.batik.bridge.ScriptSecurity;
28 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
29 import org.apache.batik.swing.JSVGCanvas;
30 import org.apache.batik.swing.svg.LinkActivationEvent;
31 import org.apache.batik.swing.svg.LinkActivationListener;
32 import org.apache.batik.swing.svg.SVGUserAgent;
33 import org.apache.batik.util.ParsedURL;
34 import org.apache.batik.util.XMLResourceDescriptor;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.svg.SVGDocument;
37
38 import javax.swing.*;
39 import java.awt.*;
40 import java.awt.event.WindowAdapter;
41 import java.awt.event.WindowEvent;
42 import java.io.IOException;
43 import java.io.StringReader;
44 import java.net.URL;
45
46 /**
47 * Displays an about box using inline SVG
48 */
49 public class About extends JSVGCanvas
50 {
51 private static final boolean EVENTS_ENABLED = true;
52 private static final boolean SELECTABLE_TEXT = true;
53
54 private static final String LOGO = "555 Timer";
55 private static final String DESIGN_TOOL = "Design Tool";
56 private static final String VERSION = "Version " + Version.getVersion();
57 private static final String AUTHOR = "Copyright © 1999-2005 Andrew J. Armstrong";
58 private static final String AUTHOR_EMAIL = "andrew_armstrong(ad)unwired(dod)com(dod)au";
59
60 private static final String ABOUT_BOX_INFO =
61 "<?xml version='1.0' standalone='no'?>"
62 + "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>"
63 + "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='450' height='500' viewBox='0 0 450 500'>"
64 + "<rect x='0' y='0' width='100%' height='100%' style='fill:white' />"
65 + "<defs>"
66
67 + "<filter id='dropShadow' filterUnits='objectBoundingBox' width='1.4' height='1.4'>"
68 + "<feGaussianBlur in='SourceAlpha' stdDeviation='4' />"
69 + "<feOffset dx='4' dy='4' />"
70 + "<feComponentTransfer result='shadow'>"
71 + "<feFuncA type='linear' slope='.5' intercept='0' />"
72 + "</feComponentTransfer>"
73 + "</filter>"
74
75 + "<filter id='emboss' >"
76 + "<feGaussianBlur in='SourceAlpha' stdDeviation='2' result='blur'/>"
77 + "<feSpecularLighting in='blur' surfaceScale='-3' style='lighting-color:white'"
78 + " specularConstant='1' specularExponent='16'"
79 + " result='spec' kernelUnitLength='1' >"
80 + "<feDistantLight azimuth='45' elevation='45' />"
81 + "</feSpecularLighting>"
82 + "<feComposite in='spec' in2='SourceGraphic' operator='in' result='specOut'/>"
83 + "</filter>"
84 + "</defs>"
85 + getEmbossedString(72, 50, 35, "172,20,20", LOGO)
86 + getEmbossedString(48, 50, 48, "172,20,20", DESIGN_TOOL)
87 + getEmbossedString(32, 50, 60, "0,40,220", VERSION)
88 + getString(14, 50, 75, "0,40,220", AUTHOR)
89 + getString(14, 50, 80, "0,40,220", AUTHOR_EMAIL)
90 + "</svg>"
91 ;
92
93 private static String getEmbossedString(int size, int x, int y, String color, String text)
94 {
95 return
96 "<g style='font-size:" + size + "; font-family:Dialog; text-anchor:middle;'>"
97 + "<text x='" + x + "%' y='" + y + "%' style='filter:url(#dropShadow)'>" + text + "</text>"
98 + "<text x='" + x + "%' y='" + y + "%' style='fill:rgb(" + color + ")'>" + text + "</text>"
99 + "<text x='" + x + "%' y='" + y + "%' style='filter:url(#emboss)'>" + text + "</text>"
100 + "</g>";
101 }
102
103 private static String getString(int size, int x, int y, String color, String text)
104 {
105 return
106 "<g style='font-size:" + size + "; font-family:Dialog; text-anchor:middle;'>"
107 + "<a xlink:href='mailto:" + AUTHOR_EMAIL + "'>"
108 + "<text x='" + x + "%' y='" + y + "%' style='fill:rgb(" + color + ")'>"
109 + text
110 + "</text>"
111 + "</a>"
112 + " </g>";
113 }
114
115 public About()
116 {
117
118 super(new SVGUserAgent()
119 {
120
121 public void displayError(Exception e)
122 {
123 }
124
125 public void displayError(String s)
126 {
127 }
128
129 public void displayMessage(String s)
130 {
131 }
132
133 public String getLanguages()
134 {
135 return "en";
136 }
137
138 public float getPixelToMM()
139 {
140 return 25.4f / 96; // = 0.264583333333333333333f; // 96 dpi
141 }
142
143 /**
144 * Returns the user stylesheet uri.
145 *
146 * @return null if no user style sheet was specified.
147 */
148 public String getUserStyleSheetURI()
149 {
150 return null;
151 }
152
153 public String getXMLParserClassName()
154 {
155 String parserName = "org.apache.xerces.parsers.SAXParser";
156 System.out.println("About.getXMLParserClassName " + parserName);
157 return parserName;
158 }
159
160 public void handleElement(Element element, Object o)
161 {
162 }
163
164 public void openLink(String uri, boolean activateInNewComponent)
165 {
166 }
167
168 public boolean supportExtension(String s)
169 {
170 return false;
171 }
172
173 public boolean isXMLParserValidating()
174 {
175 return false;
176 }
177
178 /**
179 * Returns this user agent's CSS media.
180 */
181 public String getMedia()
182 {
183 return "screen";
184 }
185
186 /**
187 * Shows an alert dialog box.
188 */
189 public void showAlert(String message)
190 {
191 }
192
193 /**
194 * Returns this user agent's alternate style-sheet title.
195 */
196 public String getAlternateStyleSheet()
197 {
198 return null;
199 }
200
201 /**
202 * Shows a confirm dialog box.
203 */
204 public boolean showConfirm(String message)
205 {
206 return false;
207 }
208
209 /**
210 * Shows a prompt dialog box.
211 */
212 public String showPrompt(String message)
213 {
214 return null;
215 }
216
217 /**
218 * Shows a prompt dialog box.
219 */
220 public String showPrompt(String message, String defaultValue)
221 {
222 return null;
223 }
224
225 public ScriptSecurity getScriptSecurity(String scriptType,
226 URL scriptURL,
227 URL docURL)
228 {
229 return null;
230 }
231
232 public void checkLoadExternalResource(ParsedURL resourceURL,
233 ParsedURL docURL) throws SecurityException
234 {
235 }
236
237 public void checkLoadScript(String scriptType,
238 ParsedURL scriptURL,
239 ParsedURL docURL) throws SecurityException
240 {
241 }
242
243 public float getMediumFontSize()
244 {
245 return 0;
246 }
247
248 public float getBolderFontWeight(float f)
249 {
250 return 0;
251 }
252
253 public float getLighterFontWeight(float f)
254 {
255 return 0;
256 }
257
258 public ExternalResourceSecurity
259 getExternalResourceSecurity(ParsedURL resourceURL,
260 ParsedURL docURL)
261 {
262 return null;
263 }
264
265 public float getPixelUnitToMillimeter()
266 {
267 return 0;
268 }
269
270 public ScriptSecurity getScriptSecurity(String scriptType,
271 ParsedURL scriptURL,
272 ParsedURL docURL)
273 {
274 return null;
275 }
276
277 public String getDefaultFontFamily()
278 {
279 return null;
280 }
281
282 }, EVENTS_ENABLED, SELECTABLE_TEXT);
283
284 this.isDynamic();
285 setEnableImageZoomInteractor(false);
286 setEnableResetTransformInteractor(false);
287 setEnableZoomInteractor(false);
288 this.addLinkActivationListener(new LinkActivationListener()
289 {
290 public void linkActivated(LinkActivationEvent e)
291 {
292 try
293 {
294 if (isWindowsPlatform())
295 {
296 String[] commandLine = {"rundll32", "url.dll,FileProtocolHandler", e.getReferencedURI()};
297 Runtime.getRuntime().exec(commandLine);
298 }
299 }
300 catch (IOException ioe)
301 {
302 System.out.println(ioe);
303 }
304
305 }
306 });
307
308 try
309 {
310 String parser = XMLResourceDescriptor.getXMLParserClassName();
311 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
312 String uri = "http:internal";
313 SVGDocument doc = (SVGDocument) f.createDocument(uri, new StringReader(ABOUT_BOX_INFO));
314 setSVGDocument(doc);
315 }
316 catch (IOException e)
317 {
318 System.out.println("e = " + e);
319 }
320 }
321
322 private boolean isWindowsPlatform()
323 {
324 String os = System.getProperty("os.name");
325 return os != null && os.startsWith("Windows");
326
327 }
328
329 public static void main(String[] args)
330 {
331 JFrame frame = new JFrame("About");
332 frame.addWindowListener
333 (new WindowAdapter()
334 {
335 public void windowClosing(WindowEvent e)
336 {
337 System.exit(0);
338 }
339 });
340
341 About aboutBox = new About();
342 frame.getContentPane().add(aboutBox);
343 frame.pack();
344 Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
345 Dimension dimFrame = frame.getSize();
346 frame.setLocation((dimScreen.width - dimFrame.width) / 2,
347 (dimScreen.height - dimFrame.height) / 2);
348 frame.show();
349 }
350
351 public Dimension getPreferredSize()
352 {
353 return new Dimension(450, 500);
354 }
355
356 }
357
358 class Version
359 {
360 private static final String VERSION = "1.1.20020612";
361
362 private Version()
363 {
364 }
365
366 public static String getVersion()
367 {
368 return VERSION;
369 }
370 }