Skip to content

Commit 7519ce1

Browse files
Merge pull request #327 from SoulNikhar/newmain
svg img generator
2 parents 4f3dc0c + 4c7ea1a commit 7519ce1

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.io.File;
2+
import java.io.FileWriter;
3+
import java.io.IOException;
4+
import org.apache.batik.dom.GenericDOMImplementation;
5+
import org.apache.batik.svggen.SVGGraphics2D;
6+
import org.w3c.dom.DOMImplementation;
7+
import org.w3c.dom.Document;
8+
9+
class TreeNode {
10+
int data;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
public TreeNode(int data) {
15+
this.data = data;
16+
this.left = null;
17+
this.right = null;
18+
}
19+
20+
public void insert(int data) {
21+
if (data < this.data) {
22+
if (this.left == null) {
23+
this.left = new TreeNode(data);
24+
} else {
25+
this.left.insert(data);
26+
}
27+
} else {
28+
if (this.right == null) {
29+
this.right = new TreeNode(data);
30+
} else {
31+
this.right.insert(data);
32+
}
33+
}
34+
}
35+
}
36+
37+
public class BinaryTreeSVGExporter {
38+
public static void export(TreeNode root, String filename) throws IOException {
39+
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
40+
Document doc = domImpl.createDocument(null, "svg", null);
41+
SVGGraphics2D svg = new SVGGraphics2D(doc);
42+
43+
// Traverse the tree and draw the nodes and edges
44+
traverse(root, svg, 100, 50, 50);
45+
46+
// Write the SVG file
47+
File file = new File(filename);
48+
FileWriter fw = new FileWriter(file);
49+
svg.stream(fw, true);
50+
}
51+
52+
private static void traverse(TreeNode node, SVGGraphics2D svg, int x, int y, int dx) {
53+
if (node == null) {
54+
return;
55+
}
56+
57+
// Draw the node
58+
svg.drawOval(x - 10, y - 10, 20, 20);
59+
svg.drawString(String.valueOf(node.data), x - 4, y + 4);
60+
61+
// Draw the left child and edge
62+
if (node.left != null) {
63+
svg.drawLine(x, y, x - dx, y + 50);
64+
traverse(node.left, svg, x - dx, y + 50, dx / 2);
65+
}
66+
67+
// Draw the right child and edge
68+
if (node.right != null) {
69+
svg.drawLine(x, y, x + dx, y + 50);
70+
traverse(node.right, svg, x + dx, y + 50, dx / 2);
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
TreeNode root = new TreeNode(10);
76+
root.insert(5);
77+
root.insert(15);
78+
root.insert(3);
79+
root.insert(7);
80+
root.insert(13);
81+
root.insert(17);
82+
83+
try {
84+
BinaryTreeSVGExporter.export(root, "binary_tree.svg");
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
}

Svg File Generator/SvgExample.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.*;
2+
import org.apache.batik.dom.*;
3+
import org.apache.batik.dom.svg.*;
4+
import org.apache.batik.svggen.*;
5+
6+
public class SvgExample {
7+
public static void main(String[] args) throws IOException {
8+
// Create a new SVG document
9+
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
10+
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
11+
Document doc = impl.createDocument(svgNS, "svg", null);
12+
13+
// Set the width and height of the document
14+
SVGOMSVGElement svgRoot = (SVGOMSVGElement)doc.getDocumentElement();
15+
svgRoot.setAttributeNS(null, "width", "200");
16+
svgRoot.setAttributeNS(null, "height", "200");
17+
18+
// Create a circle element
19+
SVGOMCircleElement circle = new SVGOMCircleElement() {
20+
protected AttributeInitializer getAttributeInitializer() {
21+
return new AttributeInitializer() {
22+
public void initialize(Element elt) {
23+
super.initialize(elt);
24+
elt.setAttributeNS(null, "cx", "100");
25+
elt.setAttributeNS(null, "cy", "100");
26+
elt.setAttributeNS(null, "r", "50");
27+
}
28+
};
29+
}
30+
};
31+
32+
// Add the circle to the document
33+
svgRoot.appendChild(circle);
34+
35+
// Write the SVG document to a file
36+
Writer out = new OutputStreamWriter(new FileOutputStream("example.svg"), "UTF-8");
37+
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
38+
SVGGraphics2D g = new SVGGraphics2D(ctx, false);
39+
g.setSVGCanvasSize(new Dimension(200, 200));
40+
g.getRoot(svgRoot);
41+
g.stream(out, true);
42+
}
43+
}

0 commit comments

Comments
 (0)