Java Reference
In-Depth Information
212 Platform.runLater(() -> cell[row][column].setToken(otherToken));
213 }
214
215
// An inner class for a cell
216
public class Cell extends Pane {
model a cell
217
// Indicate the row and column of this cell in the board
218
private int row;
219
private int column;
220
221
// Token used for this cell
222
private char token = ' ' ;
223
224 public Cell( int row, int column) {
225 this .row = row;
226 this .column = column;
227 this .setPrefSize( 2000 , 2000 ); // What happens without this?
228 setStyle( "-fx-border-color: black" ); // Set cell's border
229
this .setOnMouseClicked(e -> handleMouseClick());
register listener
230 }
231
232
/** Return token */
233
public char getToken() {
234
return token;
235 }
236
237 /** Set a new token */
238 public void setToken( char c) {
239 token = c;
240 repaint();
241 }
242
243 protected void repaint() {
244 if (token == 'X' ) {
245 Line line1 = new Line( 10 , 10 ,
246 this .getWidth() - 10 , this .getHeight() - 10 );
247 line1.endXProperty().bind( this .widthProperty().subtract( 10 ));
248 line1.endYProperty().bind( this .heightProperty().subtract( 10 ));
249 Line line2 = new Line( 10 , this .getHeight() - 10 ,
250 this .getWidth() - 10 , 10 );
251 line2.startYProperty().bind(
252 this .heightProperty().subtract( 10 ));
253 line2.endXProperty().bind( this .widthProperty().subtract( 10 ));
254
255
draw X
// Add the lines to the pane
256
this .getChildren().addAll(line1, line2);
257 }
258 else if (token == 'O' ) {
259 Ellipse ellipse = new Ellipse( this .getWidth() / 2 ,
260 this .getHeight() / 2 , this .getWidth() / 2 - 10 ,
261 this .getHeight() / 2 - 10 );
262 ellipse.centerXProperty().bind(
263 this .widthProperty().divide( 2 ));
264 ellipse.centerYProperty().bind(
265 this .heightProperty().divide( 2 ));
266 ellipse.radiusXProperty().bind(
267 this .widthProperty().divide( 2 ).subtract( 10 ));
268 ellipse.radiusYProperty().bind(
269 this .heightProperty().divide( 2 ).subtract( 10 ));
270 ellipse.setStroke(Color.BLACK);
271 ellipse.setFill(Color.WHITE);
draw O
 
Search WWH ::




Custom Search