| C:\upload\java\Life\source\java\ScreenLocations.java |
import java.awt.*;
import java.awt.Event;
final class ScreenLocations implements Constants, Strings
{
private HandleButtons buttons;
private Vars vars;
private PopupWindow error;
ScreenLocations(HandleButtons buttons)
{
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;
}
public void SetVars(Vars vars)
{
this.vars = vars;
}
protected Point GetLocationOnScreen(Window window)
{
Dimension sizeOfWindow = window.getSize();
if (vars == null)
{
System.out.println("< Vars vars null, < GetLocationOnScreen > " +
"in < ScreenLocations.java >?!?!" +
" We were called with " + window.toString());
System.exit(0);
}
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;
int xPixelCoordinateOfUpperLeft = xValOfWindowsCenter - sizeOfWindow.width / 2;
int yPixelCoordinateOfUpperLeft = yValOfWindowsCenter - sizeOfWindow.height / 2;
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;
}
return (new Point(xPixelCoordinateOfUpperLeft, yPixelCoordinateOfUpperLeft));
}
< setBoardWidthAndHeight >
< boardWidth > < boardHeight >
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;
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);
Sleep.start();
while (Sleep.isAlive())
{
;
}
Sleep = null;
try
{
genNumLocation = genNum. getLocationOnScreen();
keepGoingButtonLocation = keepGoingButton.getLocationOnScreen();
editGameButtonLocation = editGameButton. getLocationOnScreen();
quitGameButtonLocation = quitGameButton. getLocationOnScreen();
}
catch (Exception e)
{
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)
{
farthestLeftAComponentLives = vars.GetFarthestLeftAComponentLives();
if (farthestLeftAComponentLives == GARBAGE_VALUE)
{
farthestLeftAComponentLives =
DEFAULT_FARTHEST_LEFT_A_COMPONENT_LIVES;
}
else
{
farthestLeftAComponentLives = DEFAULT_FARTHEST_LEFT_A_COMPONENT_LIVES;
}
}
else
{
vars.SetGenNumLocation(genNumLocation);
vars.SetKeepGoingButtonLocation(keepGoingButtonLocation);
vars.SetEditGameButtonLocation(editGameButtonLocation);
vars.SetQuitGameButtonLocation(quitGameButtonLocation);
farthestLeftAComponentLives = Math.min(genNumLocation.x,
Math.min(keepGoingButtonLocation.x,
Math.min(editGameButtonLocation.x,
quitGameButtonLocation.x)));
vars.SetFarthestLeftAComponentLives(farthestLeftAComponentLives);
}
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);
}
}
}