Skip to content

Commit b58a6e0

Browse files
committed
Adding more methods to the Drawer Tool
1 parent c428fcd commit b58a6e0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/java/org/fxapps/llmfx/tools/graphics/JFXDrawerTool.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ You can draw using the following commands separated by line break (\n):
4444
fillRect x y w h
4545
circle cx cy r
4646
fillCircle cx cy r
47+
ellipse cx cy rw rh
48+
fillEllipse cx cy rw rh
4749
line x1 y1 x2 y2
50+
polyline x1 y1 x2 y2 … xn yn
51+
polygon x1 y1 x2 y2 … xn yn
52+
fillPolygon x1 y1 x2 y2 … xn yn
4853
text x y text
4954
color r g b
5055
width w
@@ -66,13 +71,27 @@ public void draw(String dsl) {
6671
.mapToObj(i -> tokens[i])
6772
.filter(v -> v != null && !v.isBlank())
6873
.mapToDouble(Double::parseDouble)
69-
.toArray();
74+
.toArray();
7075
switch (command) {
7176
case "rect" -> gc.strokeRect(params[0], params[1], params[2], params[3]);
7277
case "circle" -> gc.strokeOval(params[0], params[1], params[2], params[2]);
7378
case "fillRect" -> gc.fillRect(params[0], params[1], params[2], params[3]);
7479
case "fillCircle" -> gc.fillOval(params[0], params[1], params[2], params[2]);
80+
case "ellipse" -> gc.strokeOval(params[0], params[1], params[2], params[3]);
81+
case "fillEllipse" -> gc.fillOval(params[0], params[1], params[2], params[3]);
7582
case "line" -> gc.strokeLine(params[0], params[1], params[2], params[3]);
83+
case "polyline" -> {
84+
var points = extractPoints(params);
85+
gc.strokePolyline(points[0], points[1], points[0].length);
86+
}
87+
case "polygon" -> {
88+
var points = extractPoints(params);
89+
gc.strokePolygon(points[0], points[1], points[0].length);
90+
}
91+
case "fillPolygon" -> {
92+
var points = extractPoints(params);
93+
gc.fillPolygon(points[0], points[1], points[0].length);
94+
}
7695
case "width" -> gc.setLineWidth(params[0]);
7796
case "text" -> gc.fillText(text, params[0], params[1]);
7897
case "background" -> {
@@ -91,4 +110,14 @@ public void draw(String dsl) {
91110
}
92111
}
93112

113+
double [][] extractPoints(double[] params) {
114+
double[] xPoints = new double[params.length / 2];
115+
double[] yPoints = new double[params.length / 2];
116+
for (int i = 0; i < params.length; i += 2) {
117+
xPoints[i / 2] = params[i];
118+
yPoints[i / 2] = params[i + 1];
119+
}
120+
return new double[][] { xPoints, yPoints };
121+
}
122+
94123
}

src/test/java/org/fxapps/llmfx/tools/graphics/JFXDrawerToolTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public void testDSL() {
3232
width 1
3333
line 1 2 3 4
3434
text 1 2 "abc"
35+
polyline 1 2 3 4 5 6
36+
polygon 1 2 3 4 5 6
37+
fillPolygon 1 2 3 4 5 6
3538
""");
3639
var color = Color.rgb(255, 200, 10);
3740
var bg = Color.rgb(0, 10, 20);
@@ -51,6 +54,9 @@ public void testDSL() {
5154
verify(tool.gc).setLineWidth(eq(1.0d));
5255
verify(tool.gc).strokeLine(eq(1.0d), eq(2.0d), eq(3.0d), eq(4.0d));
5356
verify(tool.gc).fillText(eq("abc"), eq(1.0d), eq(2.0d));
57+
verify(tool.gc).strokePolyline(any(double[].class), any(double[].class), eq(3));
58+
verify(tool.gc).strokePolygon(any(double[].class), any(double[].class), eq(3));
59+
verify(tool.gc).fillPolygon(any(double[].class), any(double[].class), eq(3));
5460
}
5561

5662
}

0 commit comments

Comments
 (0)