Monty374

JFX 7.5

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