import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.*; import javafx.scene.paint.Color; import javafx.event.ActionEvent; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * Use this template to create Apps with Graphical User Interfaces. * * @author Dave Slemon */ public class CircleManager extends Application { // TODO: Instance Variables for View Components and Model private Circle circle; private GraphicsContext gc; private TextField radiusBox; private Button radiusButton; // TODO: Private Event Handlers and Helper Methods public void redrawCircle(ActionEvent e) { String str = radiusBox.getText(); double newRadius = Double.parseDouble(str); circle.setRadius(newRadius); drawCircle(); } public void drawCircle() { gc.setFill(Color.LIGHTBLUE); gc.fillRect(0, 0, 400, 200); circle.draw(gc); } /** * This is where you create your components and the model and add event * handlers. * * @param stage The main stage * @throws Exception */ @Override public void start(Stage stage) throws Exception { Pane root = new Pane(); Scene scene = new Scene(root, 400, 400); // set the size here stage.setTitle("Circle Manager"); // set the window title here stage.setScene(scene); // TODO: Add your GUI-building code here scene.getStylesheets().add("myStyles.css"); // 1. Create the model circle = new Circle(50,Color.web("blue"),Color.web("white"),200,100); // 2. Create the GUI components Canvas canvas = new Canvas(400,200); radiusButton = new Button("Set Radius"); radiusBox = new TextField("50"); // 3. Add components to the root root.getChildren().addAll(canvas,radiusButton,radiusBox); // 4. Configure the components (colors, fonts, size, location) radiusBox.relocate(50,250); radiusButton.relocate(200,250); // 5. Add Event Handlers and do final setup gc = canvas.getGraphicsContext2D(); drawCircle(); radiusButton.setOnAction(this::redrawCircle); // 6. Show the stage stage.show(); } /** * Make no changes here. * * @param args unused */ public static void main(String[] args) { launch(args); } }