File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes incrementing loops by executing mouse clicks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "incrementing loops by executing mouse clicks" Watch "incrementing loops by executing mouse clicks" New topic

incrementing loops by executing mouse clicks

Cal Mil

Joined: Sep 04, 2010
Posts: 25
hey everyone, I am doing a comp science lab (or re-doing) since it was due last week and me and my partners didn't finish it. the directions state:

For 1 point:
Modify this to give 3 rows of 3 squares, where the program tells which of the 9 squares is clicked and changes its color to "red".
For 2 poitns:
Add a variable that counts how many times a square is clicked. This should be a variable in class, that gets incremented each time method handleMouseClick() is called. Check this variable to see if it is even or odd. On even values, change the color of the square to "red", but on odd valued clicks, change the color of the square to "blue".
For 3 points:
Check for a win! See if there are 3 of the same color in a row

so basically I have a board, and I am trying to increment it each time the user clicks on a box by one, however, I make a loop to increment it and it just loops all the way till the end of the loop. I am trying to make it where it only adds and prints the value as well as changing the color on each time the mouse is clicked. Can anyone please help? I am on step 2. Here is my Board code:
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would use an Square[][] instead of 9 buttons. Then you can loop through it.
Also you can you divide the mouse position with the square with/height to get the column/row.
Finally I would use java.awt.Color instead of using Strings for the colors.

That will make your code easier to read and use, and reduce the error proneness.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I agree. Here's the link:
subject: incrementing loops by executing mouse clicks
It's not a secret anymore!