Skip to content

Instantly share code, notes, and snippets.

@sedj601
Created February 27, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sedj601/f5ec79bcb120b70262dff67dea736fdd to your computer and use it in GitHub Desktop.
Save sedj601/f5ec79bcb120b70262dff67dea736fdd to your computer and use it in GitHub Desktop.
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.shape.Circle;
/**
*
* @author sedri
*/
public class PVector{
DoubleProperty xDoubleProperty = new SimpleDoubleProperty();
DoubleProperty yDoubleProperty = new SimpleDoubleProperty();
public PVector() {
}
public PVector(double x, double y)
{
xDoubleProperty.setValue(x);
yDoubleProperty.setValue(y);
}
public void add(PVector pVector)
{
xDoubleProperty.setValue(xDoubleProperty.getValue() + pVector.xDoubleProperty.getValue());
yDoubleProperty.setValue(yDoubleProperty.getValue() + pVector.yDoubleProperty.getValue());
}
public void subtract(PVector pVector)
{
xDoubleProperty.setValue(xDoubleProperty.getValue() - pVector.xDoubleProperty.getValue());
yDoubleProperty.setValue(yDoubleProperty.getValue() - pVector.yDoubleProperty.getValue());
}
public void mult(double scalar)
{
xDoubleProperty.setValue(xDoubleProperty.getValue() * scalar);
yDoubleProperty.setValue(yDoubleProperty.getValue() * scalar);
}
public void div(double scalar)
{
xDoubleProperty.setValue(xDoubleProperty.getValue() / scalar);
yDoubleProperty.setValue(yDoubleProperty.getValue() / scalar);
}
public double calMag()
{
double x = xDoubleProperty.getValue();
double y = yDoubleProperty.getValue();
return Math.sqrt((x * x) + (y * y));
}
public void normalize()
{
double mag = calMag();
if(mag != 0)
{
div(mag);
}
}
@Override
public String toString() {
return "PVector{" + "xDoubleProperty=" + xDoubleProperty + ", yDoubleProperty=" + yDoubleProperty + '}';
}
}
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author sedri
*/
public class PVectorExample extends Application {
Timeline timeline;
@Override
public void start(Stage primaryStage) {
PVector location = new PVector(300, 250);
PVector velocity = new PVector(5, 0);
PVector acceleration = new PVector(1, 0);
Circle circle = new Circle(25, Color.BLUE);
circle.centerXProperty().bind(location.xDoubleProperty);
circle.centerYProperty().bind(location.yDoubleProperty);
timeline = new Timeline(new KeyFrame(Duration.seconds(.04), (event) -> {
velocity.add(acceleration);
location.add(velocity);
System.out.println(velocity);
}));
timeline.setCycleCount(Timeline.INDEFINITE);
Button btnStart = new Button();
btnStart.setText("Start");
btnStart.setOnAction((ActionEvent event) -> {
timeline.play();
});
Pane board = new Pane(circle);
board.setPrefSize(600, 500);
board.setMaxSize(600, 500);
board.setStyle("-fx-background-color: white;");
VBox root = new VBox(new StackPane(board), btnStart);
root.setStyle("-fx-background-color: yellow;");
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 700, 700);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment