Advertisement
MrPinzon

Untitled

May 19th, 2022
702
138 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javafx.animation.PathTransition;
  2. import javafx.application.Application;
  3. import static javafx.application.Application.launch;
  4. import javafx.event.EventHandler;
  5.  
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.paint.Color;
  11.  
  12. import javafx.scene.shape.Circle;
  13. import javafx.scene.shape.LineTo;
  14. import javafx.scene.shape.MoveTo;
  15. import javafx.scene.shape.Path;
  16. import javafx.stage.Stage;
  17. import javafx.util.Duration;
  18.  
  19. public class widgets extends Application {
  20.     @Override
  21.     public void start(Stage stage) {
  22.         //Drawing a Circle
  23.         Circle circle = new Circle();
  24.  
  25.         //Setting the position of the circle
  26.         circle.setCenterX(300.0f);
  27.         circle.setCenterY(135.0f);
  28.  
  29.         //Setting the radius of the circle
  30.         circle.setRadius(25.0f);
  31.  
  32.         //Setting the color of the circle
  33.         circle.setFill(Color.BROWN);
  34.  
  35.         //Setting the stroke width of the circle
  36.         circle.setStrokeWidth(20);
  37.  
  38.         //Creating a Path
  39.         Path path = new Path();
  40.  
  41.         //Moving to the staring point
  42.         MoveTo moveTo = new MoveTo(208, 71);
  43.  
  44.         //Creating 1st line
  45.         LineTo line1 = new LineTo(421, 161);
  46.  
  47.         //Creating 2nd line
  48.         LineTo line2 = new LineTo(226,232);
  49.  
  50.         //Creating 3rd line
  51.         LineTo line3 = new LineTo(332,52);
  52.  
  53.         //Creating 4th line
  54.         LineTo line4 = new LineTo(369, 250);
  55.  
  56.         //Creating 5th line
  57.         LineTo line5 = new LineTo(208, 71);
  58.  
  59.         //Adding all the elements to the path
  60.         path.getElements().add(moveTo);
  61.         path.getElements().addAll(line1, line2, line3, line4, line5);
  62.  
  63.         //Creating the path transition
  64.         PathTransition pathTransition = new PathTransition();
  65.  
  66.         //Setting the duration of the transition
  67.         pathTransition.setDuration(Duration.millis(1000));
  68.  
  69.         //Setting the node for the transition
  70.         pathTransition.setNode(circle);
  71.  
  72.         //Setting the path for the transition
  73.         pathTransition.setPath(path);
  74.  
  75.         //Setting the orientation of the path
  76.         pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
  77.  
  78.         //Setting the cycle count for the transition
  79.         pathTransition.setCycleCount(50);
  80.  
  81.         //Setting auto reverse value to true
  82.         pathTransition.setAutoReverse(false);
  83.  
  84.         //Creating play button
  85.         Button playButton = new Button("Play");
  86.         playButton.setLayoutX(300);
  87.         playButton.setLayoutY(250);
  88.  
  89.         circle.setOnMouseClicked (new EventHandler<javafx.scene.input.MouseEvent>()
  90.         {
  91.             @Override
  92.             public void handle(javafx.scene.input.MouseEvent e)
  93.             {
  94.                 System.out.println("Color changed");
  95.                 circle.setFill(Color.DARKSLATEBLUE);
  96.             }
  97.         });
  98.         playButton.setOnMouseClicked ((new EventHandler<MouseEvent>()
  99.         {
  100.             public void handle(MouseEvent event)
  101.             {
  102.                 System.out.println("Loop started");
  103.                 pathTransition.play();
  104.             }
  105.         }));
  106.  
  107.         //Creating stop button
  108.         Button stopButton = new Button("stop");
  109.         stopButton.setLayoutX(250);
  110.         stopButton.setLayoutY(250);
  111.  
  112.         stopButton.setOnMouseClicked((new EventHandler<MouseEvent>()
  113.         {
  114.             public void handle(MouseEvent event)
  115.             {
  116.                 System.out.println("Loop ended");
  117.                 pathTransition.stop();
  118.             }
  119.         }));
  120.         //Creating a Group object
  121.         Group root = new Group(circle, playButton, stopButton);
  122.  
  123.         //Creating a scene object
  124.         Scene scene = new Scene(root, 600, 300);
  125.         scene.setFill(Color.LAVENDER);
  126.  
  127.         //Setting title to the Stage
  128.         stage.setTitle("Convenience Methods Example");
  129.  
  130.         //Adding scene to the stage
  131.         stage.setScene(scene);
  132.  
  133.         //Displaying the contents of the stage
  134.         stage.show();
  135.     }
  136.     public static void main(String args[]){
  137.         launch(args);
  138.     }
  139. }
Advertisement
RAW Paste Data Copied
Advertisement