aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Wait for a mouse click before running a while loop. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wait for a mouse click before running a while loop." Watch "Wait for a mouse click before running a while loop." New topic
Author

Wait for a mouse click before running a while loop.

Grant Eastwood
Greenhorn

Joined: Oct 06, 2013
Posts: 4
Hi everyone,

I'm working my way through the Stanford CS106a programming course online and have just completed one of the assignments, which is to program a version of Steve Wozniak's "Breakout" game. I've got the game working just fine, but I don't like the way it starts playing the second you hit the "Run Last Class" button in Eclipse, and would really like to have the game start, then wait for the user to click a button before running the core WHILE loop. Everything is implemented in a single class, and here is the playGame method that starts the ball moving:



All I want to do is pause before the WHILE loop until the users clicks the mouse button, nothing fancier than that! I've done quite a bit of reading, and it looks like ActionEvent may be the way to go, but I'm not clear on how to use it correctly in this scenario. Any help would be very much appreciated.

Thanks,

Grant.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

So what you need is a way to tell when the mouse is clicked and and a way to pause your program.

For the former, I would look into adding a MouseAdapter to your window using the Component#addMouseListener method.

For the latter, I would look into Object#wait and Object#notify using Guarded Blocks.

If you have more specific questions after looking into those things, feel free to ask.

And welcome to the JavaRanch!
Grant Eastwood
Greenhorn

Joined: Oct 06, 2013
Posts: 4
Joel Christophel wrote:So what you need is a way to tell when the mouse is clicked and and a way to pause your program.

For the former, I would look into adding a MouseAdapter to your window using the Component#addMouseListener method.

For the latter, I would look into Object#wait and Object#notify using Guarded Blocks.

If you have more specific questions after looking into those things, feel free to ask.

And welcome to the JavaRanch!


Thank you for the reply, I'll have a read and see how I go

I already have a MouseListener running in another method as I need to track the x and y coordinates of the mouse. Hopefully the MouseAdapter works in a similar fashion so I can understand it!
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

Grant Eastwood wrote:
Thank you for the reply, I'll have a read and see how I go

I already have a MouseListener running in another method as I need to track the x and y coordinates of the mouse. Hopefully the MouseAdapter works in a similar fashion so I can understand it!


The only difference between the two is that with a MouseAdapter you only have to override the methods you're actually going to use. In order to use a MouseListener, you have to override all of its methods.

More specifically, MouseAdapter implements Mouselistener and creates a bunch of empty methods.
Grant Eastwood
Greenhorn

Joined: Oct 06, 2013
Posts: 4
OK so I've played around but can't quite seem to get it working correctly. Here is a quick class I put together for testing.
Ignore the fact I'm using the acm library, it's just a necessity for the course I'm following:



I was hoping this code would basically

Call the run method.
Hit the wait statement .
Once the mouse button was clicked a notifyAll would be sent.
Upon receipt of the notifyAll program execution would continue after the wait. Printing a message.

No joy unfortunately. Reading the Guarded Blocks info it says:

A more efficient guard invokes Object.wait to suspend the current thread. The invocation of wait does not return until another thread has issued a notification that some special event may have occurred


Now I believe that since my entire program is running in a single class that I only have 1 thread running, therefore there is no second thread to send the notifyAll(). Does that sound right, or am I way off the mark here?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Not at all. When you hit run or run last class on Eclipse®, it finds the class in question and starts its main method.
That main method ought to start a single thread.Since Swing is not thread‑safe, you don't want to start multiple threads. If you only have one thread, you won't get it to work with wait/notify. (At least I think so, I am not certain.)
I think you want a startGame() method and you call that from your Listener.
Grant Eastwood
Greenhorn

Joined: Oct 06, 2013
Posts: 4
Campbell Ritchie wrote:Not at all. When you hit run or run last class on Eclipse®, it finds the class in question and starts its main method.
That main method ought to start a single thread.Since Swing is not thread‑safe, you don't want to start multiple threads. If you only have one thread, you won't get it to work with wait/notify. (At least I think so, I am not certain.)
I think you want a startGame() method and you call that from your Listener.


Thanks Campbell, I'll stop playing around with wait then
San Lewy
Greenhorn

Joined: Feb 25, 2014
Posts: 2
If you are using the ACM libraries, it's as simple as:

waitForClick();



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Welcome to the Ranch

Please tell us more about the waitForClick() method.
San Lewy
Greenhorn

Joined: Feb 25, 2014
Posts: 2
The Stanford University course the OP references, CS106A, uses the third-party Association of Computing Machinery (ACM) Java libraries. These libraries are often used in college-level Computer Science courses, and that's about the only place they are used. They are documented on-line, plus the Stanford course uses an excellent introductory Java book, The Art & Science of Java, that further documents these libraries.

The waitForClick() method used in the Breakout programming exercise is described thusly:

public void waitForClick()

Waits for a mouse click in the window before proceeding.


Usage: waitForClick();


And that's about it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wait for a mouse click before running a while loop.