Advertisement
RazorBlade57

Untitled

Mar 26th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. Write a program that displays a circle of a radius 10 pixels filled with a random color at a random location. When you click the circle, it moves to another random location. After ten circles are clicked, display the time spent.
  2.  
  3. Create 10 pixels at X and Y
  4.  
  5. Crate Random Color
  6.  
  7. Add Circle to Panere
  8. Set counter to 0
  9.  
  10. Set Start Time (System.currenttimeMillis())
  11.  
  12. EventHandler for the circle when clicked (setonmouseclicked) for testing setOnMouseEntered
  13.  
  14. if counter <10
  15. Pick a random X (Use math.random * getWidth() from pane)
  16.  
  17. Pick a random Y (Use math.random * getHeight() from pane)
  18.  
  19. Create random color
  20.  
  21. Fill The circle, setCenterX, setCenterY
  22.  
  23. Add one to counter
  24.  
  25. Else
  26.  
  27. Clear The pane
  28.  
  29. set end time
  30.  
  31. disolay how long - use add Text - do not display in console
  32.  
  33. End If
  34.  
  35.  
  36.  
  37.  
  38. -----------------------------------------------------------------------------------------------
  39. import javafx.animation.PathTransition;
  40. import javafx.animation.Timeline;
  41. import javafx.application.Application;
  42. import javafx.scene.Scene;
  43. import javafx.scene.layout.Pane;
  44. import javafx.scene.paint.Color;
  45. import javafx.scene.shape.Rectangle;
  46. import javafx.scene.shape.Circle;
  47. import javafx.stage.Stage;
  48. import javafx.util.Duration;
  49.  
  50. public class PathTransitionDemo extends Application {
  51. @Override // Override the start method in the Application class
  52. public void start(Stage primaryStage) {
  53. // Create a pane
  54. Pane pane = new Pane();
  55.  
  56. // Create a rectangle
  57. Rectangle rectangle = new Rectangle (0, 0, 25, 50);
  58. Rectangle rectangle2 = new Rectangle (0, 0, 25, 50);
  59. Rectangle rectangle3 = new Rectangle (0, 0, 25, 50);
  60. Color rand = new Color(Math.random(), Math.random(), Math.random(), 1);
  61. rectangle.setFill(rand);
  62. rectangle2.setFill(rand);
  63. rectangle3.setFill(rand);
  64. // Create a circle
  65. Circle circle = new Circle(125, 100, 50);
  66. circle.setFill(Color.WHITE);
  67. circle.setStroke(Color.BLACK);
  68.  
  69. // Add circle and rectangle to the pane
  70. pane.getChildren().add(circle);
  71. pane.getChildren().add(rectangle);
  72. pane.getChildren().add(rectangle2);
  73. pane.getChildren().add(rectangle3);
  74. // Create a path transition
  75. PathTransition pt = new PathTransition();
  76. pt.setDuration(Duration.millis(800));
  77. pt.setPath(circle);
  78. pt.setNode(rectangle);
  79. pt.setOrientation(
  80. PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
  81. pt.setCycleCount(Timeline.INDEFINITE);
  82. pt.setAutoReverse(false);
  83. pt.play(); // Start animation
  84.  
  85. Duration x = new Duration(300);
  86. Duration x2 = new Duration(600);
  87. PathTransition pt2 = new PathTransition();
  88. pt2.setDuration(Duration.millis(800));
  89. pt2.setDelay(x);
  90. pt2.setPath(circle);
  91. pt2.setNode(rectangle2);
  92. pt2.setOrientation(
  93. PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
  94. pt2.setCycleCount(Timeline.INDEFINITE);
  95. pt2.setAutoReverse(false);
  96. pt2.play();
  97.  
  98. circle.setOnMousePressed(e -> pt.pause());
  99. circle.setOnMouseReleased(e -> pt.play());
  100.  
  101. PathTransition pt3 = new PathTransition();
  102. pt3.setDuration(Duration.millis(800));
  103. pt3.setPath(circle);
  104. pt3.setDelay(x2);
  105. pt3.setNode(rectangle3);
  106. pt3.setOrientation(
  107. PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
  108. pt3.setCycleCount(Timeline.INDEFINITE);
  109. pt3.setAutoReverse(false);
  110. pt3.play();
  111.  
  112. circle.setOnMousePressed(e -> pt.pause());
  113. circle.setOnMouseReleased(e -> pt.play());
  114.  
  115. // Create a scene and place it in the stage
  116. Scene scene = new Scene(pane, 250, 200);
  117. primaryStage.setTitle("PathTransitionDemo"); // Set the stage title
  118. primaryStage.setScene(scene); // Place the scene in the stage
  119. primaryStage.show(); // Display the stage[
  120. }
  121.  
  122. /**
  123. * The main method is only needed for the IDE with limited
  124. * JavaFX support. Not needed for running from the command line.
  125. */
  126. public static void main(String[] args) {
  127. launch(args);
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement