i'm looking for a system to render this animation of the line dynamic with variable change:
public class FXMLDocumentController implements Initializable {
private long batt = 0;
#FXML
private Line lancettaBatteria;
public long mappa(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#Override
public void initialize(URL url, ResourceBundle rb) {
Random random = new Random();
batt = random.nextInt(100);
long valMappatoBatteria = this.mappa(batt, 0, 100, -40, 135);
Rotate rotazioneBatteria = new Rotate();
lancettaBatteria.getTransforms().add(rotazioneBatteria);
Timeline timelineBatteria = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rotazioneBatteria.angleProperty(), -40)),
new KeyFrame(Duration.seconds(3), new KeyValue(rotazioneBatteria.angleProperty(), valMappatoBatteria)));
timelineBatteria.play();
}
with this code it show only the first random number, my target is to move the line for infinite time with the relative random number generated(i need the random number for display the line in particular position), is this possible? i try to sorround all with a while(true)
public void initialize(URL url, ResourceBundle rb) {
while(true){
Random random = new Random();
batt = random.nextInt(100);
long valMappatoBatteria = this.mappa(batt, 0, 100, -40, 135);
Rotate rotazioneBatteria = new Rotate();
lancettaBatteria.getTransforms().add(rotazioneBatteria);
Timeline timelineBatteria = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rotazioneBatteria.angleProperty(), -40)),
new KeyFrame(Duration.seconds(3), new KeyValue(rotazioneBatteria.angleProperty(), valMappatoBatteria)));
timelineBatteria.play();
}
}
but the app stop to work.
General Approaches
In general, an infinite animation in Java can be achieved multiple ways.
Here are a few:
Set the cycle count to indefinite to have an animation play forever:
Timeline.setCycleCount(Timeline.INDEFINITE);
Also setAutoReverse to true if you want it to go back and forth.
Use an AnimationTimer.
Use a Timeline and add an onFinished handler to the timeline which updates some relevant Keyframes within the timeline as necessary and then plays the timeline from start again.
The third approach of using an onFinishedHandler is the approach followed in the specific example below.
Specific Example
This example is based upon the requirements of your question as I understand them. I have no idea what why you are trying to do this. But as far as I understand what you are trying to do, the following app will do it.
Start position of each rotation:
Random max position of a rotation:
What it does is create a timeline which will update a value in a rotation transform for a line continuously. The line starts from a starting rotation angle, animates to a random maximum value and then animates back. Once the line reaches its starting position, a new random maximum value for the rotation is generated and the line animates to this new maximum value and back again. The process continues indefinitely. The setOnFinishedHandler of the timeline animation is the point which calculates the new random maximum value and updates the keyframe for the maximum animation value appropriately.
So that may or may not be exactly what you are trying to do, but perhaps it is enough for you to implement what you need.
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Random;
public class VariableLine extends Application {
private static final double S = 100;
#Override
public void start(Stage stage) throws Exception {
RandomRotator randomRotator = new RandomRotator();
Line line = new Line(0, S, S, S);
randomRotator.getRotate().setPivotY(S);
line.getTransforms().add(randomRotator.getRotate());
Label maxValueText = new Label(randomRotator.getMaxAngle() + "");
maxValueText.textProperty().bind(randomRotator.maxAngleProperty().asString());
stage.setScene(new Scene(new Pane(maxValueText, line), S, S * 2));
stage.show();
randomRotator.getTimeline().play();
}
public static void main(String[] args) {
launch(args);
}
}
class RandomRotator {
private static final Random random = new Random(42);
private static final double INIT_ANGLE = -40;
private static final double MAX_ANGLE = 90;
private static final Duration ROTATION_DURATION = Duration.seconds(3);
private final ReadOnlyDoubleWrapper maxAngle = new ReadOnlyDoubleWrapper(INIT_ANGLE);
private final Timeline timeline = new Timeline();
private final Rotate rotate = new Rotate(INIT_ANGLE);
RandomRotator() {
timeline.getKeyFrames().addAll(
new KeyFrame(
Duration.seconds(0),
new KeyValue(rotate.angleProperty(), INIT_ANGLE)
),
new KeyFrame(
ROTATION_DURATION.divide(2),
new KeyValue(rotate.angleProperty(), maxAngle.get())
),
new KeyFrame(
ROTATION_DURATION,
new KeyValue(rotate.angleProperty(), INIT_ANGLE)
)
);
timeline.setOnFinished(event -> {
maxAngle.set(random.nextInt((int) MAX_ANGLE));
timeline.getKeyFrames().set(
1,
new KeyFrame(
ROTATION_DURATION.divide(2),
new KeyValue(rotate.angleProperty(), maxAngle.get())
)
);
timeline.playFromStart();
});
}
Rotate getRotate() {
return rotate;
}
public double getMaxAngle() {
return maxAngle.get();
}
public ReadOnlyDoubleProperty maxAngleProperty() {
return maxAngle.getReadOnlyProperty();
}
public Timeline getTimeline() {
return timeline;
}
}
Related
Okay, this is driving me crazy. The documentation is pretty weak, the example application from Oracle is very weird, with a huge convoluted helper class, and even the questions about it on here have no answers!
I've largely followed and simplified this tutorial, but instead of rotating the object, I'm trying to rotate the camera, so when you drag the mouse, it should orbit the camera.
However, though I have confirmed via console logs and debugging that the event handlers are being called, and everything seems to have the right values, my rotates just never happen! What am I missing?
Furthermore, I can't manage to move the camera at all, even the (commented out) translateX and the like don't work either, so I am quite stumped, but can't get the axis to look like anywhere but the upper left corner!
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
public class RotateCameraExample extends Group {
private double anchorX, anchorY;
private double anchorAngleX = 0;
private double anchorAngleY = 0;
private DoubleProperty angleX = new SimpleDoubleProperty(0);
private DoubleProperty angleY = new SimpleDoubleProperty(0);
Camera camera;
Group axes;
public RotateCameraExample() {
axes = buildAxes();
getChildren().add(axes);
camera = new PerspectiveCamera(true);
camera.setFarClip(6000);
camera.setNearClip(0.01);
//camera.translateYProperty().set(300); // this doesn't do anything! why?
getChildren().add(camera);
initMouseControl();
}
private void initMouseControl() {
Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
camera.getTransforms().addAll(xRotate, yRotate);
xRotate.angleProperty().bind(angleX);
yRotate.angleProperty().bind(angleY);
setOnMousePressed(event -> {
anchorX = event.getSceneX();
anchorY = event.getSceneY();
anchorAngleX = angleX.get();
anchorAngleY = angleY.get();
});
setOnMouseDragged(event -> {
angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
angleY.set(anchorAngleY + anchorX - event.getSceneX());
});
}
private Group buildAxes() {
final Box xAxis = new Box(1200, 10, 10);
final Box yAxis = new Box(10, 1200, 10);
final Box zAxis = new Box(10, 10, 1200);
xAxis.setMaterial(new PhongMaterial(Color.RED));
yAxis.setMaterial(new PhongMaterial(Color.GREEN));
zAxis.setMaterial(new PhongMaterial(Color.BLUE));
Group axisGroup = new Group();
axisGroup.getChildren().addAll(xAxis, yAxis, zAxis);
return axisGroup;
}
}
Here can see that the axis is visible in the upper left, and I want it to remain at (0, 0, 0) while moving the camera around it.
Here is the Application launch code, which is clearly not the issue:
public class TestApp extends Application {
#Override
public void start(Stage stage) throws IOException {
RotateCameraExample g = new RotateCameraExample();
Scene scene = new Scene(g, 800, 800, Color.BLACK);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Instead of adding the camera to the children of the Group,
getChildren().add(camera);
You should set the scene's camera.
scene.setCamera(g.camera);
You will immediately see the axes at the center of the screen. Similarly, mouse handler(s) should be applied to the scene. You can then update the group's transforms in your scene's handler(s).
As an example, the variation below alters the camera's rotation in response to mouse scroll events. Note how the vertical mouse scroll affects rotation about the X axis, while horizontal mouse scroll affects rotation about the Y axis. The same gestures also translate the group as a whole. An assortment of keyboard commands enable one to rotate the camera around the Z axis, dolly along the Z axis, and reset the scene.
You can translate and rotate about points on a circle, as illustrated here; in contrast, this related example animates the rotation of an object about a pivot.
import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
/**
* #see https://stackoverflow.com/a/69260181/230513
*/
public class RotateCameraExample extends Application {
private static class RotateCamera extends Group {
private final Camera camera;
private final Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
private final Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
private final Rotate zRotate = new Rotate(0, Rotate.Z_AXIS);
public RotateCamera() {
buildAxes();
camera = new PerspectiveCamera(true);
camera.setFarClip(6000);
camera.setNearClip(0.01);
camera.setTranslateZ(-2000);
camera.getTransforms().addAll(xRotate, yRotate, zRotate);
}
private void buildAxes() {
final Box xAxis = new Box(1200, 10, 10);
final Box yAxis = new Box(10, 1200, 10);
final Box zAxis = new Box(10, 10, 1200);
xAxis.setMaterial(new PhongMaterial(Color.RED));
yAxis.setMaterial(new PhongMaterial(Color.GREEN));
zAxis.setMaterial(new PhongMaterial(Color.BLUE));
Group axisGroup = new Group();
axisGroup.getChildren().addAll(xAxis, yAxis, zAxis);
this.getChildren().add(axisGroup);
}
}
#Override
public void start(Stage stage) {
RotateCamera g = new RotateCamera();
Scene scene = new Scene(g, 800, 800, Color.BLACK);
scene.setCamera(g.camera);
stage.setScene(scene);
stage.show();
scene.setOnScroll((final ScrollEvent e) -> {
g.xRotate.setAngle(g.xRotate.getAngle() + e.getDeltaY() / 10);
g.yRotate.setAngle(g.yRotate.getAngle() - e.getDeltaX() / 10);
g.setTranslateX(g.getTranslateX() + e.getDeltaX());
g.setTranslateY(g.getTranslateY() + e.getDeltaY());
});
scene.setOnKeyPressed((KeyEvent e) -> {
KeyCode code = e.getCode();
switch (code) {
case LEFT:
g.zRotate.setAngle(g.zRotate.getAngle() + 10);
break;
case RIGHT:
g.zRotate.setAngle(g.zRotate.getAngle() - 10);
break;
case UP:
g.setTranslateZ(g.getTranslateZ() - 100);
break;
case DOWN:
g.setTranslateZ(g.getTranslateZ() + 100);
break;
case HOME:
g.xRotate.setAngle(0);
g.yRotate.setAngle(0);
g.zRotate.setAngle(0);
g.setTranslateX(0);
g.setTranslateY(0);
g.setTranslateZ(0);
break;
default:
break;
}
});
}
public static void main(String[] args) {
launch();
}
}
I'm trying to draw 10,000 circles in JavaFX but it seems like its not working and I'm not even able to draw a single circle. Actually it trows me an error:
This is the code that I currently have:
public class RandomCircles extends Application {
private Random randomNumbers;
private int count;
private final double MAX_X = 600;
private final double MAX_Y = 300;
private final int FINAL_CIRCLES = 10000;
public void start(Stage primaryStage){
Circle initCircle = new Circle();
initCircle.setStroke(Color.BLACK);
initCircle.setStrokeWidth(3);
initCircle.setRadius(1);
for(count = 0; count <= FINAL_CIRCLES; count++){
initCircle.setCenterX(randomNumbers.nextInt((int) MAX_X));
initCircle.setCenterY(randomNumbers.nextInt((int) MAX_Y));
}
Group baseDemo = new Group(initCircle);
// Scene scene = new Scene(baseDemo, MAX_X, MAX_Y);
Scene scene = new Scene(baseDemo);
scene.setFill(Color.WHITE);
scene.getWidth();
primaryStage.setTitle("10,000");
primaryStage.setScene(scene);
primaryStage.setResizable(true);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
launch(args);
}
}
Can somebody also tell me if using the setCenterX/Y is the right approach to create the circles in random locations?
Thanks.
UPDATE: To the person who though of my post as a duplicate, it is not. My problem comes from my logic that I implemented in my code not from a NullPointerException(not really) error. , which was wrong. Some guy already helped me to solve it.
After fixing the runtime error you are getting, your code only draws one circle. That's because you only add one circle to your scene graph. The for loop basically does nothing. The last X and Y coordinates for the circle's center are used to draw the single, solitary circle. You need to add ten thousand circles.
In the code below, I changed 10_000 to 100 (one hundred) since 10_000 has too many overlapping circles in the stage dimensions that you set. I also increased each circle's radius.
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class RandomCircles extends Application {
private Random randomNumbers = new Random();
private int count;
private final double MAX_X = 600;
private final double MAX_Y = 300;
private final int FINAL_CIRCLES = 100;
public void start(Stage primaryStage){
Group baseDemo = new Group();
for(count = 0; count <= FINAL_CIRCLES; count++){
Circle initCircle = new Circle();
initCircle.setStroke(Color.BLACK);
initCircle.setStrokeWidth(3);
initCircle.setRadius(5);
initCircle.setCenterX(randomNumbers.nextInt((int) MAX_X));
initCircle.setCenterY(randomNumbers.nextInt((int) MAX_Y));
baseDemo.getChildren().add(initCircle);
}
Scene scene = new Scene(baseDemo);
scene.setFill(Color.WHITE);
scene.getWidth();
primaryStage.setTitle("100");
primaryStage.setScene(scene);
primaryStage.setResizable(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Change this line:
private Random randomNumbers;
to this:
private Random randomNumbers = new Random();
Your code is assuming the the Random object will be allocated like the other member variables, but it is an object and must be created with new.
So I have a javafx line chart in my program. The program generates a random value from 1-6 every second and plots it on the graph. So the X axis represents the time in seconds and the Y axis represents the random value chosen. How would I go about moving the graph on the x axis as time goes by? i.e animating the graph going in the right direction.
public class MainDisplayController implements Initializable {
LineChart<String,Number> lineChart;
Timer timer = new Timer();
TimerTask task;
Random dice = new Random();
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();
series.getData().add(new XYChart.Data<String,Number>("1",0));
lineChart.getData().add(series);
task = new TimerTask(){
int secondsPassed = 0;
#Override
public void run() {
secondsPassed++;
System.out.println(secondsPassed);
int number;
number = 1+dice.nextInt(6);
series.getData().add(new XYChart.Data<String,Number>(String.valueOf(secondsPassed),number));
}
};
timer.scheduleAtFixedRate(task, 1000, 1000);
}
}
I think you should try setting:
xAxis.setForceZeroInRange(false);
And then remove data indexed 0 (first one) from your series when certain condition is met.
Here is some code that does something similar, I hope it helps.
public class PointEverySecondLineChart extends Application {
private BorderPane root = new BorderPane();
private Scene scene = new Scene(root,800,600);
private Random rand = new Random();
private SimpleIntegerProperty
lastX = new SimpleIntegerProperty(0);
private XYChart.Series <Integer,Integer> mySeries;
private NumberAxis
xAxis = new NumberAxis(),
yAxis = new NumberAxis();
private LineChart lineChart = new LineChart<>(xAxis,yAxis);
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Timeline addPointEverySecond = new Timeline(new KeyFrame(Duration.millis(500),event->{
lastX.set(lastX.add(1).get());
mySeries.getData().add(new XYChart.Data<>(lastX.get(), rand.nextInt(100)));
if (mySeries.getData().size()>10) mySeries.getData().remove(0);
}));
addPointEverySecond.setCycleCount(Timeline.INDEFINITE);
ObservableList <XYChart.Series<Integer,Integer>> data = FXCollections.observableArrayList();
LineChart<Integer,Integer> chart = makeLineChart(data);
chart.setPrefWidth(600);
chart.setPrefHeight(600);
root.setCenter(chart);
Button btGo = new Button("GO!");
btGo.setOnAction(action -> {
mySeries = new XYChart.Series<>();
data.add(mySeries);
lastX.set(0);
addPointEverySecond.playFromStart();
});
btGo.disableProperty().bind(addPointEverySecond.statusProperty().isEqualTo(Animation.Status.RUNNING));
root.setBottom(btGo);
primaryStage.setScene(scene);
primaryStage.show();
}
LineChart<Integer, Integer> makeLineChart(ObservableList <XYChart.Series<Integer,Integer>> series) {
xAxis.setForceZeroInRange(false);
lineChart.setCreateSymbols(false);
lineChart.setData(series);
return lineChart;
}
}
This question already has answers here:
How to update the label box every 2 seconds in java fx?
(3 answers)
Closed 8 years ago.
Hi I want to create a simple game in JavaFx but I have problem. I create the scene with backgorund and images of people.
This source places 3 images of persons on background in random place.
Unfortunately I can't make that images moves.
Now i want refresh scene in every 2 seconds that persons will be places in new place.
package Game;
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BoxBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Game extends Application {
final static int h = 775;
final static int w = 523;
#Override
public void start(Stage primaryStage) throws InterruptedException {
Game x = new Game();
Group root = new Group(x.background, x.persons);
x.pokaz_tlo.setEffect(new BoxBlur());
x.pokaz_tlo.setOpacity(0.9);
primaryStage.setTitle("Good game");
primaryStage.setScene(new Scene(root, h, w));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Game{
final static Image background = new Image(Game.class.getResource("background.jpg").toString());
final static Image person1 = new Image(Game.class.getResource("person1.jpg").toString());
final static Image person2 = new Image(Game.class.getResource("person2.jpg").toString());
final static Image person3 = new Image(Game.class.getResource("person3.jpg").toString());
ImageView show_background = new ImageView(background);
ImageView show_person1 = new ImageView(person1);
ImageView show_person2 = new ImageView(person2);
ImageView show_person3 = new ImageView(person3);
Group persons = pokaz_ukrywaj_ludzi(show_person1, show_person2, show_person3);
public int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public Group pokaz_ukrywaj_ludzi(ImageView person1, ImageView person2, ImageView person3) {
final int[] x = {164, 320, 475};
final int[] y = {75, 270};
person1.setTranslateX(x[randInt(0,2)]);
person1.setTranslateY(y[randInt(0,1)]);
person2.setTranslateX(x[randInt(0,2)]);
person2.setTranslateY(y[randInt(0,1)]);
person3.setTranslateX(x[randInt(0,2)]);
person3.setTranslateY(y[randInt(0,1)]);
Group l = new Group(person1, person2, person3);
return l;
}
}
Any idea?
Now here's the problem:
In JavaFX there is no void to like repaint() or validate() like in Swing.
But you can write your own void to refresh the whole window.
Every 2 sec:
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
// Here comes your void to refresh the whole application.
}
}, 2000, 2000);
You need to use a JavaFX Animation Timer
http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/
I want to display 5 randomly positioned and colored circles. It was easy part. Now I want to adjust this code to be an animation. This application should generate random circles endlessly but the condition is that it should keep only last five circles on the screen. This is where I got stuck. JavaFx provides ListChangeListener. I think it is what I should use. But how?
The following is my unfinished code:
import java.util.Random;
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class RandomColorTest extends Application {
int radius = 20;
int sceneWidth = 300;
int sceneHeight = 300;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, sceneWidth,sceneHeight));
for (int i = root.getChildren().size(); i < 5; i++) {
root.getChildren().add(createCircle());
// the following should convey the idea:
// if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
// add one new element
// if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
// add one new element
// and so on
root.getChildren().addListener(new ListChangeListener<E>() {
#Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends E> arg0) {
// TODO Auto-generated method stub
}
});
}
}
// Create randomly positioned and colored circle
private Circle createCircle() {
final Circle circle = new Circle();
circle.setRadius(radius);
Random r = new Random();
int rCol1 = r.nextInt(256);
int rCol2 = r.nextInt(256);
int rCol3 = r.nextInt(256);
int rX = radius+r.nextInt(sceneWidth);
if (rX>sceneWidth-radius) {
rX=rX-2*radius;
}
int rY = radius+r.nextInt(sceneHeight);
if (rY>sceneHeight-radius) {
rY=rY-2*radius;
}
circle.setLayoutX(rX);
circle.setLayoutY(rY);
circle.setStroke(Color.BLACK);
circle.setFill(Color.rgb(rCol1,rCol2,rCol3));
System.out.println(rCol1+"-"+rCol2+"-"+rCol3+"-"+rX+"-"+rY);
return circle;
}
#Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
After having managed to make ListChangeListener compile without errors it doesn't still work the way expected. Changes made to for loop:
for (int i = root.getChildren().size();;i++) {
final ObservableList<Node> ol = root.getChildren();
// the following should convey the idea:
// if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
// add one new element
// if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
// add one new element
// and so on
ol.add(createCircle());
ol.addListener( new ListChangeListener<Node>(){
#Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends Node> arg0) {
// TODO Auto-generated method stub
System.out.println("one new element added, size:"+ol.size());
if (ol.size()==5) {
ol.remove(0);
}
}
});
}
For loop is defined to loop infinitely (probably not the right way to solve this problem also) and I can see from console that circles are removed and added during the program run. Alas, I can't see GUI anymore.
A similar question was also asked on the on the Oracle forums last year.
Here is sample solution using Timeline, which I prefer to a solution relying on worker threading. Though both can get the job done, I find using the JavaFX animation APIs more elegant and less error prone.
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Random;
public class FiveAutoCircleExample extends Application {
private static final Random r = new Random();
public static final int SCENE_SIZE = 800;
public static void main(String[] args) throws Exception { launch(args); }
public void start(final Stage stage) throws Exception {
final Group circles = new Group();
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(.5),
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
while (circles.getChildren().size() >= 5) circles.getChildren().remove(0);
int radius = 10 * r.nextInt(20);
circles.getChildren().add(
new Circle(
r.nextInt(SCENE_SIZE - radius * 2) + radius, r.nextInt(SCENE_SIZE - radius * 2) + radius,
radius,
new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble())
)
);
}
})
);
animation.setCycleCount(Animation.INDEFINITE);
animation.play();
// display the scene.
stage.setScene(new Scene(circles, SCENE_SIZE, SCENE_SIZE, Color.CORNSILK));
stage.show();
}
}
In your code, you have some mistakes:
the GUI is not shown because, the execution flow never reaches the primaryStage.show(); due to infinite loop in the init(primaryStage);.
new ListChangeListener is added again and again in a loop. However you should add it only once in normal situations.
You are manipulating the ol (ol.remove(0);) in its own listener which triggers the new change event recursively.
As a solution: periodic tasks, long-time background executions can be separated to a different thread.
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, sceneWidth, sceneHeight));
final ObservableList<Node> ol = root.getChildren();
new Thread(new Runnable() {
#Override public void run() {
while (true) {
try {
// Wait for 2 seconds.
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
Platform.runLater(new Runnable() {
#Override public void run() {
System.out.println("ol size:" + ol.size());
if (ol.size() == 5) {
ol.remove(0);
}
ol.add(createCircle());
}
});
}
}
}).start();
primaryStage.show();
}
I have only changed the content of start(Stage primaryStage). There is no need to add a listener. This solution is very quick but not elegant way. You must manage the thread yourself. For more elegant and modern approach refer to Worker Threading in JavaFX 2.0.
In addition, if you really want a real animation then see the example Colorful Circles Application.