So I was inspired by one of Daniel Shiffmans coding challenge video (https://www.youtube.com/watch?v=17WoOqgXsRM&t=328s) and the new starwars to make a hyperspace simulation animation of sorts in java. I am a beginner to intermediate programmer and I did research the problem I am having but could not seem to find an answer to it but if someone else finds a similar question with an answer feel free to link to it. Anyway so I have set up a class known as star which is here.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.shape.Circle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.animation.AnimationTimer;
/**
* @author (Richard Zins)
* @version (1)
*/
public class Star extends Circle
{
double x = 0;
double y = 0;
double z = 0;
public Star()
{
x = Math.random()*800;
y = Math.random()*800;
z = Math.random()*800;
}
public void update(){
z = z - 10 ;
}
I am using this to create random starting positions for my stars which are in a 800X800 space and to change the values later. Also yes I do know I did not make my instance fields private this it so I can just easily refer to them when creating my stars as circles in my main without using methods I know this is not convention but I can change that later. Anyway so my problem is after I create my stars and create corresponding circles when I try to update their position by changing my z value with the update method I wrote and then taking the circles x value and subtracting the new z I thought they should all move but they dont. I am also aware that currently they if this was to work it would not create the same effect in the video but I am just trying to get them all to move right now. The code that I thought should do this is located in the anonymous inner class animation timer located in my main class bellow.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.shape.Circle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.animation.AnimationTimer;
/**
* @author (Richard Zins)
* @version (1)
*/
public class Starfield extends Application
{
public static void main(String[]args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Starfield Simulation");
Group layout = new Group();
Canvas canvas = new Canvas(800,800);
layout.getChildren().add(canvas);
//puts all the stars onto the scene
Star[] stars = new Star[100];
for(int i = 0;i < stars.length;i++){
stars[i] = new Star();
}
Circle[] circles = new Circle[100];
for(int i = 0;i < stars.length;i++){
for(int r = 0;r < circles.length;r++){
layout.getChildren().add(circles[r] = new Circle(stars[i].x,stars[i].y,5,Color.WHITE));
}
}
//going to handle moving the stars with animation timer
final long startNanoTime = System.nanoTime();
new AnimationTimer(){
public void handle(long currentNanoTime){
for(int i = 0;i < stars.length;i++){
for(int r = 0;r < circles.length;r++){
stars[i].update();
circles[r].setCenterX(stars[i].x - stars[i].z);
circles[r].setCenterY(stars[i].y - stars[i].z);
}
}
}
} .start();
Scene sim = new Scene(layout,800,800,Color.BLACK);
primaryStage.setScene(sim);
primaryStage.show();
}
}
I would appreciate any help with this problem or a suggestion of another way to do this or just overall suggestions for my code.