java - Make a recursive method pause in every recurse and continue with click -
I have changed the value of a variable in a recursive method which each Rikarson, then it shows the value of JPanel , And by then I would like to stop (my problem here is here) until I click (it pauses every new scarce). Then when I click on this method, the next iteration continues.
The following code is just like my actual program and I have tried to implement it. I have tried using threads and executives in many ways, but I failed.
I have collected two sections, PanelToBeClicked is a Jepianel which is made square and recursive method, and Panlmaus Listnr class click.
The following code is completely naked with the thread and the executor. I am so grateful if someone can add some written lines here to display correctly, or tell me some clue how can I implement it.
Here's the code:
Import java.awt. *; Import javax.swing *; Public square panettabelekte JPanel {int counter; Public panelsbacked () {super (); Setpriffred size (new dimension (100,100)); AddMouseListener (new panelmuse listener (this)); } @ Override Protected Zero Paint Comonant (Graphics G) {SuperPaintConant (G); G.drawString ("+ counter, 10, 10);} public zero recursiveMethod () {counter ++; repaint (); / * * stop / wait until the panel is clicked, complete procedures * Missing code here. I tried thread.sleep *, but I have now looks exception * / if (counter & lt; 10) recursiveMethod ();} public static void main (string [] Arges) { Last JFrame frame = New JFrame ("How can I do this?"); Frame.setDefaultCloseOperation (JFrame.EXIT_O N_CLOSE); Frame.add (new Panltobeclait ()); SwingUtilitieskinvokeLater (New Rnnabl) {@ Override public Boyd run () {Framekpack (); Fremksestwubl (true);}});}} listener:
public class Panlmoiselisr provides mouse adapter {private Panltobakktl Panltobeclait; public Panlmaus Listnr (Panltobakktl Panltobecleted) {this.panelToBeClicked = panelToBeClicked;} @ Override Public Wide Mouseclutte (Mouse EVENT E) {/ * * Method PaneltoBackclat Dkrekarsiv Method () ... * / Panltobbektedkrskurivmth (); / * * If the method is already running and it does not stop, do not do anything * ** Missing code * If the method is running and paused, then the method will continue to * ** missing code Do not forget that your swing panel will be executed on a separate thread. . You will have to wait on a monitor object (like a object shared between 2 threads), then clicking on the panel will send a notification to that object.
In pseudo-code (Exceptions Exclude)
Object Monitor = New Object (); Zero recursiveMethod () {// do the job and then wait ... synchronize (monitor) {monitor.wait (); }} Zero doClick () {synchronize} {monitor.notify (); }}
See the tutorial for more information.
Comments
Post a Comment