944,124 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1654
  • Java RSS
Oct 8th, 2005
0

Trying to detect what Box I'm in in Dots and Boxes

Expand Post »
I am trying to do a Java GUI game of dots and boxes, and I need to make the bounds for detecting whether or not the mouseMoved that the person made was in the bounds of the top, left, bottom, or right of a box. I refer to each box that we are in by a number that is added when the boxArray has a box added to it, and graphicall wise by the top right x,y coordinate that the box has (BX1, and BY1 for BOX X)

Now I believe I know why my problem is occuring, but have no clue how to fix it, and I was working on it for an eternity last night.

The box's bounds are decided in this format:

Java Syntax (Toggle Plain Text)
  1. ---------------
  2. |\ /|
  3. | \ / | Where this is all area in the
  4. | \ / | top triangle for the top line, all area in
  5. | \ / | the left triangle for the left line and so on
  6. | \ / |
  7. | / \ |
  8. | / \ |
  9. | / \ |
  10. | / \ |
  11. |/ \|
  12. ----------------

Now if the mouse is positioned above the top triangle and within that box, then it should hi-light the possible move for the top of the box that you are currently in. And if the mouse moves over a bit, and goes over the line into the left or right triangle, then the left or right should be accessed.

The problem I am having is my code to do this, handles the cases in order, which I have no idea how else to handle them...I think I need to handle them all at once but don't know how.

Here is the code I have inside my mouseMoved method

Java Syntax (Toggle Plain Text)
  1.  
  2. public void mouseMoved(MouseEvent e)
  3. {
  4. Line currentLine;
  5. xMousePos = e.getX();
  6. yMousePos = e.getY();
  7. mouseMovable = true;
  8.  
  9. int boxNum = -1;
  10. Graphics g = getGraphics();
  11. g.setColor(Color.gray);
  12. orientBoxes(); //fills the boxArray with the boxes and their pos
  13.  
  14. for(int i=0; i < theBoxes.length; i++)
  15. {
  16. //top line
  17. if( (xMousePos > theBoxes[i].BX1 && xMousePos < (theBoxes[i].BX1+dist) )
  18. && ( yMousePos >= theBoxes[i].BY1 && yMousePos < theBoxes[i].BY1+(dist/2) ) )
  19. {
  20. //boxNum = i;
  21. currentLine = theBoxes[i].top;
  22. System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 +
  23. ", " + theBoxes[i].BY1 + "]");
  24. currentLine.isTop = true;
  25.  
  26. System.out.println("You are in the top sector of box: " + theBoxes[i].boxNumber);
  27. break;
  28. }
  29. //left line
  30. else if( (yMousePos > theBoxes[i].BY1 && yMousePos < (theBoxes[i].BY1+dist) )
  31. && xMousePos < theBoxes[i].BX1+(dist/2) )
  32. {
  33. //boxNum = i;
  34. currentLine = theBoxes[i].left;
  35. System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 +
  36. ", " + theBoxes[i].BY1 + "]");
  37. currentLine.isLeft = true;
  38.  
  39. System.out.println("You are in the left sector of box: " + theBoxes[i].boxNumber);
  40. break;
  41. }
  42. //bottom line
  43. else if( (xMousePos > theBoxes[i].BX1 && xMousePos < (theBoxes[i].BX1+dist-20) )
  44. && yMousePos >= theBoxes[i].BY1+(dist/2)
  45. && yMousePos <= theBoxes[i].BY1+dist )
  46. {
  47. //boxNum = i;
  48. currentLine = theBoxes[i].bottom;
  49. System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 +
  50. ", " + theBoxes[i].BY1 + "]");
  51. currentLine.isBottom = true;
  52.  
  53. System.out.println("You are in the bottom sector of box: " + theBoxes[i].boxNumber);
  54. break;
  55. }
  56. //right line
  57. else if( (yMousePos > theBoxes[i].BY1 && yMousePos < (theBoxes[i].BY1+dist) )
  58. && xMousePos >= theBoxes[i].BX1+(dist/2)
  59. && xMousePos <= theBoxes[i].BX1+dist)
  60. {
  61. //boxNum = i;
  62. currentLine = theBoxes[i].right;
  63. System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 +
  64. ", " + theBoxes[i].BY1 + "]");
  65. currentLine.isRight = true;
  66.  
  67. System.out.println("You are in the right sector of box: " + theBoxes[i].boxNumber);
  68. break;
  69. }
  70. }
  71. }

Keeping in mind, that right now I am not actually drawing anything, just trying to verify im in the right bounds when I am. Currently what I have will say I'm in the top, when I want to be in the right, or in the bottom when I want left...There is overlap that isn't being accounted for and I have no idea how to make it happen. Please help this poor old fool!!
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005
Oct 8th, 2005
0

Re: Trying to detect what Box I'm in in Dots and Boxes

Okay so I figured out how to fix my bounds more properly. I fixed that problem, so disregard this posting. Sorry for anyone who began reading it.
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Networking Project!!
Next Thread in Java Forum Timeline: Email application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC