Guest User

Untitled

a guest
Nov 24th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package code;
  2.  
  3. import javafx.animation.KeyFrame;
  4. import javafx.animation.Timeline;
  5. import javafx.application.Application;
  6. import javafx.geometry.Pos;
  7. import javafx.geometry.Insets;
  8. import javafx.scene.Scene;
  9. import javafx.scene.Node;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.layout.BorderPane;
  13. import javafx.scene.layout.HBox;
  14. import javafx.scene.layout.VBox;
  15. import javafx.scene.layout.Pane;
  16. import javafx.scene.layout.FlowPane;
  17. import javafx.scene.layout.GridPane;
  18. import javafx.scene.paint.Color;
  19. import javafx.scene.shape.Arc;
  20. import javafx.scene.shape.ArcType;
  21. import javafx.scene.shape.Circle;
  22. import javafx.stage.Stage;
  23. import javafx.util.Duration;
  24. import javafx.scene.control.Slider;
  25. import javafx.scene.layout.StackPane;
  26. import javafx.scene.text.*;
  27.  
  28.  
  29. public class FanAuto extends Application {
  30.  
  31. @Override // Override the start method in the Application class
  32. public void start(Stage primaryStage) {
  33. FanPane fan = new FanPane();
  34. StackPane spane = new StackPane();
  35. spane.getChildren().addAll(fan);
  36.  
  37. VBox vBox = new VBox(5);
  38. Button btAuthor = new Button("Author");
  39. Button btDescription = new Button("Description");
  40. Button btResources = new Button("Resources");
  41. Button btProgram = new Button("Program");
  42. vBox.getChildren().addAll(btAuthor, btDescription, btResources, btProgram);
  43.  
  44. HBox hBox = new HBox(5);
  45. Button btPause = new Button("Pause");
  46. Button btResume = new Button("Resume");
  47. Button btReverse = new Button("Reverse");
  48. //hBox.setAlignment(Pos.CENTER_RIGHT);
  49. hBox.getChildren().addAll(btPause, btResume, btReverse);
  50.  
  51. Slider slider = new Slider(0,10, 3);
  52. slider.setShowTickLabels(true);
  53. slider.setShowTickMarks(true);
  54.  
  55. BorderPane pane = new BorderPane();
  56. pane.setCenter(spane);
  57. pane.setTop(hBox);
  58. pane.setRight(vBox);
  59. pane.setBottom(slider);
  60.  
  61. // Create a scene and place it in the stage
  62. Scene scene = new Scene(pane, 300, 275);
  63. primaryStage.setTitle("FanWithControls"); // Set the stage title
  64. primaryStage.setScene(scene); // Place the scene in the stage
  65. primaryStage.show(); // Display the stage
  66.  
  67. Timeline animation = new Timeline(
  68. new KeyFrame(Duration.millis(50), e -> fan.move()));
  69. animation.setCycleCount(Timeline.INDEFINITE);
  70. animation.play(); // Start animation
  71.  
  72. scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
  73. scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));
  74.  
  75. btPause.setOnAction(e -> animation.pause());
  76. btResume.setOnAction(e -> animation.play());
  77. btReverse.setOnAction(e -> fan.reverse());
  78.  
  79. slider.valueProperty().addListener(ov -> animation.setRate(slider.getValue()));
  80. }
  81.  
  82. public static void main(String[] args) {
  83. launch(args);
  84. }
  85. }
  86.  
  87. class FanPane extends Pane {
  88. private double w = 200;
  89. private double h = 200;
  90. private double radius = Math.min(w, h) * 0.45;
  91. private Arc arc[] = new Arc[4];
  92. private double startAngle = 30;
  93. private Circle circle = new Circle(w / 2, h / 2, radius);
  94.  
  95.  
  96. public FanPane() {
  97. circle.setStroke(Color.BLUE);
  98. circle.setFill(Color.WHITE);
  99. circle.setStrokeWidth(4);
  100. getChildren().add(circle);
  101.  
  102. for (int i = 0; i < 4; i++) {
  103. arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35);
  104. arc[i].setFill(Color.RED); // Set fill color
  105. arc[i].setType(ArcType.ROUND);
  106. getChildren().addAll(arc[i]);
  107. }
  108. }
  109.  
  110. private double increment = 5;
  111.  
  112. public void reverse() {
  113. increment = -increment;
  114. }
  115.  
  116. public void move() {
  117. setStartAngle(startAngle + increment);
  118. }
  119.  
  120. public void setStartAngle(double angle) {
  121. startAngle = angle;
  122. setValues();
  123. }
  124.  
  125. public void setValues() {
  126. radius = Math.min(w, h) * 0.45;
  127. circle.setRadius(radius);
  128. circle.setCenterX(w / 2);
  129. circle.setCenterY(h / 2);
  130.  
  131.  
  132. for (int i = 0; i < 4; i++) {
  133. arc[i].setRadiusX(radius * 0.9);
  134. arc[i].setRadiusY(radius * 0.9);
  135. arc[i].setCenterX(w / 2);
  136. arc[i].setCenterY(h / 2);
  137. arc[i].setStartAngle(startAngle + i * 90);
  138. }
  139. }
  140.  
  141. public void setW(double w) {
  142. this.w = w;
  143. setValues();
  144. }
  145.  
  146. public void setH(double h) {
  147. this.h = h;
  148. setValues();
  149. }
  150.  
  151. public double getCenterX() {
  152. return circle.getCenterX();
  153. }
  154.  
  155. public double getCenterY() {
  156. return circle.getCenterY();
  157. }
  158.  
  159. public double getRadius() {
  160. return circle.getRadius();
  161. }
  162. }
Add Comment
Please, Sign In to add comment