iamaamir

ButterFlyFX

Feb 20th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package funWithParametricEquations;
  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. public class Butterfly extends Application {
  20.  
  21.     private GraphicsContext g;
  22.     double t = 0.0;
  23.     final int WIDTH = 800;
  24.     final int HEIGHT = 600;
  25.  
  26.     @Override
  27.     public void start(Stage win) throws Exception {
  28.         Scene scene = new Scene(createContent(), WIDTH, HEIGHT);
  29.         win.setScene(scene);
  30.         win.setTitle("ButterFly - aamir-4u.blogspot.in");
  31.         win.show();
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         launch(args);
  36.     }
  37.  
  38.     private Parent createContent() {
  39.         Pane root = new Pane();
  40.         Canvas canvas = new Canvas(WIDTH, HEIGHT);
  41.         g = canvas.getGraphicsContext2D();
  42.  
  43.         AnimationTimer timer;
  44.         timer = new AnimationTimer() {
  45.             @Override
  46.             public void handle(long now) {
  47.                 t += 0.010;
  48.                 draw();
  49.             }
  50.  
  51.             private void draw() {
  52.                 Point2D p = curve();
  53.                 g.setStroke(Color.RED);
  54.                 double newX = p.getX() + 400;
  55.                 double newY = p.getY() + 300;
  56.                 final int STROKE_SIZE = 1;
  57.                 g.strokeOval(newX, newY, STROKE_SIZE, STROKE_SIZE);
  58.             }
  59.  
  60.             private Point2D curve() {
  61.                 double x = sin(t) * (pow(E,cos(t)) - 2 * cos(4*t) - pow(sin(t/12),5));
  62.                 double y = cos(t) * (pow(E,cos(t)) - 2 * cos(4*t) - pow(sin(t/12),5));
  63.                 final double PREF_RADIUS = 85;
  64.                 return new Point2D(x, y).multiply(PREF_RADIUS);
  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