1  import javafx.application.Application;
  2  import javafx.geometry.Pos;
  3  import javafx.scene.Scene;
  4  import javafx.scene.control.Button;
  5  import javafx.scene.input.KeyCode;
  6  import javafx.scene.input.MouseButton;
  7  import javafx.scene.layout.HBox;
  8  import javafx.scene.layout.BorderPane;
  9  import javafx.stage.Stage;
 10  
 11  public class ControlCircleWithMouseAndKey extends Application {
 12    private CirclePane circlePane = new CirclePane();
 13  
 14    @Override // Override the start method in the Application class
 15    public void start(Stage primaryStage) {
 16      // Hold two buttons in an HBox
 17      HBox hBox = new HBox();
 18      hBox.setSpacing(10);
 19      hBox.setAlignment(Pos.CENTER);
 20      Button btEnlarge = new Button("Enlarge");
 21      Button btShrink = new Button("Shrink");
 22      hBox.getChildren().add(btEnlarge);
 23      hBox.getChildren().add(btShrink);
 24      
 25      // Create and register the handler
 26      btEnlarge.setOnAction(e -> circlePane.enlarge());
 27      btShrink.setOnAction(e -> circlePane.shrink());
 28      
 29      BorderPane borderPane = new BorderPane();
 30      borderPane.setCenter(circlePane);
 31      borderPane.setBottom(hBox);
 32      BorderPane.setAlignment(hBox, Pos.CENTER);
 33      
 34      // Create a scene and place it in the stage
 35      Scene scene = new Scene(borderPane, 200, 150);
 36      primaryStage.setTitle("ControlCircle"); // Set the stage title
 37      primaryStage.setScene(scene); // Place the scene in the stage
 38      primaryStage.show(); // Display the stage
 39      
 40      circlePane.setOnMouseClicked(e -> {
 41        if (e.getButton() == MouseButton.PRIMARY) {
 42          circlePane.enlarge();
 43        }
 44        else if (e.getButton() == MouseButton.SECONDARY) {
 45          circlePane.shrink();
 46        }
 47      });
 48      
 49      scene.setOnKeyPressed(e -> {
 50        if (e.getCode() == KeyCode.UP) {
 51          circlePane.enlarge();
 52        }
 53        else if (e.getCode() == KeyCode.DOWN) {
 54          circlePane.shrink();
 55        }
 56      });
 57    }
 58    
 59    /**
 60     * The main method is only needed for the IDE with limited
 61     * JavaFX support. Not needed for running from the command line.
 62     */
 63    public static void main(String[] args) {
 64      launch(args);
 65    }
 66  }