I have a program where sequential drawing of the picture is performed - like animation.
//------------ public void paint (Graphics g){ g = (Graphics2D) g; if (p.dx==1&&!k){ k=true; ani.start(); // ani - это поток } } //--- public void run() { boolean l=false; while(l==false){ p.move(); try { ani.sleep(10); catch{//обработка исключения } } }
Here, when you press the key, we start the stream, which every 10ms changes the image coordinates. But there is one problem. In this form, the thread will run only 1 time, and then the error will be thrown out. If you enter ani= new Thread(this)
in the paint()
method before starting the thread, then everything will work, but a lot of threads will be created, which will affect the speed of work accordingly.
How to stop it after the work of the thread and then, when necessary, start it with the .start()
method?