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
package sample; import java.util.ArrayList; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ColorPicker; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.stage.Window; public class _shape_dialog extends Stage { public static class _vertex { public double x, y; public _vertex() {} public _vertex(double x, double y) { this.x = x; this.y = y; } } public static class _shape { public boolean b_close; public double width; public Color color, fill_color; public ArrayList<_vertex> vertices; public _shape() { this(false, 1, Color.BLACK, Color.TRANSPARENT); } public _shape(boolean b, double d, Color c, Color fill_c) { b_close = b; width = d; color = c; fill_color = fill_c; vertices = new ArrayList<>(); } } private _shape shape = new _shape(); // test private static final String[] ORDER_COMMANDS = {"最背面へ", "一つ後ろ", "一つ前へ", "最前面へ"}; private CheckBox chk_close; private ComboBox<String> cmb_width; private CheckBox[] chk_transparent = new CheckBox[2]; private ColorPicker[] cp = new ColorPicker[2]; private ArrayList<Button> buttons = new ArrayList<>(); public _shape_dialog(Window owner) { super(StageStyle.UTILITY); VBox vbox = new VBox(); Scene scene = new Scene(vbox, 400, 100); HBox close_pane = new HBox(); chk_close = new CheckBox("閉じた形状"); chk_close.selectedProperty().addListener((ov, prev, curr) -> value_changed()); close_pane.getChildren().add(chk_close); HBox line_pane = new HBox(); Label lbl = new Label("線"); Label lbl_width = new Label("太さ"); ObservableList<String> width_list = FXCollections.observableArrayList("0.5", "1.0", "1.5", "2.0", "2.5", "3.0"); cmb_width = new ComboBox<String>(width_list); cmb_width.setValue("1"); cmb_width.valueProperty().addListener((ov, prev, curr) -> value_changed()); for (int i = 0; i < 2; i++) { chk_transparent[i] = new CheckBox("透明"); chk_transparent[i].selectedProperty().addListener((ov, prev, curr) -> value_changed()); cp[i] = new ColorPicker(); cp[i].valueProperty().addListener((ov, prev, curr) -> value_changed()); } line_pane.getChildren().addAll( lbl, lbl_width, cmb_width, chk_transparent[0], cp[0]); HBox fill_pane = new HBox(); lbl = new Label("塗りつぶし"); fill_pane.getChildren().addAll( lbl, chk_transparent[1], cp[1]); HBox order_pane = new HBox(); Button btn; for (int i = 0; i < 4; i++) { btn = new Button(ORDER_COMMANDS[i]); btn.setOnAction((e) -> { if (order_change_listener == null) return; if (shape == null) return; int index = buttons.indexOf(e.getTarget()); order_change_listener.order_changed(index); }); buttons.add(btn); } order_pane.getChildren().addAll(buttons); vbox.getChildren().addAll(close_pane, line_pane, fill_pane, order_pane); setScene(scene); setResizable(false); } public void set_shape(_shape sh) { shape = null; if (sh == null) return; chk_close.setSelected(sh.b_close); chk_close.setDisable(sh.vertices.size() < 3); cmb_width.setValue(Double.toString(sh.width)); Color[] colors = {sh.color, sh.fill_color}; for (int i = 0; i < 2; i++) { if (colors[i] == Color.TRANSPARENT) { chk_transparent[i].setSelected(true); cp[i].setValue(Color.WHITE); cp[i].setDisable(true); } else { chk_transparent[i].setSelected(false); cp[i].setValue(colors[i]); cp[i].setDisable(false); } } shape = sh; } private void value_changed() { if (shape == null) return; shape.b_close = chk_close.isSelected(); shape.width = Double.parseDouble(cmb_width.getValue()); shape.color = get_color(0); shape.fill_color = get_color(1); if (shape_change_listener != null) shape_change_listener.shape_chaned(); } private Color get_color(int index) { if (chk_transparent[index].isSelected()) { cp[index].setDisable(true); return Color.TRANSPARENT; } else { cp[index].setDisable(false); return cp[index].getValue(); } } public static interface _shape_change_listener { public void shape_chaned(); } private _shape_change_listener shape_change_listener; public void add_shape_change_listener(_shape_change_listener lis) { shape_change_listener = lis; } public static interface _order_change_listener { public void order_changed(int index); } private _order_change_listener order_change_listener; public void add_order_change_listener(_order_change_listener lis) { order_change_listener = lis; } }