This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

incrementing loops by executing mouse clicks

 
Cal Mil
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Board.java, 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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic