Skip to content

Instantly share code, notes, and snippets.

@arnesacnussem
Last active March 17, 2019 02:40
Show Gist options
  • Save arnesacnussem/a049f0761c0db9844d19e2d639dcccff to your computer and use it in GitHub Desktop.
Save arnesacnussem/a049f0761c0db9844d19e2d639dcccff to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class t extends Application {
Pane pane = new Pane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(pane, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
for (int i = 0; i < 20; i++) {
pane.getChildren().add(new AutoMoveCircle(800, 800, Math.random() * 20 + 100, Math.random() * 40 + 100, Math.random() * 70 + 30, Math.random() , (long) (Math.random() * 7 + 90)));
}
}
class AutoMoveCircle extends Circle {
double paneX, paneY;
double initX, initY;
double radius, slope;
double edgePadding = 1;
long updateLag;
Service<Double> serviceX = new Service<Double>() {
@Override
protected Task<Double> createTask() {
return new Task<Double>() {
@Override
protected Double call() throws Exception {
System.out.println(Thread.currentThread().getName()+" => serviceX");
double u = initX;
boolean forward = true;
while (true) {
if (forward) u++;
else u--;
if (u + radius >= paneX - edgePadding)
forward = false;
if (u - radius <= edgePadding)
forward = true;
updateValue(u);
Thread.sleep(updateLag);
}
}
};
}
};
Service<Double> serviceY = new Service<Double>() {
@Override
protected Task<Double> createTask() {
return new Task<Double>() {
@Override
protected Double call() throws Exception {
System.out.println(Thread.currentThread().getName()+" => serviceY");
double u = initY;
boolean forward = true;
while (true) {
if (forward) u += slope;
else u -= slope;
if (u + radius >= paneX - edgePadding)
forward = false;
if (u - radius <= edgePadding)
forward = true;
updateValue(u);
Thread.sleep(updateLag);
}
}
};
}
};
Service<Paint> paintService = new Service<Paint>() {
@Override
protected Task<Paint> createTask() {
return new Task<Paint>() {
@Override
protected Paint call() throws Exception {
System.out.println(Thread.currentThread().getName()+" => paintService");
double[] rgba = {1.0f, 1.0f, 1.0f, 1.0f};
while (true) {
for (int i = 0; i < rgba.length; i++) {
if (Math.random() > 0.5f) rgba[i] += 0.01f;
else rgba[i] -= 0.01f;
if (rgba[i] > 1.0f) rgba[i] = 1.0f;
if (rgba[i] < 0f) rgba[i] = 0f;
}
updateValue(Color.color(rgba[0], rgba[1], rgba[2], rgba[3]));
Thread.sleep(updateLag);
}
}
};
}
};
AutoMoveCircle(double paneX, double paneY, double centerX, double centerY, double radius, double slope, long speed) {
super(centerX, centerY, radius);
this.paneX = paneX;
this.paneY = paneY;
this.radius = radius;
this.slope = slope;
this.updateLag = 101 - speed;
pre_init();
post_init();
}
void pre_init() {
centerXProperty().bind(serviceX.valueProperty());
centerYProperty().bind(serviceY.valueProperty());
fillProperty().bind(paintService.valueProperty());
}
void post_init() {
serviceX.start();
serviceY.start();
paintService.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment