Skip to content

Commit 024f0ca

Browse files
committed
Improve SVG icon rendering
1 parent 517faf3 commit 024f0ca

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed

src/main/java/edu/rpi/legup/ui/HomePanel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ private void initButtons() {
156156
};
157157
URL button1IconLocation =
158158
ClassLoader.getSystemClassLoader()
159-
.getResource("edu/rpi/legup/images/Legup/homepanel/new_puzzle_file.png");
160-
ImageIcon button1Icon = new ImageIcon(button1IconLocation);
159+
.getResource("edu/rpi/legup/images/Legup/homepanel/new_puzzle_file.svg");
160+
SVGImage button1Icon = new SVGImage(button1IconLocation);
161161
this.buttons[1].setFocusPainted(false);
162-
this.buttons[1].setIcon(resizeButtonIcon(button1Icon, this.buttonSize, this.buttonSize));
162+
this.buttons[1].setIcon(button1Icon.getIcon(this.buttonSize, this.buttonSize));
163163
this.buttons[1].setHorizontalTextPosition(AbstractButton.CENTER);
164164
this.buttons[1].setVerticalTextPosition(AbstractButton.BOTTOM);
165165
this.buttons[1].addActionListener(l -> this.openPuzzleEditorDialog());

src/main/java/edu/rpi/legup/utility/SVGImage.java

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import java.awt.Stroke;
1414
import java.awt.geom.AffineTransform;
1515
import java.awt.geom.Area;
16+
import java.awt.geom.Ellipse2D;
17+
import java.awt.geom.Line2D;
1618
import java.awt.geom.Path2D;
19+
import java.awt.geom.Point2D;
1720
import java.awt.geom.RoundRectangle2D;
1821
import java.io.IOException;
1922
import java.net.URL;
@@ -106,25 +109,116 @@ public ElementProcessor setValue(ElementProcessor value) {
106109
case "M" -> {
107110
path.moveTo(scanner.nextFloat(), scanner.nextFloat());
108111
}
112+
case "m" -> {
113+
Point2D current = path.getCurrentPoint();
114+
double x = scanner.nextFloat() + current.getX();
115+
double y = scanner.nextFloat() + current.getY();
116+
path.moveTo(x, y);
117+
}
109118
case "Q" -> {
110119
path.quadTo(scanner.nextFloat(), scanner.nextFloat(),
111120
scanner.nextFloat(), scanner.nextFloat());
112121
}
122+
case "q" -> {
123+
Point2D current = path.getCurrentPoint();
124+
double x1 = scanner.nextFloat() + current.getX();
125+
double y1 = scanner.nextFloat() + current.getY();
126+
double x2 = scanner.nextFloat() + current.getX();
127+
double y2 = scanner.nextFloat() + current.getY();
128+
path.quadTo(x1, y1, x2, y2);
129+
}
130+
case "C" -> {
131+
path.curveTo(scanner.nextFloat(), scanner.nextFloat(),
132+
scanner.nextFloat(), scanner.nextFloat(),
133+
scanner.nextFloat(), scanner.nextFloat());
134+
}
135+
case "c" -> {
136+
Point2D current = path.getCurrentPoint();
137+
double x1 = scanner.nextFloat() + current.getX();
138+
double y1 = scanner.nextFloat() + current.getY();
139+
double x2 = scanner.nextFloat() + current.getX();
140+
double y2 = scanner.nextFloat() + current.getY();
141+
double x3 = scanner.nextFloat() + current.getX();
142+
double y3 = scanner.nextFloat() + current.getY();
143+
path.curveTo(x1, y1, x2, y2, x3, y3);
144+
}
113145
case "L" -> {
114146
path.lineTo(scanner.nextFloat(), scanner.nextFloat());
115147
}
148+
case "l" -> {
149+
Point2D current = path.getCurrentPoint();
150+
double x = scanner.nextFloat() + current.getX();
151+
double y = scanner.nextFloat() + current.getY();
152+
path.lineTo(x, y);
153+
}
116154
case "V" -> {
117155
path.lineTo(path.getCurrentPoint().getX(), scanner.nextFloat());
118156
}
157+
case "v" -> {
158+
Point2D current = path.getCurrentPoint();
159+
path.lineTo(current.getX(), scanner.nextFloat() + current.getY());
160+
}
119161
case "H" -> {
120162
path.lineTo(scanner.nextFloat(), path.getCurrentPoint().getY());
121163
}
122-
case "Z" -> {
164+
case "h" -> {
165+
Point2D current = path.getCurrentPoint();
166+
path.lineTo(scanner.nextFloat() + current.getX(), current.getY());
167+
}
168+
case "Z", "z" -> {
123169
path.closePath();
124170
}
171+
case "S", "s", "T", "t" -> {
172+
System.err.println("Curve extension not supported");
173+
}
174+
case "A", "a" -> {
175+
System.err.println("Arcs not supported");
176+
}
177+
default -> {
178+
System.err.println("Operation not supported");
179+
}
125180
}
126181
}
127182
return path;
183+
}),
184+
new ElementProcessor("polyline", (Element node) -> {
185+
Path2D path = new Path2D.Float();
186+
Scanner scanner = new Scanner(node.getAttribute("points"));
187+
path.moveTo(scanner.nextFloat(), scanner.nextFloat());
188+
while(scanner.hasNext()) {
189+
path.lineTo(scanner.nextFloat(), scanner.nextFloat());
190+
}
191+
return path;
192+
}),
193+
new ElementProcessor("polygon", (Element node) -> {
194+
Path2D path = new Path2D.Float();
195+
Scanner scanner = new Scanner(node.getAttribute("points"));
196+
path.moveTo(scanner.nextFloat(), scanner.nextFloat());
197+
while(scanner.hasNext()) {
198+
path.lineTo(scanner.nextFloat(), scanner.nextFloat());
199+
}
200+
path.closePath();
201+
return path;
202+
}),
203+
new ElementProcessor("circle", (Element node) -> {
204+
float x = Float.parseFloat(node.getAttribute("cx"));
205+
float y = Float.parseFloat(node.getAttribute("cy"));
206+
float r = Float.parseFloat(node.getAttribute("r"));
207+
return new Ellipse2D.Float(x - r, y - r, r * 2, r * 2);
208+
}),
209+
new ElementProcessor("ellipse", (Element node) -> {
210+
float x = Float.parseFloat(node.getAttribute("cx"));
211+
float y = Float.parseFloat(node.getAttribute("cy"));
212+
float rx = Float.parseFloat(node.getAttribute("rx"));
213+
float ry = Float.parseFloat(node.getAttribute("ry"));
214+
return new Ellipse2D.Float(x - rx, y - ry, rx * 2, ry * 2);
215+
}),
216+
new ElementProcessor("line", (Element node) -> {
217+
float x1 = Float.parseFloat(node.getAttribute("x1"));
218+
float y1 = Float.parseFloat(node.getAttribute("y1"));
219+
float x2 = Float.parseFloat(node.getAttribute("x2"));
220+
float y2 = Float.parseFloat(node.getAttribute("y2"));
221+
return new Line2D.Float(x1, y1, x2, y2);
128222
})
129223
);
130224
public SVGImage(URL url) {
@@ -148,7 +242,7 @@ public SVGImage(URL url) {
148242
}
149243
}
150244
} catch (ParserConfigurationException | SAXException | IOException e) {
151-
System.out.println(e);
245+
System.err.println(e);
152246
}
153247
}
154248
public Icon getIcon(int width, int height) {
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Loading
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)