| 1 |
/* |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
contributor license agreements. See the NOTICE file distributed with |
| 5 |
this work for additional information regarding copyright ownership. |
| 6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
(the "License"); you may not use this file except in compliance with |
| 8 |
the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
*/ |
| 19 |
import java.net.URL; |
| 20 |
|
| 21 |
import javax.swing.JApplet; |
| 22 |
|
| 23 |
import org.apache.batik.dom.svg.SAXSVGDocumentFactory; |
| 24 |
import org.apache.batik.swing.JSVGCanvas; |
| 25 |
import org.apache.batik.util.XMLResourceDescriptor; |
| 26 |
import org.w3c.dom.Document; |
| 27 |
import org.w3c.dom.Element; |
| 28 |
import org.w3c.dom.Node; |
| 29 |
|
| 30 |
/** |
| 31 |
* A applet demonstrating the JSVGCanvas. |
| 32 |
* |
| 33 |
* @version $Id$ |
| 34 |
*/ |
| 35 |
public class AppletDemo extends JApplet { |
| 36 |
|
| 37 |
protected JSVGCanvas canvas; |
| 38 |
|
| 39 |
protected Document doc; |
| 40 |
|
| 41 |
protected Element svg; |
| 42 |
|
| 43 |
public void init() { |
| 44 |
// Create a new JSVGCanvas. |
| 45 |
canvas = new JSVGCanvas(); |
| 46 |
getContentPane().add(canvas); |
| 47 |
|
| 48 |
try { |
| 49 |
// Parse the barChart.svg file into a Document. |
| 50 |
String parser = XMLResourceDescriptor.getXMLParserClassName(); |
| 51 |
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); |
| 52 |
URL url = new URL(getCodeBase(), "barChart.svg"); |
| 53 |
doc = f.createDocument(url.toString()); |
| 54 |
|
| 55 |
svg = doc.getDocumentElement(); |
| 56 |
|
| 57 |
// Change the document viewBox. |
| 58 |
svg.setAttributeNS(null, "viewBox", "40 95 370 265"); |
| 59 |
|
| 60 |
// Make the text look nice. |
| 61 |
svg.setAttributeNS(null, "text-rendering", "geometricPrecision"); |
| 62 |
|
| 63 |
// Remove the xml-stylesheet PI. |
| 64 |
for (Node n = svg.getPreviousSibling(); |
| 65 |
n != null; |
| 66 |
n = n.getPreviousSibling()) { |
| 67 |
if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { |
| 68 |
doc.removeChild(n); |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Remove the Batik sample mark 'use' element. |
| 74 |
for (Node n = svg.getLastChild(); |
| 75 |
n != null; |
| 76 |
n = n.getPreviousSibling()) { |
| 77 |
if (n.getNodeType() == Node.ELEMENT_NODE |
| 78 |
&& n.getLocalName().equals("use")) { |
| 79 |
svg.removeChild(n); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} catch (Exception ex) { |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public void start() { |
| 88 |
// Display the document. |
| 89 |
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); |
| 90 |
canvas.setDocument(doc); |
| 91 |
} |
| 92 |
|
| 93 |
public void stop() { |
| 94 |
// Remove the document. |
| 95 |
canvas.setDocument(null); |
| 96 |
} |
| 97 |
|
| 98 |
public void destroy() { |
| 99 |
canvas.dispose(); |
| 100 |
} |
| 101 |
|
| 102 |
public void updateBar(final String name, final float value) { |
| 103 |
canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater |
| 104 |
(new Runnable() { |
| 105 |
public void run() { |
| 106 |
Element bar = doc.getElementById(name); |
| 107 |
if (bar == null) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
Node n; |
| 112 |
Element path1, path2, path3; |
| 113 |
for (n = bar.getFirstChild(); |
| 114 |
n.getNodeType() != Node.ELEMENT_NODE; |
| 115 |
n = n.getNextSibling()) { |
| 116 |
} |
| 117 |
path1 = (Element) n; |
| 118 |
for (n = n.getNextSibling(); |
| 119 |
n.getNodeType() != Node.ELEMENT_NODE; |
| 120 |
n = n.getNextSibling()) { |
| 121 |
} |
| 122 |
path2 = (Element) n; |
| 123 |
for (n = n.getNextSibling(); |
| 124 |
n.getNodeType() != Node.ELEMENT_NODE; |
| 125 |
n = n.getNextSibling()) { |
| 126 |
} |
| 127 |
path3 = (Element) n; |
| 128 |
|
| 129 |
int offset; |
| 130 |
if (name.equals("ShoeBar")) { |
| 131 |
offset = 0; |
| 132 |
} else if (name.equals("CarBar")) { |
| 133 |
offset = 79; |
| 134 |
} else if (name.equals("TravelBar")) { |
| 135 |
offset = 158; |
| 136 |
} else { |
| 137 |
offset = 237; |
| 138 |
} |
| 139 |
|
| 140 |
String d = |
| 141 |
"M " + (offset + 86) + ",240 v -" + (3.7 * value) + " l 15,-15 v " + (3.7 * value) + " l -15,15 z"; |
| 142 |
path1.setAttributeNS(null, "d", d); |
| 143 |
d = "M " + (offset + 86) + "," + (240 - 3.7 * value) + " h -39 l 15,-15 h 39 l -15,15 z"; |
| 144 |
path2.setAttributeNS(null, "d", d); |
| 145 |
d = "M " + (offset + 47) + "," + (240 - 3.7 * value) + " v " + (3.7 * value) + " h 39 v -" + (3.7 * value) + " h -39 z"; |
| 146 |
path3.setAttributeNS(null, "d", d); |
| 147 |
} |
| 148 |
}); |
| 149 |
} |
| 150 |
} |