JavaFX intro

Create a new JavaFX project

After setting up a new JavaFX project, You should see something similar to this

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }

Creating a panel with canvas and text area

public void start(Stage stage) throws IOException {
        stage.setTitle("My Window");

        // we will be using a panel
        BorderPane myPanel = new BorderPane();

        // set up the canvas
        Canvas myCanvas = new Canvas(600, 400);
        GraphicsContext gc = myCanvas.getGraphicsContext2D();

        // set up text area
        TextArea myTextArea = new TextArea();
        myTextArea.setPrefHeight(200);
        myTextArea.setEditable(false);

        // place canvas and text area in the panel
        myPanel.setCenter(myCanvas);
        myPanel.setBottom(myTextArea);
        
        scene = new Scene(myPanel, 640, 480);
        stage.setScene(scene);
        stage.show();
    }

Adding text to the text area

myTextArea.appendText("hi there");

Adding shapes to canvas

Color c = Color.web("94300f");
gc.setFill(c);
gc.fillRect(10, 10, 10, 10);

Canvas tutorial

Working with Canvas

Adding text entry

TextField userEntry = new TextField();
userEntry.setPromptText("Enter your command:");
userEntry.getText();

Add text entry to top of the border panel

myPanel.setTop(userEntry.setPromptText);

Adding a panel in the panel

GridPane myGrid = new GridPane();
myGrid.setPadding(new Insets(10, 10, 10, 10));
myPanel.setTop(myGrid);

And then

myGrid.getChildren().add(userEntry);

Adding a button

Button submit = new Button("Submit");
GridPane.setConstraints(submit, 1, 0);
myGrid.getChildren().add(submit);

Adding an event to a button

submit.setOnAction((actionEvent) -> {
    myTextArea.appendText(userEntry.getText());
});

Quiz 6

current time