Advertisement
jill744

Circle Pane

Dec 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. public static void main(String[] args) {
  2. launch(args);
  3. }
  4. }
  5.  
  6.  
  7. import javafx.application.Application;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.input.KeyCode;
  12. import javafx.scene.input.MouseButton;
  13. import javafx.scene.layout.HBox;
  14. import javafx.scene.layout.BorderPane;
  15. import javafx.stage.Stage;
  16.  
  17. public class ControlCircleWithMouseAndKey extends Application {
  18. private CirclePane circlePane = new CirclePane();
  19.  
  20. @Override // Override the start method in the Application class
  21. public void start(Stage primaryStage) {
  22. // Hold two buttons in an HBox
  23. HBox hBox = new HBox();
  24. hBox.setSpacing(10);
  25. hBox.setAlignment(Pos.CENTER);
  26. Button btEnlarge = new Button("Enlarge");
  27. Button btShrink = new Button("Shrink");
  28. hBox.getChildren().add(btEnlarge);
  29. hBox.getChildren().add(btShrink);
  30.  
  31. // Create and register the handler
  32. btEnlarge.setOnAction(e -> circlePane.enlarge());
  33. btShrink.setOnAction(e -> circlePane.shrink());
  34.  
  35. BorderPane borderPane = new BorderPane();
  36. borderPane.setCenter(circlePane);
  37. borderPane.setBottom(hBox);
  38. BorderPane.setAlignment(hBox, Pos.CENTER);
  39.  
  40. // Create a scene and place it in the stage
  41. Scene scene = new Scene(borderPane, 200, 150);
  42. primaryStage.setTitle("ControlCircle"); // Set the stage title
  43. primaryStage.setScene(scene); // Place the scene in the stage
  44. primaryStage.show(); // Display the stage
  45.  
  46. circlePane.setOnMouseClicked(e -> {
  47. if (e.getButton() == MouseButton.PRIMARY) {
  48. circlePane.enlarge();
  49. }
  50. else if (e.getButton() == MouseButton.SECONDARY) {
  51. circlePane.shrink();
  52. }
  53. });
  54.  
  55. scene.setOnKeyPressed(e -> {
  56. if (e.getCode() == KeyCode.UP) {
  57. circlePane.enlarge();
  58. }
  59. else if (e.getCode() == KeyCode.DOWN) {
  60. circlePane.shrink();
  61. }
  62. });
  63. }
  64. public void start(Stage primaryStage) {
  65. // Hold two buttons in an HBox
  66. HBox hBox = new HBox();
  67. hBox.setSpacing(10);
  68. hBox.setAlignment(Pos.CENTER);
  69. Button btEnlarge = new Button("Enlarge");
  70. Button btShrink = new Button("Shrink");
  71. hBox.getChildren().add(btEnlarge);
  72. hBox.getChildren().add(btShrink);
  73.  
  74. // Create and register the handler
  75. btEnlarge.setOnAction(new EnlargeHandler());
  76.  
  77. BorderPane borderPane = new BorderPane();
  78. borderPane.setCenter(circlePane);
  79. borderPane.setBottom(hBox);
  80. BorderPane.setAlignment(hBox, Pos.CENTER);
  81.  
  82. // Create a scene and place it in the stage
  83. Scene scene = new Scene(borderPane, 200, 150);
  84. primaryStage.setTitle("ControlCircle"); // Set the stage title
  85. primaryStage.setScene(scene); // Place the scene in the stage
  86. primaryStage.show(); // Display the stage
  87. }
  88.  
  89. class EnlargeHandler implements EventHandler<ActionEvent> {
  90. @Override // Override the handle method
  91. public void handle(ActionEvent e) {
  92. circlePane.enlarge();
  93. }
  94. }
  95.  
  96. /**
  97. * The main method is only needed for the IDE with limited
  98. * JavaFX support. Not needed for running from the command line.
  99. */
  100. public static void main(String[] args) {
  101. launch(args);
  102. }
  103. }
  104.  
  105. class CirclePane extends StackPane {
  106. private Circle circle = new Circle(50);
  107.  
  108. public CirclePane() {
  109. getChildren().add(circle);
  110. circle.setStroke(Color.BLACK);
  111. circle.setFill(Color.WHITE);
  112. }
  113.  
  114. public void enlarge() {
  115. circle.setRadius(circle.getRadius() + 2);
  116. }
  117.  
  118. public void shrink() {
  119. circle.setRadius(circle.getRadius() > 2 ?
  120. circle.getRadius() - 2 : circle.getRadius());
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement