C:\upload\java\Life\source\java\ManageBoardCoordinates.java
/* ******************************************************************
    This short simple class exists for one reason: to manage
    coordinates on the board, and, importantly:

    !!! KEEP ALL THE CODE IN ONE PLACE. !!!

    This is a maintenance issue. We don't know at the outset how many
    rows and columns there will be, how many pixels per cell ...

    So: this class handles all of the "where on the board is this"
    problems, both for setting (i, j) values to "alive" and "dead,"
    in our playing board array, as well as, "What (i, j) value did 
    that mouse click correspond to?"

    What the class does: given an (i, j) coordinate pair on the playing
    board, it returns a pixel value (northwest corner, always). And, 
    given pixel values, it returns (i, j) coordinates on the board of 
    to whom that pixel belongs.
********************************************************************* */ 

import java.awt.Point;
import java.awt.Frame;
final class ManageBoardCoordinates implements Constants, Strings
{
    private PopupWindow error;  // For errors.

/* ******************************************************************
    Constructor follows:
********************************************************************* */ 

    ManageBoardCoordinates(Frame parentFrame, ScreenLocations screenLocations)
    {
        String errorString = ((parentFrame     != null) &&
                              (screenLocations != null))? 
                             NULL_STRING : "parentFrame = " + parentFrame +
                                           "screenLocations = " + screenLocations;
        if (!errorString.equals(NULL_STRING))
        {
            System.out.println("Error, null pointer, ManageBoardCoordinates " + errorString);
            return;
        }
        error =  new PopupWindow(parentFrame, screenLocations, ERROR_WINDOW_NAME);
    }
/* ******************************************************************
    Next converts x & y-values to pixel values on the screen.
    
    Note rows & columns do not start at 0, hence the "(x - 1)" below.
********************************************************************* */ 
    protected final Point boardValuesToPixels(int i, int j, int pixelsPerCell)
    {
        return new Point(xValueToPixels(i, pixelsPerCell), 
                         yValueToPixels(j, pixelsPerCell));
    }
    
/* ******************************************************************
    Next utility converts x-values to pixel values on the screen.
    
    Note rows & columns do not start at 0, hence the "(x - 1)" below.
********************************************************************* */ 

    private final int xValueToPixels(int x, int pixelsPerCell)
    {
        return (PIXELS_TO_SKIP_FROM_LEFT_SIDE_OF_BOARD + (x - 1)*pixelsPerCell);
    }

/* ******************************************************************
    Next utility converts y-values to pixel values on the screen
    
    Note rows & columns do not start at 0, hence the "(y - 1)" below.
********************************************************************* */ 

    private final int yValueToPixels(int y, int pixelsPerCell)
    {
        return (PIXELS_TO_SKIP_FROM_TOP_AND_BOTTOM + (y - 1)*pixelsPerCell);
    }
/* ******************************************************************
    Next converts x and y-pixel-values to pixel values on the screen.
    
    Note we return a "bad" value if the user clicked the mouse off 
    of the board.
********************************************************************* */
    protected final Point PixelsToCoordinates(Vars vars)
    {
        int pixelsPerCell = vars.GetPixelsPerCell();
        return new Point(xPixelValToXCoordinate(vars.GetMouseX(), pixelsPerCell, vars.GetColumns()),
                         yPixelValToYCoordinate(vars.GetMouseY(), pixelsPerCell, vars.GetRows()));
    }
/* ******************************************************************
    Next utility converts x-pixel-values to x values on the screen -- really
    the inverse function of xValueToPixels.
    
    Note we return a "bad" value if the user clicked the mouse off 
    of the board.
********************************************************************* */ 

    private final int xPixelValToXCoordinate(int x, int pixelsPerCell, int columns )
    {
        int answer;
        if ((pixelsPerCell == GARBAGE_VALUE) || (columns == GARBAGE_VALUE))
        {
            answer = NOT_ON_BOARD;
        }
        else
        {
            answer = (int) ((float)((x - PIXELS_TO_SKIP_FROM_LEFT_SIDE_OF_BOARD) / pixelsPerCell) + 1);
        }
        if ((answer < 1) || (answer > columns))
        {
            answer = NOT_ON_BOARD;
        }
        return (answer);
    }
    
/* ******************************************************************
    Next utility converts y-pixel-values to y values on the screen -- really
    the inverse function of yValueToPixels.
    
    Note we return a "bad" value if the user clicked the mouse off 
    of the board.
********************************************************************* */

    private final int yPixelValToYCoordinate(int y, int pixelsPerCell, int rows)
    {
        int answer;
        if ((pixelsPerCell == GARBAGE_VALUE) || (rows == GARBAGE_VALUE))
        {
            answer = NOT_ON_BOARD;
        }
        else
        {
            answer = (int) ((float)((y - PIXELS_TO_SKIP_FROM_TOP_AND_BOTTOM) / pixelsPerCell) + 1);
        }
        if ((answer < 1) || (answer > rows))
        {
            answer = NOT_ON_BOARD;
        }
        return (answer);
    }
}   // End class ManageBoardCoordinates