import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.util.Duration;

public class PendulumPane extends Pane{
	
	double pendulumRadius = 250.0;
	double angle = 91;
	double x1 = 300;
	double y1 = 300;
	double dAngle = 0.1;
	double rate;
	
	Circle smCircle = new Circle(x1, y1, 5);
	
	double x2 = x1 + pendulumRadius * Math.cos(Math.toRadians(angle));
	double y2 = y1 + pendulumRadius * Math.sin(Math.toRadians(angle));
	Circle lrgCircle = new Circle(x2, y2, 15);
	Line line = new Line(x1, y1, x2, y2);
	
	private Timeline animation;
	
	PendulumPane() {		
		getChildren().addAll(line, smCircle, lrgCircle);
		
	       animation =new Timeline(new KeyFrame (Duration.millis(10),e->next()));
	       animation.setCycleCount(Timeline.INDEFINITE);
	       animation.play();
	}
	
	public void play()
	   {
	       animation.play();
	   }
	   
	   public void pause()
	   {
	       animation.pause();
	   }
	

	
	  public void increaseSpeed()
	   {
	       animation.setRate (animation.getRate()+10);
	   }
	   public void decreaseSpeed()
	   {
	       animation.setRate(animation.getRate ()>0 ? animation.getRate()-10:animation.getRate());
	   }
	
	void next() {
		
		//
		if(angle <= 45 || angle >= 135 )
			dAngle *= -1;
		
		//System.out.println(angle);
		
		angle -= dAngle;
		
		//Setting the second point for the end of the line and lrgCircle
		x2 = x1 + pendulumRadius * Math.cos(Math.toRadians(angle));
		y2 = y1+ pendulumRadius * Math.sin(Math.toRadians(angle));
		
		
		lrgCircle.setCenterX(x2);
		lrgCircle.setCenterY(y2);
		
		line.setEndX(x2);
		line.setEndY(y2);
	}
	
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;

public class Pendulum extends Application{
	@Override
	public void start(Stage primaryStage) {
		
		PendulumPane ppane = new PendulumPane();
		
		ppane.setOnKeyPressed(e -> { 
	    	System.out.println("working event");
	    	System.out.println(e.getCode());
	    	
	     if (e.getCode() == KeyCode.UP)
	    	 ppane.increaseSpeed();
	     
	     if (e.getCode() == KeyCode.DOWN)
	    	 ppane.decreaseSpeed();
	    });
		
		ppane.setOnMousePressed(e->ppane.pause());
		ppane.setOnMouseReleased(e->ppane.play());
		Scene scene = new Scene(ppane, 600, 600);
		primaryStage.setTitle("Pendulum");
		primaryStage.setScene(scene);
		primaryStage.show();
		
		ppane.requestFocus();
		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}