P
P
pasandep2020-11-22 15:15:46
Multithreading
pasandep, 2020-11-22 15:15:46

How to bind sockets and multithreading to JavaFX?

Good day! I am doing a semester related to sockets, multithreading and JavaFX. Decided to make Atari PONG. It should work as follows, two classes Player1 and Player2 are launched, connected to the server socket and a separate window is opened for each of them. Naturally, there is a generic Game class that expects two players and starts the game.

Based on the code from here fxapps.blogspot.com/2017/01/pong-game-in-javafx-us.... And then I got up a bit. I do not quite understand how to implement the classes that implement the Callable interface, and where exactly to write the logic for changing the positions of the players. In general, I just don’t understand where and what I’m implementing to write. I would be glad for any answer, advice and a link to a useful resource, since the videos about Multithreading in JavaFX are mainly about displaying any background processes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2020-11-22
@pasandep

JavaFX simply renders graphics and ALWAYS runs on its own thread. To interact with it (to move something on the screen) from another thread, use the following Platform.runLater(java.lang.Runnable runnable) thing.
In your task, I imagine the picture as follows:
Server:
1) holds the states of 4 variables. PositionI1(y),PositionI2(y),BallPosition(x,y),BallDirectionVector(x,y). For good, additionally, the number of points for each player and a flag for each player about the readiness of the game / pause.
2) listens to the socket and waits for players. As soon as the players are connected, it starts to accept their club positions from the players and enter them into variables. Simulates the playing field in a separate thread for a fixed number of ticks with the positions of the players' sticks, the ball and its movement. Sends players the position of the sticks and the ball. Optionally sends the number of points and starts / pauses the game based on flags from the players.
Do not forget that everything in the game is maximally deterministic, i.e. the size of the field to be simulated is known, the positions of the players are known, and so on. Those. everything is perfectly simulated.
The client:
1) keeps states of 4 variables. Self Position(y), PositionI2(y), Ball Position(x, y), Ball Direction Vector(x, y).
2) In one thread connects to the server. Takes all the coordinates and sends the positions to the javafx stream.
3) The javafx thread draws graphics (remember that all sizes are known in advance), listens to button / mouse clicks and sends to the server.
At a minimum, this will work.
Those. as a result, the game logic occurs on the server. The players simply receive the coordinates of each other and the ball, in response they send the position of their stick to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question