iamaamir

Circle Animation

Feb 21st, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package CircleAnimation;
  2.  
  3. import javafx.animation.AnimationTimer;
  4. import javafx.application.Application;
  5. import javafx.geometry.Point2D;
  6. import javafx.scene.Parent;
  7. import javafx.scene.Scene;
  8. import javafx.scene.canvas.Canvas;
  9. import javafx.scene.canvas.GraphicsContext;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.paint.Color;
  12. import javafx.stage.Stage;
  13. import static java.lang.Math.*;
  14.  
  15. /**
  16.  *
  17.  * @author Aamir khan
  18.  *
  19.  */
  20. public class Main extends Application {
  21.  
  22.     private GraphicsContext g;
  23.     double t = 0.0;
  24.     private Stage stage;
  25.  
  26.     @Override
  27.     public void start(Stage stage) throws Exception {
  28.         this.stage = stage;
  29.         stage.setTitle("Parametric Equation");
  30.         this.stage.setScene(new Scene(createContent()));
  31.         this.stage.show();
  32.  
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         launch(args);
  37.     }
  38.  
  39.     private Parent createContent() {
  40.         Pane root = new Pane();
  41.         root.setPrefSize(800, 600);
  42.         Canvas canvas = new Canvas(800, 600);
  43.         g = canvas.getGraphicsContext2D();
  44.  
  45.         AnimationTimer timer;
  46.         timer = new AnimationTimer() {
  47.             @Override
  48.             public void handle(long now) {
  49.                 t += 0.017;
  50.                 draw();
  51.  
  52.             }
  53.  
  54.             private void draw() {
  55.                 Point2D p = curve();
  56.                 g.setStroke(Color.BLACK);
  57.                 double strokeSize = 1;
  58.                 double newX = 400 + p.getX();
  59.                 double newY = 300 + p.getY();
  60.                 g.strokeOval(newX, newY, strokeSize, strokeSize);
  61.             }
  62.  
  63.             private Point2D curve() {
  64.                 return new Point2D(cos(t), sin(t)).multiply(50);
  65.             }
  66.  
  67.         };
  68.         timer.start();
  69.         root.getChildren().add(canvas);
  70.         return root;
  71.  
  72.     }
  73. }
Add Comment
Please, Sign In to add comment