Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package funWithParametricEquations;
- import javafx.animation.AnimationTimer;
- import javafx.application.Application;
- import javafx.geometry.Point2D;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import static java.lang.Math.*;
- /**
- *
- * @author Aamir khan
- */
- public class Butterfly extends Application {
- private GraphicsContext g;
- double t = 0.0;
- final int WIDTH = 800;
- final int HEIGHT = 600;
- @Override
- public void start(Stage win) throws Exception {
- Scene scene = new Scene(createContent(), WIDTH, HEIGHT);
- win.setScene(scene);
- win.setTitle("ButterFly - aamir-4u.blogspot.in");
- win.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- private Parent createContent() {
- Pane root = new Pane();
- Canvas canvas = new Canvas(WIDTH, HEIGHT);
- g = canvas.getGraphicsContext2D();
- AnimationTimer timer;
- timer = new AnimationTimer() {
- @Override
- public void handle(long now) {
- t += 0.010;
- draw();
- }
- private void draw() {
- Point2D p = curve();
- g.setStroke(Color.RED);
- double newX = p.getX() + 400;
- double newY = p.getY() + 300;
- final int STROKE_SIZE = 1;
- g.strokeOval(newX, newY, STROKE_SIZE, STROKE_SIZE);
- }
- private Point2D curve() {
- double x = sin(t) * (pow(E,cos(t)) - 2 * cos(4*t) - pow(sin(t/12),5));
- double y = cos(t) * (pow(E,cos(t)) - 2 * cos(4*t) - pow(sin(t/12),5));
- final double PREF_RADIUS = 85;
- return new Point2D(x, y).multiply(PREF_RADIUS);
- }
- };
- timer.start();
- root.getChildren().add(canvas);
- return root;
- }
- }
Add Comment
Please, Sign In to add comment