我一直在嘗試完成這個終極井字遊戲,但我在一個損失在哪裏我出了錯。我的問題是,當我編譯和運行的代碼,我得到的是一個空白的圖形用戶界面Blank GUI。除此之外,代碼在後臺工作。它只是沒有顯示構成遊戲的垂直和水平線以及棋子。身份證讚賞任何幫助或只是指出我在正確的方向感謝。終極井字遊戲的GUI是空白的,每次我運行我的代碼
我希望重新看看我的代碼會看到我缺少的東西。這是我的XOBoard代碼
package javafxapplicationprototype;
//an implementation of the XO board and the game logic
//imports necessary for this class
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Translate;
//class definition for drawing a game board
class XOBoard extends Pane
{
//constructor for the class
public XOBoard(XOUltimateBoard ultimate1)
{
this.ultimate1 = ultimate1;
// Initialise the boards
board = new int[3][3];
// renders is an array that holds the 'render pieces'
renders = new XOPiece[3][3];
// initialise the rectangle
back = new Rectangle();
// add the rectangle
getChildren().addAll(back);
//init the board
board = new int[3][3];
renders = new XOPiece[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++){
board[i][j] = EMPTY;
renders[i][j] = null;
}
back = new Rectangle();
back.setFill(Color.BLACK);
h1 = new Line(); h2 = new Line();
v1 = new Line(); v2 = new Line();
h1.setStroke(Color.WHITE); h2.setStroke(Color.WHITE);
v1.setStroke(Color.WHITE); v2.setStroke(Color.WHITE);
//horazontal lines
h1.setStartX(0); h1.setStartY(0); h1.setEndY(0);
h2.setStartX(0); h2.setStartY(0); h2.setEndY(0);
//vertical lines
v1.setStartX(0); v1.setStartY(0); v1.setEndX(0);
v2.setStartX(0); v2.setStartY(0); v2.setEndX(0);
//translation of one cell height and two cell heights
ch_one = new Translate(0,0);
ch_two = new Translate(0,0);
h1.getTransforms().add(ch_one);
h2.getTransforms().add(ch_two);
//translation of one cell height and two cell heights
cw_one = new Translate(0,0);
cw_two = new Translate(0,0);
v1.getTransforms().add(cw_one);
v2.getTransforms().add(cw_two);
//add rectangle and lines to this group
getChildren().addAll(back,h1,h2,v1,v2);
}
// we have to override resizing behaviour to make our view appear properly
@Override
public void resize(double width, double height)
{
//call the superclass method first
super.resize(width, height);
// figure out the width and height of a cell
cell_width = width/3.0;
cell_height = height/3.0;
//resize the rectangle to take the full size of the widget
back.setWidth(width); back.setHeight(height);
//set a new y on the horizontal lines and translate them in to place
ch_one.setY(cell_height); ch_two.setY(2 * cell_height);
h1.setEndX(width); h2.setEndX(width);
//set a new x on the vertical lines and translate them into place
cw_one.setX(cell_width); cw_two.setX(2 * cell_width);
v1.setEndX(height); v2.setEndX(height);
// we need to reset the sizes and positions of all XOPieces that were placed
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(board[i][j] != 0)
{
renders[i][j].relocate(i * cell_width, j * cell_height);
renders[i][j].resize(cell_width, cell_height);
}
}
}
}
//public method for resetting the game
public void resetGame()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = 0;
getChildren().remove(renders[i][j]);
renders[i][j]= null;
}
}
}
//public method that tries to place a piece
public void placePiece(final double x, final double y)
{
// translate the x, y coordinates into cell indexes
int indexx = (int) (x/cell_width);
int indexy = (int) (y/cell_height);
// if the position is empty then place a piece and swap the players
if(board[indexx][indexy] == XPIECE) //toke this out == EMPTY && current_player
{
// board is the array that holds all the pieces
board[indexx][indexy] = XPIECE;
// Create a new XPIECE
renders[indexx][indexy] = new XOPiece(XPIECE);
renders[indexx][indexy].resize(cell_width, cell_height);
renders[indexx][indexy].relocate(indexx * cell_width,indexy * cell_height);
// Place an X on the board at position x,y
getChildren().add(renders[indexx][indexy]);
this.ultimate1.setCurrent_player(OPIECE);
}
else if(board[indexx][indexy]== OPIECE) //toke this out == EMPTY && current_player
{
// board is the array that holds all the pieces
board[indexx][indexy] = OPIECE;
// Create a new XPIECE
renders[indexx][indexy] = new XOPiece(OPIECE);
renders[indexx][indexy].resize(cell_width, cell_height);
renders[indexx][indexy].relocate(indexx * cell_width,indexy * cell_height);
// Place an X on the board at position x,y
getChildren().add(renders[indexx][indexy]);
this.ultimate1.setCurrent_player(XPIECE);
}
}
//private fields of the class
private int[][] board; // array that holds all pieces
private XOPiece[][] renders; // array that holds all the render pieces
private Rectangle back; // background of the board
private final Line h1, h2, v1, v2; // horizontal and vertical grid lines
private double cell_width, cell_height; // width and height of a cell
//translation of {one, two} cell {width, height}
private final Translate ch_one, ch_two, cw_one, cw_two;
// constants for the class
private final XOUltimateBoard ultimate1;
private final int EMPTY = 0;
private final int XPIECE = 1;
private final int OPIECE = 2;
}
,並從我XOUltimateBoard類女巫調用它的應該做的3場比賽最終的棋盤遊戲* 3個遊戲
package javafxapplicationprototype;
//an implementation of the XO board and the game logic
//imports necessary for this class
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Translate;
//class definition for drawing a game board
class XOUltimateBoard extends Pane
{
//constructor for the class
public XOUltimateBoard()
{
// renders is an array that holds the 'render pieces'
renders = new XOBoard[3][3];
// Initialise the boards
board = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = EMPTY;
renders[i][j] = new XOBoard(this);
getChildren().add(renders[i][j]);
}
}
// Current Player is Always X to begin with
current_player = OPIECE;
}
// override resizing behaviour to make our view appear properly
@Override
public void resize(double width, double height)
{
//call the superclass method first
super.resize(width, height);
// figure out the width and height of a cell
cell_width = width/3.0;
cell_height = height/3.0;
// reset the sizes and positions of all XOPieces that were placed
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(board[i][j] != 0)
{
renders[i][j].relocate(i * cell_width, j * cell_height);
renders[i][j].resize(cell_width, cell_height);
}
}
}
}
//public method for resetting the game
public void resetGame()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = 0;
getChildren().remove(renders[i][j]);
}
}
}
// Return the current player
public int getCurrent_player(){
return current_player;
}
// Set the current player
public void setCurrent_player(int current_player){
this.current_player = current_player;
}
//public method that tries to place a piece
public void placePiece(final double x, final double y)
{
//x and y store the coordinates of where the user clicked on the board
System.out.println(x);
System.out.println(y);
// all cells have a constant height and width
System.out.println(cell_width);
System.out.println(cell_height);
// translate the x, y coordinates into cell indexes
int indexx = (int) (x/cell_width);
int indexy = (int) (y/cell_height);
System.out.println("indexx y and indexx x :");
System.out.println(indexx);
System.out.println(indexy);
// if the position is empty then place a piece and swap the players
if(board[indexx][indexy] == EMPTY && current_player == XPIECE)
{
System.out.println("Value of board[indexx][indexy] IS..."+XPIECE);
// board is the array that holds all the pieces
board[indexx][indexy] = XPIECE;
System.out.println("board[indexx][indexy]....");
System.out.println(board[indexx][indexy]);
// Create a new XPIECE
//renders[indexx][indexy] = new XOPiece(XPIECE);
// Place an X on the board at position x,y
getChildren().add(renders[indexx][indexy]);
current_player = OPIECE;
}
else if(board[indexx][indexy] == EMPTY && current_player == OPIECE)
{
board[indexx][indexy] = OPIECE;
System.out.println("Value of OPIECE IS...");
System.out.println(OPIECE);
//renders[indexx][indexy] = new XOPiece(OPIECE);
getChildren().add(renders[indexx][indexy]);
current_player = XPIECE;
}
}
//private fields of the class
private final int[][] board; // array that holds all pieces
private final XOBoard[][] renders; // array that holds all the render pieces
private double cell_width, cell_height; // width and height of a cell
// translation of {one, two} cell {width, height}
private int current_player; // who is the current player
// constants for the class
private final int EMPTY = 0;
private final int XPIECE = 1;
private final int OPIECE = 2;
}
你已經張貼了大量的代碼。當您僅提取重要部分和精確問題時,您很可能會得到一些幫助。 – Kacper
你有沒有看到我發佈的代碼運行時顯示的鏈接(空白圖形用戶界面)。但我的代碼指出,它應該有一個黑色的背景,白色的垂直和水平線..我不知道我錯了那就是爲什麼我提出這兩個類.. – enat
我只是想告訴你,並不多人們會閱讀你的代碼,因爲它有很多。 – Kacper