| | |
Trying to detect what Box I'm in in Dots and Boxes
![]() |
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:
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
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!!
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)
--------------- |\ /| | \ / | Where this is all area in the | \ / | top triangle for the top line, all area in | \ / | the left triangle for the left line and so on | \ / | | / \ | | / \ | | / \ | | / \ | |/ \| ----------------
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)
public void mouseMoved(MouseEvent e) { Line currentLine; xMousePos = e.getX(); yMousePos = e.getY(); mouseMovable = true; int boxNum = -1; Graphics g = getGraphics(); g.setColor(Color.gray); orientBoxes(); //fills the boxArray with the boxes and their pos for(int i=0; i < theBoxes.length; i++) { //top line if( (xMousePos > theBoxes[i].BX1 && xMousePos < (theBoxes[i].BX1+dist) ) && ( yMousePos >= theBoxes[i].BY1 && yMousePos < theBoxes[i].BY1+(dist/2) ) ) { //boxNum = i; currentLine = theBoxes[i].top; System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 + ", " + theBoxes[i].BY1 + "]"); currentLine.isTop = true; System.out.println("You are in the top sector of box: " + theBoxes[i].boxNumber); break; } //left line else if( (yMousePos > theBoxes[i].BY1 && yMousePos < (theBoxes[i].BY1+dist) ) && xMousePos < theBoxes[i].BX1+(dist/2) ) { //boxNum = i; currentLine = theBoxes[i].left; System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 + ", " + theBoxes[i].BY1 + "]"); currentLine.isLeft = true; System.out.println("You are in the left sector of box: " + theBoxes[i].boxNumber); break; } //bottom line else if( (xMousePos > theBoxes[i].BX1 && xMousePos < (theBoxes[i].BX1+dist-20) ) && yMousePos >= theBoxes[i].BY1+(dist/2) && yMousePos <= theBoxes[i].BY1+dist ) { //boxNum = i; currentLine = theBoxes[i].bottom; System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 + ", " + theBoxes[i].BY1 + "]"); currentLine.isBottom = true; System.out.println("You are in the bottom sector of box: " + theBoxes[i].boxNumber); break; } //right line else if( (yMousePos > theBoxes[i].BY1 && yMousePos < (theBoxes[i].BY1+dist) ) && xMousePos >= theBoxes[i].BX1+(dist/2) && xMousePos <= theBoxes[i].BX1+dist) { //boxNum = i; currentLine = theBoxes[i].right; System.out.println("The coordinates of bx and by are: [" + theBoxes[i].BX1 + ", " + theBoxes[i].BY1 + "]"); currentLine.isRight = true; System.out.println("You are in the right sector of box: " + theBoxes[i].boxNumber); break; } } }
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
- Increase Your Cable Modem or DSL Speed in XP (Windows tips 'n' tweaks)
- Combo box selection problem (VB.NET)
- Trojan Desktophijack.B and others (Viruses, Spyware and other Nasties)
- The Datagrid: How does one fill text boxes in edit mode with their original content? (ASP.NET)
- Cannot Find Server error message when loading IE (Web Browsers)
- Problems with IE cannot find server/DNS error (Viruses, Spyware and other Nasties)
- "Error in linking List box with the VB6.0 database" (Visual Basic 4 / 5 / 6)
- shuting down multiple nix boxes (*nix Software)
Other Threads in the Java Forum
- Previous Thread: Networking Project!!
- Next Thread: Email application
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor





