Graphic Rendering System 1.0
A Java-based graphic rendering system implementing various design patterns
Loading...
Searching...
No Matches
SvgRenderer.java
Go to the documentation of this file.
1package com.example.graphics.render;
2
3import com.example.graphics.model.Circle;
4import com.example.graphics.model.Line;
5import com.example.graphics.model.Rectangle;
6import com.example.graphics.model.Triangle;
7
12public class SvgRenderer implements Renderer {
13 private StringBuilder svgContent;
14 private final int width;
15 private final int height;
16
22 public SvgRenderer(int width, int height) {
23 this.width = width;
24 this.height = height;
25 clear();
26 }
27
28 @Override
29 public void renderCircle(Circle circle) {
30 svgContent.append(String.format(
31 "<circle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"none\" stroke=\"black\" stroke-width=\"1\" />\n",
32 circle.getX(), circle.getY(), circle.getRadius()
33 ));
34 }
35
36 @Override
37 public void renderRectangle(Rectangle rectangle) {
38 svgContent.append(String.format(
39 "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"none\" stroke=\"black\" stroke-width=\"1\" />\n",
40 rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()
41 ));
42 }
43
44 @Override
45 public void renderLine(Line line) {
46 svgContent.append(String.format(
47 "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"black\" stroke-width=\"1\" />\n",
48 line.getX1(), line.getY1(), line.getX2(), line.getY2()
49 ));
50 }
51
52 @Override
53 public void renderTriangle(Triangle triangle) {
54 svgContent.append(String.format(
55 "<polygon points=\"%d,%d %d,%d %d,%d\" fill=\"none\" stroke=\"black\" stroke-width=\"1\" />\n",
56 triangle.getX1(), triangle.getY1(),
57 triangle.getX2(), triangle.getY2(),
58 triangle.getX3(), triangle.getY3()
59 ));
60 }
61
62 @Override
63 public void clear() {
64 svgContent = new StringBuilder();
65 svgContent.append(String.format(
66 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
67 "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"%d\" height=\"%d\">\n",
69 ));
70 }
71
72 @Override
73 public void display() {
74 svgContent.append("</svg>");
75 System.out.println("SVG Output:");
76 System.out.println(svgContent.toString());
77 }
78
83 public String getSvgContent() {
84 return svgContent.toString() + "</svg>";
85 }
86}
void renderRectangle(Rectangle rectangle)
void renderTriangle(Triangle triangle)