Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/Diagram.java
1462 views
1
package org.newdawn.slick.svg;
2
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
6
/**
7
* A diagram read from SVG containing multiple figures
8
*
9
* @author kevin
10
*/
11
public class Diagram {
12
/** The figures in the diagram */
13
private ArrayList figures = new ArrayList();
14
/** The pattern definitions */
15
private HashMap patterns = new HashMap();
16
/** The linear gradients defined within the diagram */
17
private HashMap gradients = new HashMap();
18
/** The figures mapping */
19
private HashMap figureMap = new HashMap();
20
21
/** The width of the diagram */
22
private float width;
23
/** The height of the diagram */
24
private float height;
25
26
/**
27
* Create a new empty diagram
28
*
29
* @param width The width of the diagram
30
* @param height The height of the diagram
31
*/
32
public Diagram(float width, float height) {
33
this.width = width;
34
this.height = height;
35
}
36
37
/**
38
* Get the width of the diagram
39
*
40
* @return The width of the diagram
41
*/
42
public float getWidth() {
43
return width;
44
}
45
46
/**
47
* Get the height of the diagram
48
*
49
* @return The height of the diagram
50
*/
51
public float getHeight() {
52
return height;
53
}
54
55
/**
56
* Add a pattern definition basd on a image
57
*
58
* @param name The name of the pattern
59
* @param href The href to the image specified in the doc
60
*/
61
public void addPatternDef(String name, String href) {
62
patterns.put(name, href);
63
}
64
65
/**
66
* Add gradient to the diagram
67
*
68
* @param name The name of the gradient
69
* @param gradient The gradient to be added
70
*/
71
public void addGradient(String name, Gradient gradient) {
72
gradients.put(name, gradient);
73
}
74
75
/**
76
* Get a pattern definition from the diagram
77
*
78
* @param name The name of the pattern
79
* @return The href to the image that was specified for the given pattern
80
*/
81
public String getPatternDef(String name) {
82
return (String) patterns.get(name);
83
}
84
85
/**
86
* Get the gradient defined in this document
87
*
88
* @param name The name of the gradient
89
* @return The gradient definition
90
*/
91
public Gradient getGradient(String name) {
92
return (Gradient) gradients.get(name);
93
}
94
95
/**
96
* Get the names of the patterns defined
97
*
98
* @return The names of the pattern
99
*/
100
public String[] getPatternDefNames() {
101
return (String[]) patterns.keySet().toArray(new String[0]);
102
}
103
104
/**
105
* Get a figure by a given ID
106
*
107
* @param id The ID of the figure
108
* @return The figure with the given ID
109
*/
110
public Figure getFigureByID(String id) {
111
return (Figure) figureMap.get(id);
112
}
113
114
/**
115
* Add a figure to the diagram
116
*
117
* @param figure The figure to add
118
*/
119
public void addFigure(Figure figure) {
120
figures.add(figure);
121
figureMap.put(figure.getData().getAttribute(NonGeometricData.ID), figure);
122
123
String fillRef = figure.getData().getAsReference(NonGeometricData.FILL);
124
Gradient gradient = getGradient(fillRef);
125
if (gradient != null) {
126
if (gradient.isRadial()) {
127
for (int i=0;i<InkscapeLoader.RADIAL_TRIANGULATION_LEVEL;i++) {
128
figure.getShape().increaseTriangulation();
129
}
130
}
131
}
132
}
133
134
/**
135
* Get the number of figures in the diagram
136
*
137
* @return The number of figures in the diagram
138
*/
139
public int getFigureCount() {
140
return figures.size();
141
}
142
143
/**
144
* Get the figure at a given index
145
*
146
* @param index The index of the figure to retrieve
147
* @return The figure at the given index
148
*/
149
public Figure getFigure(int index) {
150
return (Figure) figures.get(index);
151
}
152
153
/**
154
* Remove a figure from the diagram
155
*
156
* @param figure The figure to be removed
157
*/
158
public void removeFigure(Figure figure) {
159
figures.remove(figure);
160
figureMap.remove(figure.getData().getAttribute("id"));
161
}
162
}
163
164