Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Create 10 pixels at X and Y
- Crate Random Color
- Add Circle to Panere
- Set counter to 0
- Set Start Time (System.currenttimeMillis())
- EventHandler for the circle when clicked (setonmouseclicked) for testing setOnMouseEntered
- if counter <10
- Pick a random X (Use math.random * getWidth() from pane)
- Pick a random Y (Use math.random * getHeight() from pane)
- Create random color
- Fill The circle, setCenterX, setCenterY
- Add one to counter
- Else
- Clear The pane
- set end time
- disolay how long - use add Text - do not display in console
- End If
- -----------------------------------------------------------------------------------------------
- import javafx.animation.PathTransition;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class PathTransitionDemo extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- // Create a pane
- Pane pane = new Pane();
- // Create a rectangle
- Rectangle rectangle = new Rectangle (0, 0, 25, 50);
- Rectangle rectangle2 = new Rectangle (0, 0, 25, 50);
- Rectangle rectangle3 = new Rectangle (0, 0, 25, 50);
- Color rand = new Color(Math.random(), Math.random(), Math.random(), 1);
- rectangle.setFill(rand);
- rectangle2.setFill(rand);
- rectangle3.setFill(rand);
- // Create a circle
- Circle circle = new Circle(125, 100, 50);
- circle.setFill(Color.WHITE);
- circle.setStroke(Color.BLACK);
- // Add circle and rectangle to the pane
- pane.getChildren().add(circle);
- pane.getChildren().add(rectangle);
- pane.getChildren().add(rectangle2);
- pane.getChildren().add(rectangle3);
- // Create a path transition
- PathTransition pt = new PathTransition();
- pt.setDuration(Duration.millis(800));
- pt.setPath(circle);
- pt.setNode(rectangle);
- pt.setOrientation(
- PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt.setCycleCount(Timeline.INDEFINITE);
- pt.setAutoReverse(false);
- pt.play(); // Start animation
- Duration x = new Duration(300);
- Duration x2 = new Duration(600);
- PathTransition pt2 = new PathTransition();
- pt2.setDuration(Duration.millis(800));
- pt2.setDelay(x);
- pt2.setPath(circle);
- pt2.setNode(rectangle2);
- pt2.setOrientation(
- PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt2.setCycleCount(Timeline.INDEFINITE);
- pt2.setAutoReverse(false);
- pt2.play();
- circle.setOnMousePressed(e -> pt.pause());
- circle.setOnMouseReleased(e -> pt.play());
- PathTransition pt3 = new PathTransition();
- pt3.setDuration(Duration.millis(800));
- pt3.setPath(circle);
- pt3.setDelay(x2);
- pt3.setNode(rectangle3);
- pt3.setOrientation(
- PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
- pt3.setCycleCount(Timeline.INDEFINITE);
- pt3.setAutoReverse(false);
- pt3.play();
- circle.setOnMousePressed(e -> pt.pause());
- circle.setOnMouseReleased(e -> pt.play());
- // Create a scene and place it in the stage
- Scene scene = new Scene(pane, 250, 200);
- primaryStage.setTitle("PathTransitionDemo"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage[
- }
- /**
- * The main method is only needed for the IDE with limited
- * JavaFX support. Not needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement