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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
package sample; import sample._shape_dialog._shape; import sample._shape_dialog._vertex; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import javafx.scene.input.KeyCode; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.FileChooser; import javafx.stage.Stage; public class _drawing extends Application { public static void main(String... args) { Application.launch(args); } private final int VIEW_W = 400, VIEW_H = 300; private GraphicsContext gc0, gc1, gc2; private KeyCode key = null; private boolean b_ctrl, b_shift; private double curr_x, curr_y; private static final int NO_SELECT = 1, SHAPE_SELECT = 2, VERTEX_SELECT = 3, ADD_SHAPE = 4, ADD_VERTEX = 5; private static final double LEN_SEL_VERTEX = 3, LEN_SEL_SHAPE = 3, LEN_DRAG = 3; private int state = NO_SELECT; private ArrayList<_shape> shapes = new ArrayList<>(); private _shape sel_shape; private _vertex sel_vertex, new_vertex, near_vertex; private _vertex prev_vertex, next_vertex; private int sel_index; private Image image; private _shape_dialog dlg; public void start(Stage stage) { BorderPane pane = new BorderPane(); Scene scene = new Scene(pane); stage.setScene(scene); StackPane layer = new StackPane(); Canvas view0 = new Canvas(VIEW_W, VIEW_H); Canvas view1 = new Canvas(VIEW_W, VIEW_H); Canvas view2 = new Canvas(VIEW_W, VIEW_H); gc0 = view0.getGraphicsContext2D(); gc1 = view1.getGraphicsContext2D(); gc2 = view2.getGraphicsContext2D(); layer.getChildren().addAll(view2, view1, view0); pane.setCenter(layer); dlg = new _shape_dialog(stage); dlg.add_shape_change_listener(() -> draw()); dlg.add_order_change_listener((index) -> { switch (index) { case 0: index = -shapes.size(); break; case 1: index = -1; break; case 2: index = 1; break; case 3: index = shapes.size(); break; } int prev_index = shapes.indexOf(sel_shape); index += prev_index; index = Math.min(Math.max(index, 0), shapes.size() - 1); if (index == prev_index) return; shapes.remove(prev_index); shapes.add(index, sel_shape); draw(); }); FileChooser file_chooser = new FileChooser(); scene.setOnKeyPressed((e) -> { int index = 0; double k; key = e.getCode(); switch (key) { case CONTROL: b_ctrl = true; break; case SHIFT: b_shift = true; break; case SPACE: preview(true); break; case N: if (!b_ctrl) return; if (state == ADD_SHAPE || state == ADD_VERTEX) return; select_shape(null); new_vertex = new _vertex(curr_x, curr_y); sel_shape = new _shape(); sel_shape.vertices.add(new_vertex); shapes.add(sel_shape); prev_vertex = next_vertex = null; state = ADD_SHAPE; break; case ENTER: if (state == NO_SELECT || state == ADD_VERTEX) return; if (state == SHAPE_SELECT || state == VERTEX_SELECT) { select_vertex(null); select_shape(null); return; } if (sel_shape.vertices.size() < (b_shift ? 4 : 3)) return; sel_shape.vertices.remove(new_vertex); if (b_shift) sel_shape.b_close = true; select_shape(sel_shape); break; case ESCAPE: if (state == VERTEX_SELECT) select_vertex(null); break; case LEFT: index++; case DOWN: index++; case RIGHT: index++; case UP: k = (b_shift ? 0.125 : 1); if (state != VERTEX_SELECT) return; if (index % 2 == 1) { sel_vertex.x -= k * (index - 2); if (!b_shift) sel_vertex.x = Math.round(sel_vertex.x); } else { sel_vertex.y += k * (index - 1); if (!b_shift) sel_vertex.y = Math.round(sel_vertex.y); } draw(); break; case BACK_SPACE: if (state != VERTEX_SELECT) return; sel_shape.vertices.remove(sel_index); int count = sel_shape.vertices.size(); if (count == 1) { shapes.remove(sel_shape); select_shape(null); return; } if (count == 2) sel_shape.b_close = false; select_shape(sel_shape); break; case A: if (!b_ctrl) return; if (state != VERTEX_SELECT) return; new_vertex = new _vertex(curr_x, curr_y); prev_vertex = sel_shape.vertices.get(sel_index); next_vertex = sel_shape.vertices.get( (sel_index + 1) % sel_shape.vertices.size()); sel_shape.vertices.add(sel_index + 1, new_vertex); select_vertex(null); state = ADD_VERTEX; break; case TAB: index = (b_shift) ? -1 : 1; if (state == NO_SELECT || state == SHAPE_SELECT) { index += shapes.indexOf(sel_shape);; if (index < 0) index = shapes.size() - 1; else if (index >= shapes.size()) index = 0; select_shape(shapes.get(index)); } else if (state == VERTEX_SELECT) { index += sel_index; index = Math.min(Math.max(index, 0), sel_shape.vertices.size() - 1); if (index != sel_index) { sel_index = index; sel_vertex = sel_shape.vertices.get(sel_index); draw(); } } break; case I: index++; if (!b_ctrl) { draw2((b_shift) ? null : image); return; } case O: index++; case S: if (!b_ctrl) return; b_ctrl = false; if (index == 2 && b_shift) { gc2.clearRect(0, 0, VIEW_W, VIEW_H); return; } File file; if (index == 0) file = file_chooser.showSaveDialog(stage); else file = file_chooser.showOpenDialog(stage); if (file == null) return; file_chooser.setInitialDirectory(file.getParentFile()); if (index == 2) { image = new Image(file.toURI().toString()); if (image != null) draw2(image); } else if (index == 0) save_data(file); else open_data(file); break; case D: if (!b_ctrl) return; b_ctrl = false; if (!dlg.isShowing()) { dlg.showAndWait(); if (state == SHAPE_SELECT || state == VERTEX_SELECT) dlg.set_shape(sel_shape); } } }); scene.setOnKeyReleased((e) -> { key = e.getCode(); switch (key) { case CONTROL: b_ctrl = false; break; case SHIFT: b_shift = false; break; case SPACE: preview(false); break; } }); scene.setOnMouseClicked((e) -> { if (is_preview()) { preview(false); return; } if (state == SHAPE_SELECT || state == VERTEX_SELECT) { int prev_state = state; select_vertex(curr_x, curr_y); if (prev_state == VERTEX_SELECT) return; if (state == VERTEX_SELECT) return; } if (state == NO_SELECT || state == SHAPE_SELECT) { select_shape(curr_x, curr_y); return; } if (b_shift) { if (near_vertex == null) return; int index = sel_shape.vertices.indexOf(new_vertex); sel_shape.vertices.set(index, near_vertex); } if (state == ADD_SHAPE) { prev_vertex = new_vertex; new_vertex = new _vertex(curr_x, curr_y); sel_shape.vertices.add(new_vertex); draw(); return; } select_shape(sel_shape); }); scene.setOnMouseDragged((e) -> { double x = e.getX(), y = e.getY(); if (state == VERTEX_SELECT) { if (len(sel_vertex, curr_x, curr_y) < LEN_DRAG) { sel_vertex.x = x; sel_vertex.y = y; draw(); } } curr_x = x; curr_y = y; }); scene.setOnMouseMoved((e) -> { double x = e.getX(), y = e.getY(); if (state == ADD_SHAPE || state == ADD_VERTEX) { new_vertex.x = x; new_vertex.y = y; if (b_shift) near_vertex = search_vertex(x, y); else near_vertex = null; draw(); } curr_x = x; curr_y = y; }); draw2(null); stage.show(); } private void draw0() { gc0.clearRect(0, 0, VIEW_W, VIEW_H); if (b_preview || sel_shape == null) return; draw_shape(sel_shape, gc0); gc0.setLineWidth(1); gc0.setStroke(Color.BLACK); for (_vertex v : sel_shape.vertices) { if (v == new_vertex) continue; draw_vertex(v, (v == sel_vertex) ? Color.BLACK : Color.WHITE, gc0); } if (near_vertex != null) draw_vertex(near_vertex, Color.GRAY, gc0); } private void draw_vertex(_vertex v, Color color, GraphicsContext gc) { gc.setFill(color); gc.fillRect(v.x - 1.5, v.y - 1, 3, 3); gc.strokeRect(v.x - 2, v.y - 2, 4, 4); } private void draw1() { gc1.clearRect(0, 0, VIEW_W, VIEW_H); for (_shape shape : shapes) { if (!b_preview && shape == sel_shape) continue; draw_shape(shape, gc1); } } private void draw2(Image img) { gc2.setFill(Color.WHITE); gc2.fillRect(0, 0, VIEW_W, VIEW_H); if (img == null) return; double left, top, x, y, img_x, img_y; img_x = img.getWidth(); img_y = img.getHeight(); if (img_x / img_y > VIEW_W / VIEW_H) { x = VIEW_W; y = img_y * VIEW_W / img_x; } else { x = img_x * VIEW_H / img_y; y = VIEW_H; } left = (VIEW_W - x) / 2; top = (VIEW_H - y) / 2; gc2.drawImage(img, 0, 0, img_x, img_y, left, top, x, y); } private void draw_shape(_shape shape, GraphicsContext gc) { gc.setLineWidth(shape.width); gc.setStroke(shape.color); boolean b_first = true; for (_vertex v : shape.vertices) { if (b_first) { b_first = false; gc.beginPath(); gc.moveTo(v.x, v.y); } else { gc.lineTo(v.x, v.y); } } if (shape.b_close) gc.closePath(); if (shape.vertices.size() >= 3) { gc.setFill(shape.fill_color); gc.fill(); } gc.stroke(); } private void select_vertex(double x, double y) { if (sel_shape == null) return; _vertex min_v = null; double min_len = LEN_SEL_VERTEX, len; for (_vertex v : sel_shape.vertices) { len = len(v, x, y); if (len >= min_len) continue; min_len = len; min_v = v; } select_vertex(min_v); } private void select_vertex(_vertex v) { sel_vertex = v; sel_index = sel_shape.vertices.indexOf(v); state = (v == null) ? SHAPE_SELECT : VERTEX_SELECT; draw(); } private _vertex search_vertex(double x, double y) { _vertex min_v = null; double min_len = LEN_SEL_VERTEX, len; for (_shape shape : shapes) { for (_vertex v : shape.vertices) { if (v == new_vertex || v == prev_vertex || v == next_vertex) continue; len = len(v, x, y); if (len >= min_len) continue; min_len = len; min_v = v; } } return min_v; } private void select_shape(double x, double y) { if (shapes.size() == 0) return; _shape min_shape = null; double min_len = LEN_SEL_SHAPE, len; _vertex prev_v; int last; for (_shape shape : shapes) { if (shape.b_close) { last = shape.vertices.size() - 1; prev_v = shape.vertices.get(last); } else { prev_v = null; } for (_vertex v : shape.vertices) { if (prev_v == null) { prev_v = v; continue; } len = len(prev_v, v, x, y); prev_v = v; if (len >= min_len) continue; min_len = len; min_shape = shape; } } select_shape(min_shape); } private void select_shape(_shape shape) { sel_shape = shape; state = (shape == null) ? NO_SELECT : SHAPE_SELECT; sel_vertex = new_vertex = near_vertex = null; draw1(); draw0(); dlg.set_shape(shape); } private double len(_vertex v, double x, double y) { double dx = v.x - x, dy = v.y - y; return Math.sqrt(dx * dx + dy * dy); } private double len(_vertex v1, _vertex v2, double x, double y) { double dx = v2.x - v1.x, dy = v2.y - v1.y; double len1, len2, len; len = Math.sqrt(dx * dx + dy * dy); len = Math.max(len, 10e-9); dx /= len; dy /= len; len1 = dx * v1.x + dy * v1.y; len2 = dx * v2.x + dy * v2.y; len = dx * x + dy * y; if (len < len1 || len > len2) return LEN_SEL_SHAPE; len1 = dy * v1.x - dx * v1.y; len = dy * x - dx * y; return Math.abs(len - len1); } private void save_data(File file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { bw.write("shapes"); bw.newLine(); ArrayList<_vertex> vertices = new ArrayList<>(); int vertex_count; int count, index; String s; count = 0; for (_shape shape : shapes) { vertex_count = shape.vertices.size(); if (vertex_count == 2) { s = String.format(" line %s %s", double_data(shape.width), shape.color.toString()); } else { s = String.format(" %s %s %s %s", (shape.b_close) ? "poly" : "path", double_data(shape.width), shape.color.toString(), shape.fill_color.toString()); } bw.write(++count + s); for (_vertex v : shape.vertices) { index = vertices.indexOf(v); if (index == -1) { index = vertices.size(); vertices.add(v); } bw.write(" " + (index + 1)); } bw.newLine(); } bw.write("vertices"); bw.newLine(); count = 0; for (_vertex v : vertices) { s = String.format("%d %s %s", ++count, double_data(v.x), double_data(v.y)); bw.write(s); bw.newLine(); } bw.close(); } catch (Exception e) { System.err.println(e.getMessage()); } } private String double_data(double x) { String s = String.format("%.3f", x); int last; char c; for (last = s.length() - 1; (c = s.charAt(last)) == '0'; last--) ; if (c != '.') last++; return s.substring(0, last); } private static final int READ_NONE = 0, READ_SHAPE = 1, READ_VEREX = 2; private void open_data(File file) { int line_index = 1; try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; String[] a; int state = READ_NONE; int index, count = 0, start; _shape shape; ArrayList<_vertex> vertices = new ArrayList<>(); _vertex v; shapes.clear(); for (; (line = br.readLine()) != null; line_index++) { a = line.split(" "); if (a.length == 1) { switch (a[0]) { case "shapes": state = READ_SHAPE; break; case "vertices": state = READ_VEREX; break; default: throw new Exception("認識できないカテゴリ"); } continue; } if (state == READ_SHAPE) { shape = new _shape(); start = 4; shape.fill_color = Color.TRANSPARENT; switch (a[1]) { case "poly": shape.b_close = true; case "path": start++; shape.fill_color = Color.valueOf(a[4]); case "line": shape.width = Double.parseDouble(a[2]); shape.color = Color.valueOf(a[3]); break; default: throw new Exception("認識できない形状"); } for (int i = start; i < a.length; i++) { index = Integer.parseInt(a[i]); while (vertices.size() < index) vertices.add(new _vertex()); shape.vertices.add(vertices.get(index - 1)); } shapes.add(shape); } else if (state == READ_VEREX) { v = vertices.get(count++); v.x = Double.parseDouble(a[1]); v.y = Double.parseDouble(a[2]); } } br.close(); select_shape(null); } catch (Exception e) { System.err.println(line_index + ": " + e.getMessage()); } } private boolean b_preview; private void preview(boolean b) { if (b == b_preview) return; if (b && state != SHAPE_SELECT && state != VERTEX_SELECT) return; b_preview = b; draw1(); draw0(); } private boolean is_preview() { return b_preview; } private void draw() { if (b_preview) draw1(); else draw0(); } }