import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.RadioButton; import javafx.scene.control.Slider; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; public class PaintApp extends Application { // constants private static final int APP_WIDTH = 800; private static final int APP_HEIGHT = 650; private static final int CANVAS_HEIGHT = 600; // GUI variables private BorderPane mainPane; private FlowPane controlPanel; private MyCanvas canvas; private Button bnClear; private Slider slSize; // stroke size private ComboBox cbStrokeType; private RadioButton rbFilled, rbHollow; // non-GUI variables private boolean isFilled = true; @Override public void start(Stage stage) { mainPane = new BorderPane(); setupControls(); setupCanvas(); Scene scene = new Scene(mainPane, APP_WIDTH, APP_HEIGHT); stage.setTitle("CS112 PaintApp FX"); stage.setScene(scene); stage.show(); } private void setupControls() { // Q. flow pane vs. hbox? controlPanel = new FlowPane(); mainPane.setTop(controlPanel); // (1) size slSize = new Slider(1, 10, 4); // (2) type // (3) filled or not rbFilled = new RadioButton("Filled"); rbHollow = new RadioButton("Outline"); rbFilled.setSelected(true); // default option // mutually-exclusive ToggleGroup tgFilled = new ToggleGroup(); tgFilled.getToggles().addAll(rbFilled, rbHollow); // rbFilled.setToggleGroup(tgFilled); // rbFilled.setOnAction(e -> { // if (rbFilled.isSelected()) { // isFilled = true; // } // else { // isFilled = false; // } // isFilled = rbFilled.isSelected(); // }); // rbHollow.setOnAction(e -> { // if (rbHollow.isSelected()) { // isFilled = false; // } // else { // isFilled = true; // } // }); // (4) color // (5) clear canvas bnClear = new Button("Clear Canvas"); // listener bnClear.setOnAction(e -> { canvas.clearCanvas(); }); controlPanel.getChildren().addAll(slSize, rbFilled, rbHollow, bnClear); } private void setupCanvas() { canvas = new MyCanvas(APP_WIDTH, CANVAS_HEIGHT); mainPane.setCenter(canvas); // listener: MouseDragged canvas.setOnMouseDragged(e -> { // 1. get user's mouse location from e (of MouseEvent type) int mx = (int) e.getX(); int my = (int) e.getY(); // 2. create one CircularStroke at location from (1) Stroke s = new CircularStroke(mx, my, (int) slSize.getValue(), rbFilled.isSelected()); // 3. add the stroke from (2) to canvas canvas.addStroke(s); }); } public static void main(String[] args) { launch(args); } }