@@ -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}
0 commit comments