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:

---------------
|\                 /|
|  \             /  |       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

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!!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.