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

Reply

Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

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

 
0
  #1
Oct 8th, 2005
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:

  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

  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!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

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

 
0
  #2
Oct 8th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC