19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

// Create cells and place cells in the panel<br />

cell = new Cell[3][3];<br />

for (int i = 0; i < 3; i++)<br />

for (int j = 0; j < 3; j++)<br />

jPanel1.add(cell[i][j] = new Cell(i, j));<br />

try {<br />

initializeRMI();<br />

}<br />

catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

/** Initialize RMI */<br />

protected boolean initializeRMI() throws Exception {<br />

String host = "";<br />

if (!isStandalone) host = getCodeBase().getHost();<br />

}<br />

try {<br />

Registry registry = LocateRegistry.getRegistry(host);<br />

ticTacToe = (TicTacToeInterface) registry.lookup("TicTacToeImpl");<br />

System.out.println("Server object " + ticTacToe + " found");<br />

}<br />

catch (Exception ex) {<br />

System.out.println(ex);<br />

}<br />

// Create callback for use by the server <strong>to</strong> control the client<br />

CallBackImpl callBackControl = new CallBackImpl(this);<br />

if (<br />

(marker = ticTacToe.connect((CallBack)callBackControl)) != ' ')<br />

{<br />

System.out.println("connected as " + marker + " player.");<br />

jlblIdentification.setText("You are player " + marker);<br />

return true;<br />

}<br />

else {<br />

System.out.println("already two players connected as ");<br />

return false;<br />

}<br />

/** Set variable myTurn <strong>to</strong> true or false */<br />

public void setMyTurn(boolean myTurn) {<br />

this.myTurn = myTurn;<br />

}<br />

/** Set message on the status label */<br />

public void setMessage(String message) {<br />

jlblStatus.setText(message);<br />

}<br />

/** Mark the specified cell using the <strong>to</strong>ken */<br />

public void mark(int row, int column, char <strong>to</strong>ken) {<br />

cell[row][column].setToken(<strong>to</strong>ken);<br />

23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!