So Im trying to connect JavaFX and Processing in one application and this kinda provides it:
https://github.com/micycle1/ProcessingFX
#Threads wait()/notify(), JavaFX + Processing
1 messages · Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
it does this:
new Thread(() -> Application.launch(App.class)).start();
while (fxSurface.stage == null) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
}
inside the PApplet which is the Processing part
public class App extends Application {
public static PSurfaceFX surface;
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ProcessingFX.fxml"));
Parent root = loader.load();
Controller.stage = primaryStage;
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("ProcessingFX Demo");
primaryStage.setScene(scene);
primaryStage.show();
surface.stage = primaryStage; // finish until here
Controller.stage = primaryStage;
}
}
so it kinda waits for this method to finish
but isnt there a nicer way of doing this? smth like wait() and notify()?
the Application.launch must be in a different thread
how would I use wait and notify here?
is this the correct way? or would now the javafxthread wait for the current thread?
Thread javafxThread = new Thread(() -> Application.launch(App.class));
javafxThread.start();
try {
javafxThread.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ProcessingFX.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("N-BODY SIMULATION");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
surface.stage = primaryStage;
notify();
}
Why are you trying to connect both 🤔
the example that shows how to use javafx and processing is doing it
oh yeah you mean generally, why I even work with both
instead of just one
Yea
yeah so I want to make an simulation (preferably in Processing) but also want to add some user friendly interface to it to modify the simulation
Can't you use javafx for the simulation?
not sure and im not that familiar with javafx for simulation like thsi
this is kinda all for the simulation:
public class Body {
private static final float G = 1;
private PVector position;
private PVector velocity;
private PVector acceleration;
private float mass;
private Color color = Color.WHITE;
public Body(PVector position, PVector velocity, float mass) {
this.position = position;
this.velocity = velocity;
this.acceleration = new PVector(0, 0);
this.mass = mass;
}
public void draw(PApplet parent) {
parent.fill(255);
parent.stroke(255);
float radius = PApplet.sqrt(mass) * 2 * 2;
parent.ellipse(position.x, position.y, radius*2, radius*2);
}
public void attract(Body other) {
PVector force = PVector.sub(position, other.position);
float distanceSq = PApplet.constrain(force.magSq(), 100, 1000);
force.normalize();
float fg = -(G * (mass * other.mass)) / (distanceSq);
force.setMag(fg);
acceleration.add(PVector.div(force, mass));
}
public void update() {
position.add(velocity.add(acceleration));
acceleration.set(0, 0);
}
public void setPosition(PVector position) {
this.position = position;
}
public void setVelocity(PVector velocity) {
this.velocity = velocity;
}
public void setAcceleration(PVector acceleration) {
this.acceleration = acceleration;
}
public void setMass(float mass) {
this.mass = mass;
}
public void setColor(Color color) {
this.color = color;
}
public PVector getPosition() {
return position;
}
public PVector getVelocity() {
return velocity;
}
public PVector getAcceleration() {
return acceleration;
}
public float getMass() {
return mass;
}
public Color getColor() {
return color;
}
}
I somehow need to know how to get a window with own coordinates in javafx
so the right side is the simulation and it should be kinda its own
so I can do stuff like:
simulationWindow = ...;
simulationWindow.ellipse/cirlce(x, y, radius) // or smth similar
and I dont know which window can do that in javafx
Use a canvas
A canvas allows free drawing
Basically what you have in processing
ah ok
and is the getGraphicsContext2D the method I am looking for in Canvas?
canvas itself provides no drawing
oh yeah it is
that kinda saves my life
any disadvantages with Canvas?
can you still explain me in what way to use wait and notify in this example?
don't ever use wait and notify
thats tip number 1
but to accomplish what you want you can use a Condition
declaration: module: java.base, package: java.util.concurrent.locks, interface: Condition
I don't really see disadvantages
Why do you still want yo do that
🤷
ok thanks anyways :D
Closed the thread.
is new AnimationTimer the way to go to create some kind of update loop?
maybe, i don't remember exactly