-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathRectangleComponent.java
More file actions
148 lines (136 loc) · 4.55 KB
/
Copy pathRectangleComponent.java
File metadata and controls
148 lines (136 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package write;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.xml.parsers.*;
import javax.xml.stream.*;
import org.w3c.dom.*;
/**
* A component that shows a set of colored rectangles
*/
public class RectangleComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private java.util.List<Rectangle2D> rects;
private java.util.List<Color> colors;
private Random generator;
private DocumentBuilder builder;
public RectangleComponent()
{
rects = new ArrayList<>();
colors = new ArrayList<>();
generator = new Random();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}
/**
* Create a new random drawing.
*/
public void newDrawing()
{
int n = 10 + generator.nextInt(20);
rects.clear();
colors.clear();
for (int i = 1; i <= n; i++)
{
int x = generator.nextInt(getWidth());
int y = generator.nextInt(getHeight());
int width = generator.nextInt(getWidth() - x);
int height = generator.nextInt(getHeight() - y);
rects.add(new Rectangle(x, y, width, height));
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
colors.add(new Color(r, g, b));
}
repaint();
}
public void paintComponent(Graphics g)
{
if (rects.size() == 0) newDrawing();
Graphics2D g2 = (Graphics2D) g;
// draw all rectangles
for (int i = 0; i < rects.size(); i++)
{
g2.setPaint(colors.get(i));
g2.fill(rects.get(i));
}
}
/**
* Creates an SVG document of the current drawing.
* @return the DOM tree of the SVG document
*/
public Document buildDocument()
{
String namespace = "http://www.w3.org/2000/svg";
Document doc = builder.newDocument();
Element svgElement = doc.createElementNS(namespace, "svg");
doc.appendChild(svgElement);
svgElement.setAttribute("width", "" + getWidth());
svgElement.setAttribute("height", "" + getHeight());
for (int i = 0; i < rects.size(); i++)
{
Color c = colors.get(i);
Rectangle2D r = rects.get(i);
Element rectElement = doc.createElementNS(namespace, "rect");
rectElement.setAttribute("x", "" + r.getX());
rectElement.setAttribute("y", "" + r.getY());
rectElement.setAttribute("width", "" + r.getWidth());
rectElement.setAttribute("height", "" + r.getHeight());
rectElement.setAttribute("fill", colorToString(c));
svgElement.appendChild(rectElement);
}
return doc;
}
/**
* Writes an SVG document of the current drawing.
* @param writer the document destination
*/
public void writeDocument(XMLStreamWriter writer) throws XMLStreamException
{
writer.writeStartDocument();
writer.writeDTD("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20000802//EN\" "
+ "\"http://www.w3.org/TR/2000/CR-SVG-20000802/DTD/svg-20000802.dtd\">");
writer.writeStartElement("svg");
writer.writeDefaultNamespace("http://www.w3.org/2000/svg");
writer.writeAttribute("width", "" + getWidth());
writer.writeAttribute("height", "" + getHeight());
for (int i = 0; i < rects.size(); i++)
{
Color c = colors.get(i);
Rectangle2D r = rects.get(i);
writer.writeEmptyElement("rect");
writer.writeAttribute("x", "" + r.getX());
writer.writeAttribute("y", "" + r.getY());
writer.writeAttribute("width", "" + r.getWidth());
writer.writeAttribute("height", "" + r.getHeight());
writer.writeAttribute("fill", colorToString(c));
}
writer.writeEndDocument(); // closes svg element
}
/**
* Converts a color to a hex value.
* @param c a color
* @return a string of the form #rrggbb
*/
private static String colorToString(Color c)
{
StringBuffer buffer = new StringBuffer();
buffer.append(Integer.toHexString(c.getRGB() & 0xFFFFFF));
while (buffer.length() < 6)
buffer.insert(0, '0');
buffer.insert(0, '#');
return buffer.toString();
}
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}