The game is drawn on the canvas and there is such an approximately stopwatch implementation code.

while(true)( Tread.sleep(1); millisec++; class.onDraw1(canvas,millisec)// тут читаю строку и рисую изображения цифр. 

Further, the code can not write, it is enough to ask a question. If this code is attributed to the game cycle stream, then it will sleep almost all the time. And the cycle itself spends time and I don’t get an exact stopwatch. If you make another stream and somehow get access to the canvas, which is declared in the game cycle stream, all the same, either the outline will lock in the game stream, or I will have to lock it all the time, every millisecond, to draw time.

Hence the question, how to solve this problem?

While I wrote these thoughts: if the top line of the playing field is still for beauty, can this part be drawn on another canvas in another thread? But you have to adjust the dimensions carefully, and here there are also different screen resolutions.
Another thought: in the Layout in the ImageView draw the canvas, and on top of this ImageView another one. Even if this is possible, then due to different streams, one on top will draw something else. It turns out not to impose, as well as placed nearby.

Which option seems better to you and what other solutions are there. And by the way, if I draw on a Layout , then I can use the Chronometer object, and I can change its images of numbers with my own

  • 2
    make one thread that takes data from the model and displays the changes. other threads can change the model as they need. and they also need to sync on the model. - Mikhail Vaysman
  • Under the model, are you talking about layout? And how will the rest change as you like? In order for one to change the other one must wait until the canvas is released, and he will be busy every millisecond. It is necessary that he was always free. Therefore, it will not work on one canvas. Although I can skip frames for a stopwatch and synchronize. Just how to synchronize correctly, but that is another question. - Turalllb
  • By model, I mean a model from MVC. model is a description of the state of the application (or its part). - Mikhail Vaysman
  • I did not read the paternes at all, I decided to play the first game without them. Well, MVC in general, I understand. And the model in it is the classes where the result of the game situation calculations will be stored. Suppose the flow 1 considers the position of the character, the flow 2 changes the time data every millisecond. And stream 3 reads some data made in stream 2, others in stream 1 and 30 times per second (30fps) displays them. I understood. Now I am tortured with synchronization and receiving data from one class to another. I don't know much about it yet. - Turalllb
  • you can then answer your own question and choose it as correct - Mikhail Vaysman

1 answer 1

If I wanted to draw everything on the canvas, then I do this: every millisecond, trying to get access to the canvas on which it is possible to draw another stream, is meaningless, at least because it is physically unable to refresh the screen 1000 times a second. 60 frames per second means time can be output every approximately 16 milliseconds. And the option of trying to draw from two different threads on one canvas is not true. What should be drawn on the stopwatch is stored in a variable and changed in one stream, and the value of this variable is drawn from another stream. All according to the MVC pattern. The variables are our model, the canvas is a view, and the controller is the thread that variables request to draw. And the classes that calculate these variables I think also belong to the model, correct it if I'm wrong.