| C:\upload\java\Life\source\java\HowGameIsPlayedScreen.java |
import java.awt.*;
import java.awt.event.*;
final class HowGameIsPlayedScreen extends Dialog
implements ActionListener,
Constants,
Strings
{
private Vars vars;
private Objects objects;
private Frame parentFrame;
private PopupWindow error;
private boolean inputByRowsAndColumns;
private String title;
private boolean modal;
private TextFieldObject numRowsTextField;
private TextFieldObject numColumnsTextField;
private TextFieldObject timeDelayTextField;
private TextFieldObject probabilityTextField;
private TextFieldObject pixelsPerCellTextField;
private Button byMouseClickButton;
private Button byTimeDelayButton;
private Button clickToSayWhoIsAliveAndDeadButton;
private Button byRowsAndColumnsButton;
private Button byPixelsButton;
private Button probalisticButton;
private Button okButton;
private Button quitButton;
private boolean quit;
HowGameIsPlayedScreen(String title,
boolean modal,
Frame parentFrame,
Objects objects,
Vars vars)
{
super(parentFrame, title, modal);
String errorString = ((title != null) &&
(parentFrame != null) &&
(objects != null) &&
(vars != null))?
NULL_STRING : "title = " + title +
"parentFrame = " + parentFrame +
"objects = " + objects +
"vars = " + vars;
if (!errorString.equals(NULL_STRING))
{
System.out.println("Error, null pointer, HowGameIsPlayedScreen " +
errorString);
return;
}
this.parentFrame = parentFrame;
this.title = title;
this.modal = modal;
this.objects = objects;
this.vars = vars;
quit = false;
inputByRowsAndColumns = true;
setFont(SMALL_FONT);
byMouseClickButton = new Button(BY_MOUSE_CLICK);
byTimeDelayButton = new Button(BY_TIME_DELAY);
clickToSayWhoIsAliveAndDeadButton =
new Button(INPUT_ALIVE_OR_DEAD_BY_MOUSE_CLICK);
byRowsAndColumnsButton = new Button(BY_ROWS_AND_COLUMNS);
byPixelsButton = new Button(BY_PIXELS);
probalisticButton = new Button(INPUT_VIA_PROBALISTIC);
okButton = new Button(OK);
quitButton = new Button(QUIT);
Label buttonOrTimeLabel = new Label(
HOW_DO_WE_GO_FROM_GENERATION_TO_GENERATION,
Label.LEFT);
Label millisecondsLabel = new Label(MILLISECONDS_COMMA_MIN_EQUALS +
MIN_TIME_DELAY, Label.LEFT);
Label rowColInfoLabel = new Label(
INPUT_SIZE_OF_BOARD_BY_ROWS_AND_COLUMNS_PLUS_RANGE +
" " + vars.GetMinNumberOfRows() + " " +
TO + " " + vars.GetMaxNumberOfRows() + " " +
ROWS + ", " + vars.GetMinNumberOfColumns() + " " +
TO + " " +
vars.GetMaxNumberOfColumns() + " " + COLUMNS +
", " + OR + ", ");
Label pixelsLabel = new Label(INPUT_SIZE_OF_BOARD_BY_PIXELS_PER_SIDE_PLUS_COMMA +
vars.GetMinPixelsPerCell() + " " + TO + " " +
vars.GetMaxPixelsPerCell() + ".");
Label inputRowsLabel = new Label(NUMBER_OF_ROWS_COLON, Label.LEFT);
Label inputColsLabel = new Label(NUMBER_OF_COLUMNS);
Label inputByMouseClickOrProbabilityLabel = new Label(
INPUT_GAME_BY_CLICKING_ON_CELLS_OR_PROBALISTICALLY);
Label asPercentLabel = new Label(AS_A_PERCENT_WITH_LEFT_PARENS + MIN_PROBABILITY +
DASHES + MAX_PROBABILITY + DOUBLE_RIGHT_PARENS, Label.LEFT);
pixelsPerCellTextField = new TextFieldObject(vars.GetPixelsPerCell(),
vars.GetMinPixelsPerCell(),
vars.GetMaxPixelsPerCell(),
PIXELS_PER_CELL);
timeDelayTextField = new TextFieldObject(vars.GetTimeBetweenGenerations(),
MIN_TIME_DELAY,
MAX_INT,
TIME_BETWEEN_GENERATIONS);
probabilityTextField = new TextFieldObject(vars.GetProbability(),
MIN_PROBABILITY,
MAX_PROBABILITY,
PROBABILITY);
numRowsTextField = new TextFieldObject(vars.GetRows(),
vars.GetMinNumberOfRows(),
vars.GetMaxNumberOfRows(),
ROWS);
numColumnsTextField = new TextFieldObject(vars.GetColumns(),
vars.GetMinNumberOfColumns(),
vars.GetMaxNumberOfColumns(),
COLUMNS);
add(buttonOrTimeLabel);
add(byMouseClickButton);
add(byTimeDelayButton);
add(byRowsAndColumnsButton);
add(byPixelsButton);
add(timeDelayTextField);
add(millisecondsLabel);
add(pixelsPerCellTextField);
add(rowColInfoLabel);
add(pixelsLabel);
add(inputColsLabel);
add(inputRowsLabel);
add(numRowsTextField);
add(numColumnsTextField);
add(inputByMouseClickOrProbabilityLabel);
add(clickToSayWhoIsAliveAndDeadButton);
add(probalisticButton);
add(probabilityTextField);
add(asPercentLabel);
add(okButton);
add(quitButton);
quitButton. setCursor(vars.GetHandCursor());
okButton. setCursor(vars.GetHandCursor());
byMouseClickButton. setCursor(vars.GetHandCursor());
byTimeDelayButton. setCursor(vars.GetHandCursor());
byRowsAndColumnsButton. setCursor(vars.GetHandCursor());
byPixelsButton. setCursor(vars.GetHandCursor());
clickToSayWhoIsAliveAndDeadButton.setCursor(vars.GetHandCursor());
probalisticButton. setCursor(vars.GetHandCursor());
byMouseClickButton.addActionListener(this);
byTimeDelayButton.addActionListener(this);
byRowsAndColumnsButton.addActionListener(this);
byPixelsButton.addActionListener(this);
pixelsPerCellTextField.addActionListener(this);
timeDelayTextField.addActionListener(this);
numRowsTextField.addActionListener(this);
numColumnsTextField.addActionListener(this);
clickToSayWhoIsAliveAndDeadButton.addActionListener(this);
probalisticButton.addActionListener(this);
probabilityTextField.addActionListener(this);
okButton.addActionListener(this);
quitButton.addActionListener(this);
setButtonColors();
GridBagLayout gridbag = new GridBagLayout();
setBackground(Color.gray);
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
int numberOfColumnComponents = 100 / 12;
int numberOfRowComponents = 100 / 18;
buildConstraints(constraints, 0,
0,
4,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(buttonOrTimeLabel, constraints);
buildConstraints(constraints, 0,
1,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(byMouseClickButton, constraints);
buildConstraints(constraints, 1,
1,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(byTimeDelayButton, constraints);
buildConstraints(constraints, 2,
1,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(timeDelayTextField, constraints);
buildConstraints(constraints, 3,
1,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(millisecondsLabel, constraints);
buildConstraints(constraints, 0,
2,
4,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(rowColInfoLabel, constraints);
buildConstraints(constraints, 0,
3,
4,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(pixelsLabel, constraints);
buildConstraints(constraints, 0,
4,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(byRowsAndColumnsButton, constraints);
buildConstraints(constraints, 1,
4,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(byPixelsButton, constraints);
buildConstraints(constraints, 2,
4,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(pixelsPerCellTextField, constraints);
buildConstraints(constraints, 0,
5,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(inputRowsLabel, constraints);
buildConstraints(constraints, 1,
5,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(numRowsTextField, constraints);
buildConstraints(constraints, 2,
5,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(inputColsLabel, constraints);
buildConstraints(constraints, 3,
5,
1,
2,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.NORTHEAST;
gridbag.setConstraints(numColumnsTextField, constraints);
buildConstraints(constraints, 0,
6,
4,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(inputByMouseClickOrProbabilityLabel, constraints);
buildConstraints(constraints, 0,
7,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(clickToSayWhoIsAliveAndDeadButton, constraints);
buildConstraints(constraints, 1,
7,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(probalisticButton, constraints);
buildConstraints(constraints, 2,
7,
1,
1,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(probabilityTextField, constraints);
buildConstraints(constraints, 3,
7,
1,
2,
numberOfColumnComponents,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.NORTHEAST;
gridbag.setConstraints(asPercentLabel, constraints);
buildConstraints(constraints, 0,
8,
4,
0,
0,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
gridbag.setConstraints(okButton, constraints);
buildConstraints(constraints, 4,
8,
2,
0,
0,
numberOfRowComponents,
SMALL_PADDING,
SMALL_PADDING);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
gridbag.setConstraints(quitButton, constraints);
}
protected void SetVars(Vars vars)
{
this.vars = vars;
}
private void setButtonColors()
{
if (vars.GetWaitForTimeDelay())
{
byMouseClickButton.setBackground(DE_SELECTED_BUTTON_COLOR);
byTimeDelayButton.setBackground(SELECTED_BUTTON_COLOR);
}
else
{
byMouseClickButton.setBackground(SELECTED_BUTTON_COLOR);
byTimeDelayButton.setBackground(DE_SELECTED_BUTTON_COLOR);
}
if (vars.GetClickLiveCellsViaMouse())
{
clickToSayWhoIsAliveAndDeadButton.setBackground(SELECTED_BUTTON_COLOR);
probalisticButton.setBackground(DE_SELECTED_BUTTON_COLOR);
}
else
{
clickToSayWhoIsAliveAndDeadButton.setBackground(DE_SELECTED_BUTTON_COLOR);
probalisticButton.setBackground(SELECTED_BUTTON_COLOR);
}
if (inputByRowsAndColumns)
{
byRowsAndColumnsButton.setBackground(SELECTED_BUTTON_COLOR);
byPixelsButton.setBackground(DE_SELECTED_BUTTON_COLOR);
}
else
{
byRowsAndColumnsButton.setBackground(DE_SELECTED_BUTTON_COLOR);
byPixelsButton.setBackground(SELECTED_BUTTON_COLOR);
}
}
public void actionPerformed (ActionEvent e)
{
if (vars == null)
{
System.out.println("Vars null, actionPerformed, HowGameIsPlayedScreen");
System.exit(0);
}
else
if (!(e.getSource() instanceof Button))
{
error.ShowMessage(UNRECOVERABLE_ERROR + " " +
INPUT_NOT_A_BUTTON + ", " +
" < HowGameIsPlayedScreen : actionPerformed >",
OK);
}
else
{
String label = ((Button)e.getSource()).getLabel();
if (label.equals(OK))
{
if (OKButtonSucceeded())
{
setVisible(false);
}
}
else if (label.equals(BY_MOUSE_CLICK))
{
vars.SetWaitForTimeDelay(false);
if (verifyInput(timeDelayTextField))
{
vars.SetTimeBetweenGenerations(timeDelayTextField.getCurrentVal());
timeDelayTextField.setText(INVALID);
setButtonColors();
}
}
else if (label.equals(BY_TIME_DELAY))
{
vars.SetWaitForTimeDelay(true);
if (verifyInput(timeDelayTextField))
{
vars.SetTimeBetweenGenerations(timeDelayTextField.getCurrentVal());
setButtonColors();
}
}
else if (label.equals(INPUT_VIA_PROBALISTIC))
{
vars.SetClickLiveCellsViaMouse(false);
if (verifyInput(probabilityTextField))
{
vars.SetProbability(probabilityTextField.getCurrentVal());
setButtonColors();
}
}
else if (label.equals(INPUT_ALIVE_OR_DEAD_BY_MOUSE_CLICK))
{
vars.SetClickLiveCellsViaMouse(true);
if (verifyInput(probabilityTextField))
{
vars.SetProbability(probabilityTextField.getCurrentVal());
probabilityTextField.setText(INVALID);
setButtonColors();
}
}
else if (label.equals(BY_ROWS_AND_COLUMNS))
{
inputByRowsAndColumns = true;
if (verifyInput(numColumnsTextField) &&
verifyInput(numRowsTextField))
{
setBoardParameters();
pixelsPerCellTextField.setText("" + vars.GetPixelsPerCell());
setButtonColors();
}
}
else if (label.equals(BY_PIXELS))
{
inputByRowsAndColumns = false;
if (verifyInput(pixelsPerCellTextField))
{
setBoardParameters();
setButtonColors();
numRowsTextField.setText("" + vars.GetRows());
numColumnsTextField.setText("" + vars.GetColumns());
}
}
else if (label.equals(QUIT))
{
quit = true;
setVisible(false);
return;
}
else
{
error.ShowMessage(UNRECOVERABLE_ERROR + " " +
UNRECOGNIZED_BUTTON + " " +
"HowGameIsPlayedScreen: " +
"\"" + label + ".\" ",
OK);
}
}
}
public Insets getInsets()
{
return new Insets(DEFAULT_INSET, DEFAULT_INSET,
DEFAULT_INSET, DEFAULT_INSET);
}
< UI >
public final void setBoardParameters()
{
if (inputByRowsAndColumns)
{
vars.SetColumns(numColumnsTextField.getCurrentVal());
vars.SetRows(numRowsTextField.getCurrentVal());
int colPixels = vars.GetBoardWidth() / vars.GetColumns();
int rowPixels = vars.GetBoardHeight() / vars.GetRows();
vars.SetPixelsPerCell(Math.min(rowPixels, colPixels));
}
else
{
vars.SetPixelsPerCell(pixelsPerCellTextField.getCurrentVal());
vars.SetColumns(vars.GetBoardWidth() / vars.GetPixelsPerCell());
vars.SetRows( vars.GetBoardHeight() / vars.GetPixelsPerCell());
}
numRowsTextField.setText("" + vars.GetRows());
numColumnsTextField.setText("" + vars.GetColumns());
pixelsPerCellTextField.setText("" + vars.GetPixelsPerCell());
}
private final boolean verifyInput(TextFieldObject theTextFieldObject)
{
boolean inputGood = true;
PopupWindow inputError;
inputError = new PopupWindow(parentFrame,
objects.GetScreenLocations(),
INPUT_ERROR_STRING);
int currentVal = theTextFieldObject.getCurrentVal();
int minVal = theTextFieldObject.getMinVal();
int maxVal = theTextFieldObject.getMaxVal();
String text = theTextFieldObject.getText();
if (text.equals(INVALID))
{
theTextFieldObject.setText(""+ currentVal);
}
else
{
int answer = -1;
try
{
answer = Integer.parseInt(text);
if ((answer < minVal) || (answer > maxVal))
{
throw new IndexOutOfBoundsException();
}
}
catch(NumberFormatException exception)
{
inputError.ShowMessage(MALFORMED_INPUT_COLON + " " +
theTextFieldObject.toString(),
OK);
inputGood = false;
}
catch(IndexOutOfBoundsException exception)
{
inputError.ShowMessage(INPUT_OUT_OF_RANGE_COLON + " " +
theTextFieldObject.toString() + " " +
GOES_FROM + " " + minVal + " " + TO + " " +
maxVal + ". ",
OK);
inputGood = false;
}
catch(Exception e)
{
inputError.ShowMessage("some crash " + e.toString() +
theTextFieldObject.toString(),
OK);
inputGood = false;
}
finally
{
if (!inputGood)
{
theTextFieldObject.setText(""+ currentVal);
theTextFieldObject.requestFocus();
theTextFieldObject.selectAll();
}
else
{
theTextFieldObject.setCurrentVal(answer);
}
}
}
return (inputGood);
}
public boolean OKButtonSucceeded()
{
boolean succeeded;
succeeded = verifyInput(numRowsTextField) &
verifyInput(numColumnsTextField) &
verifyInput(probabilityTextField) &
verifyInput(timeDelayTextField) &
verifyInput(pixelsPerCellTextField);
if (succeeded)
{
setBoardParameters();
setButtonColors();
vars.SetTimeBetweenGenerations(timeDelayTextField.getCurrentVal());
vars.SetProbability(probabilityTextField.getCurrentVal());
}
return succeeded;
}
private class TextFieldObject extends TextField
{
private int min, max, currentVal;
private String name;
TextFieldObject(int currentVal, int min, int max, String name)
{
super("" + currentVal);
this.min = min;
this.max = max;
this.currentVal = currentVal;
this.name = name;
}
public String toString()
{
return name;
}
public int getMinVal()
{
return min;
}
public int getMaxVal()
{
return max;
}
public int getCurrentVal()
{
return currentVal;
}
public void setCurrentVal(int currentVal)
{
this.currentVal = currentVal;
}
}
private final void buildConstraints(GridBagConstraints gbc,
int gx,
int gy,
int gw,
int gh,
int wx,
int wy,
int padx,
int pady)
{
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.ipadx = padx;
gbc.ipady = pady;
}
public String toString()
{
return "class HowGameIsPlayedScreen";
}
public boolean playGame()
{
return !quit;
}
}