#Threads wait()/notify(), JavaFX + Processing

1 messages · Page 1 of 1 (latest)

little sierra
glossy sapphireBOT
#

<@&987246487241105418> please have a look, thanks.

glossy sapphireBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

little sierra
#

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();
    }
smoky bear
little sierra
#

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

smoky bear
#

Yea

little sierra
#

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

smoky bear
#

Can't you use javafx for the simulation?

little sierra
#

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

smoky bear
#

A canvas allows free drawing

#

Basically what you have in processing

little sierra
#

ah ok

#

and is the getGraphicsContext2D the method I am looking for in Canvas?

#

canvas itself provides no drawing

little sierra
#

that kinda saves my life

#

any disadvantages with Canvas?

little sierra
cursive gulch
#

thats tip number 1

#

but to accomplish what you want you can use a Condition

little sierra
#

isnt that kinda the same as wait and notify xD

#

await(), singal, singalAll xD

smoky bear
smoky bear
little sierra
#

I wont

#

just interested

#

is it also possible to use some kind of Canvas3D?

smoky bear
#

🤷

little sierra
#

ok thanks anyways :D

glossy sapphireBOT
#

Closed the thread.

little sierra
smoky bear