C:\upload\java\Life\source\java\ScreenLocations.java
/*******************************************************************
    Next is for screen locations of things like buttons and windows.
    This is a pretty simple class. We try to place windows in
    "attractive" locations.
******************************************************************** */
 
import java.awt.*;
import java.awt.Event;

final class ScreenLocations implements Constants, Strings
{
    private HandleButtons buttons;
    private Vars vars;       // Only one copy of this!!!

    private PopupWindow error;
/*******************************************************************
    Constructor:
******************************************************************** */

    ScreenLocations(HandleButtons buttons/*, Frame parentFrame*/)
    {
    //    error = new PopupWindow(parentFrame, this /* Takes a pointer to ScreenLocations*/, 
      //                          ERROR_WINDOW_NAME);
        String errorString = (buttons != null) ? 
                             NULL_STRING : "buttons = " + buttons;
        if (!errorString.equals(NULL_STRING))
        {
            System.out.println("Error, null pointer, ScreenLocations " 
                              + errorString);
            return;
        }
        this.buttons = buttons;
        vars = null;            // This must be set!
    }

    public void SetVars(Vars vars)
    {
        this.vars = vars;
    }

/*******************************************************************
    Where on screen should a given window be, given its size?
******************************************************************** */

    protected Point GetLocationOnScreen(Window window)
    {
        Dimension sizeOfWindow = window.getSize();  // How big am I?
        
        if (vars == null)
        {
            System.out.println("< Vars vars null, < GetLocationOnScreen > " +
                               "in < ScreenLocations.java >?!?!" +
                               " We were called with " + window.toString());
            System.exit(0);
        }

        /* Find center of the window < window > on screen, given
           the center of the board. Note y-value is raised by
           < Y_OFFSET_TO_RAISE_WINDOW > for aesthetics:
        */
        int xValOfWindowsCenter = vars.GetAppletLocation().x +
                                       PIXELS_TO_SKIP_FROM_WIDGETS_AND_EDGES + 
                                                PIXELS_TO_SKIP_FROM_LEFT_SIDE_OF_BOARD +
                                                        vars.GetBoardWidth() / 2;

        int yValOfWindowsCenter = vars.GetAppletLocation().y +
                                          PIXELS_TO_SKIP_FROM_TOP_AND_BOTTOM + 
                                                    vars.GetBoardHeight() / 2 - 
                                                          Y_OFFSET_TO_RAISE_WINDOW;

        // Now find where on the screen this window will live:

        int xPixelCoordinateOfUpperLeft = xValOfWindowsCenter - sizeOfWindow.width / 2;
        int yPixelCoordinateOfUpperLeft = yValOfWindowsCenter - sizeOfWindow.height / 2;

        // Always be careful. Negative or "too small" numbers are nonsense.

        if (xPixelCoordinateOfUpperLeft <
                    PIXELS_WE_MUST_SKIP_FROM_EDGES_FOR_WINDOW_POSITIONING)
        {
            xPixelCoordinateOfUpperLeft =
                    PIXELS_WE_MUST_SKIP_FROM_EDGES_FOR_WINDOW_POSITIONING;
        }
        if (yPixelCoordinateOfUpperLeft <
                    PIXELS_WE_MUST_SKIP_FROM_EDGES_FOR_WINDOW_POSITIONING)
        {
            yPixelCoordinateOfUpperLeft =
                    PIXELS_WE_MUST_SKIP_FROM_EDGES_FOR_WINDOW_POSITIONING;
        }
        // Tell the patron:

        return (new Point(xPixelCoordinateOfUpperLeft, yPixelCoordinateOfUpperLeft));
    }

/*******************************************************************
    Routine < setBoardWidthAndHeight > is a small utility routine to
    set the variables < boardWidth > and < boardHeight >.

    We put this in this class because it makes more sense here than
    anywhere else.

    We are very, very, VERY careful in this routine, due to myriad bugs the
    writer found while writing / debugging it.
******************************************************************** */

   protected void SetBoardWidthAndHeightAndComponentPositions(Objects objects)
    {
        if ((objects == null) || (vars == null))
        {
            String errorStr = "";
            errorStr += (objects == null) ? "Objects null " : NULL_STRING;
            errorStr += (vars == null) ? " vars null " : NULL_STRING;
            System.out.println("In ScreenLocations, " + errorStr + "?");
            return;
        }

        boolean errorOccurred           = false;    // Through hard luck, I found
                                                    // bugs in this routine. We are
                                                    // very, very careful.
        Point genNumLocation            = null;
        Point keepGoingButtonLocation   = null;
        Point editGameButtonLocation    = null;
        Point quitGameButtonLocation    = null;
        int farthestLeftAComponentLives = 0;

        Button keepGoingButton        = null;
        Button quitGameButton         = null;
        Button editGameButton         = null;
        GenerationNumberCanvas genNum = null;

        try
        {
            if ((objects.GetButtons() == null) ||
                (objects.GetGenNum()  == null))
             {
                String errorStr = "";
                errorStr += (objects.GetButtons() == null) ? "Buttons null " : NULL_STRING;
                errorStr += (objects.GetGenNum()  == null) ? "Gen Number null " : NULL_STRING;
                throw new NullPointerException(errorStr);
             }
            keepGoingButton = objects.GetButtons().GetKeepGoingButton();
            quitGameButton  = objects.GetButtons().GetQuitGameButton();
            editGameButton  = objects.GetButtons().GetEditGameButton();
            genNum          = objects.GetGenNum();
            
            if ((keepGoingButton == null) ||
                (quitGameButton  == null) ||
                (editGameButton  == null) ||
                (genNum          == null)) 
            {
                String errorStr = "";
                errorStr += (keepGoingButton == null) ? "Keep going button null " : NULL_STRING;
                errorStr += (quitGameButton  == null) ? "Quit game button null " : NULL_STRING;
                errorStr += (editGameButton  == null) ? "Edit button null " : NULL_STRING;
                errorStr += (genNum          == null) ? "Gen num null " : NULL_STRING;
                throw new NullPointerException(errorStr);
            }
        }
        catch (NullPointerException e)
        {
            errorOccurred   = true;
            System.out.println(e.toString() + ", ScreenLocations? ");
            return;
        }

        keepGoingButton.    repaint();
        quitGameButton.     repaint();
        editGameButton.     repaint();
        objects.GetGenNum().repaint();

        Pause Sleep = new Pause(TIME_TO_WAIT_FOR_PAINTING);     //Time
        Sleep.start();                                          // to
        while (Sleep.isAlive())                                 // paint.
        {
            ;
        }
        Sleep = null;   // Garbage collect

        try // This generates errors if these aren't painted properly. Best be safe.
        {
            genNumLocation          = genNum.         getLocationOnScreen();
            keepGoingButtonLocation = keepGoingButton.getLocationOnScreen();
            editGameButtonLocation  = editGameButton. getLocationOnScreen();
            quitGameButtonLocation  = quitGameButton. getLocationOnScreen();
        }
        catch (Exception e)    // Something went wrong.
        {
            String errorStr = "";
            errorOccurred = true;
            
            errorStr += (genNumLocation          == null) ? " genNum "          : NULL_STRING;
            errorStr += (keepGoingButtonLocation == null) ? " KeepGoingButton " : NULL_STRING;
            errorStr += (editGameButtonLocation  == null) ? " EditGameButton "  : NULL_STRING;
            errorStr += (quitGameButtonLocation  == null) ? " quitGameButton "  : NULL_STRING;

            System.out.println("Error, farthestLeftAComponentLives = " + 
                                farthestLeftAComponentLives + ". Error: " +
                                e.toString() + ": someone not painted: " +
                                errorStr);
        }
        finally
        {
            if (errorOccurred)
            {
                // Get last good value.

                farthestLeftAComponentLives = vars.GetFarthestLeftAComponentLives();

                // But what if that's garbage?

                if (farthestLeftAComponentLives == GARBAGE_VALUE)
                {
                    farthestLeftAComponentLives = 
                                            DEFAULT_FARTHEST_LEFT_A_COMPONENT_LIVES;
                }
                else
                {
                    // Get default value. This shouldn't happen, but we are careful.

                    farthestLeftAComponentLives = DEFAULT_FARTHEST_LEFT_A_COMPONENT_LIVES;
                }
                // Set board locations, as they may be garbage:
/*
                keepGoingButton.    setLocation(vars.GetKeepGoingButtonLocation());
                quitGameButton.     setLocation(vars.GetQuitGameButtonLocation());
                editGameButton.     setLocation(vars.GetEditGameButtonLocation());
                objects.GetGenNum().setLocation(vars.GetGenNumLocation());
  */          }
            else    // No errors; all is well.
            {
                // Save "Good" locations.

                vars.SetGenNumLocation(genNumLocation);
                vars.SetKeepGoingButtonLocation(keepGoingButtonLocation);
                vars.SetEditGameButtonLocation(editGameButtonLocation);
                vars.SetQuitGameButtonLocation(quitGameButtonLocation);

                // Who is furthest left?

                farthestLeftAComponentLives = Math.min(genNumLocation.x,    // "Min-ed" with: 
                                                Math.min(keepGoingButtonLocation.x, 
                                                Math.min(editGameButtonLocation.x, 
                                                        quitGameButtonLocation.x)));

                vars.SetFarthestLeftAComponentLives(farthestLeftAComponentLives);

            }   // End "else" clause, "no error occurred."

        // Now we set the farthest left a widget lives on the board. Minus a
        // buffer, this will give us the available board size.

            vars.SetBoardWidth(farthestLeftAComponentLives - 
                                2*PIXELS_TO_SKIP_FROM_WIDGETS_AND_EDGES -
                                PIXELS_TO_SKIP_FROM_RIGHT_SIDE_OF_BOARD -
                                PIXELS_TO_SKIP_FROM_LEFT_SIDE_OF_BOARD);

            vars.SetBoardHeight(vars.GetAppletHeight() - 
                                    2*PIXELS_TO_SKIP_FROM_TOP_AND_BOTTOM);

        }       // End "finally" clause.
    }           // End routine < SetBoardWidthAndHeightAndComponentPositions >.
}