How can I manipulate childrens of a GridPane in JavaFX?
up vote
0
down vote
favorite
I made a Chess Board using JavaFX, and set all the tiles to be children of the GridPane, is there a way I can acess these tiles and change their properties using the GridPane index?
I tried to change the color property of one tile by acessing it by the matrix of tiles, but it doesn't change the tile displayed in the GridPane.
The .getChildren() method returns a list of nodes, and I can't acess the methods of the tile object once it became a node.
Here's my Tile Class:
package chess.Board;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Tile extends Rectangle{
private final int width = TILE_W;
private final int height = TILE_Y;
private final int x;
private final int y;
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor(){
return this.color;
}
public Tile(Color c, int x, int y){
this.color = c;
this.x = x;
this.y = y;
setFill(c);
setHeight(this.height);
setWidth(this.width);
relocate(x*TILE_W, y*TILE_Y);
}
}
And Here is my Board class:
package chess.Board;
import static chess.Chess.HEIGHT;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import static chess.Chess.WIDTH;
import javafx.scene.Parent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
public class Board {
private Tile tilesMatrix;
private void setMatrix(){
Tile tilesCreator = new Tile[WIDTH][HEIGHT];
for(int y = 0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
if(y%2 == 0){
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
else
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
}
else{
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
else
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
}
}
}
this.tilesMatrix = tilesCreator;
}
public Tile getMatrix(){
return this.tilesMatrix;
}
public Parent getBoard(){
GridPane board = new GridPane();
board.setPrefSize(WIDTH*TILE_W, HEIGHT*TILE_Y);
for(int y = 0; y < HEIGHT; y++)
for(int x = 0; x < WIDTH; x++)
board.add(getMatrix()[x][y], x, y);
return board;
}
public Board(){
setMatrix();
}
}
java javafx children gridpane
add a comment |
up vote
0
down vote
favorite
I made a Chess Board using JavaFX, and set all the tiles to be children of the GridPane, is there a way I can acess these tiles and change their properties using the GridPane index?
I tried to change the color property of one tile by acessing it by the matrix of tiles, but it doesn't change the tile displayed in the GridPane.
The .getChildren() method returns a list of nodes, and I can't acess the methods of the tile object once it became a node.
Here's my Tile Class:
package chess.Board;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Tile extends Rectangle{
private final int width = TILE_W;
private final int height = TILE_Y;
private final int x;
private final int y;
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor(){
return this.color;
}
public Tile(Color c, int x, int y){
this.color = c;
this.x = x;
this.y = y;
setFill(c);
setHeight(this.height);
setWidth(this.width);
relocate(x*TILE_W, y*TILE_Y);
}
}
And Here is my Board class:
package chess.Board;
import static chess.Chess.HEIGHT;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import static chess.Chess.WIDTH;
import javafx.scene.Parent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
public class Board {
private Tile tilesMatrix;
private void setMatrix(){
Tile tilesCreator = new Tile[WIDTH][HEIGHT];
for(int y = 0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
if(y%2 == 0){
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
else
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
}
else{
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
else
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
}
}
}
this.tilesMatrix = tilesCreator;
}
public Tile getMatrix(){
return this.tilesMatrix;
}
public Parent getBoard(){
GridPane board = new GridPane();
board.setPrefSize(WIDTH*TILE_W, HEIGHT*TILE_Y);
for(int y = 0; y < HEIGHT; y++)
for(int x = 0; x < WIDTH; x++)
board.add(getMatrix()[x][y], x, y);
return board;
}
public Board(){
setMatrix();
}
}
java javafx children gridpane
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I made a Chess Board using JavaFX, and set all the tiles to be children of the GridPane, is there a way I can acess these tiles and change their properties using the GridPane index?
I tried to change the color property of one tile by acessing it by the matrix of tiles, but it doesn't change the tile displayed in the GridPane.
The .getChildren() method returns a list of nodes, and I can't acess the methods of the tile object once it became a node.
Here's my Tile Class:
package chess.Board;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Tile extends Rectangle{
private final int width = TILE_W;
private final int height = TILE_Y;
private final int x;
private final int y;
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor(){
return this.color;
}
public Tile(Color c, int x, int y){
this.color = c;
this.x = x;
this.y = y;
setFill(c);
setHeight(this.height);
setWidth(this.width);
relocate(x*TILE_W, y*TILE_Y);
}
}
And Here is my Board class:
package chess.Board;
import static chess.Chess.HEIGHT;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import static chess.Chess.WIDTH;
import javafx.scene.Parent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
public class Board {
private Tile tilesMatrix;
private void setMatrix(){
Tile tilesCreator = new Tile[WIDTH][HEIGHT];
for(int y = 0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
if(y%2 == 0){
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
else
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
}
else{
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
else
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
}
}
}
this.tilesMatrix = tilesCreator;
}
public Tile getMatrix(){
return this.tilesMatrix;
}
public Parent getBoard(){
GridPane board = new GridPane();
board.setPrefSize(WIDTH*TILE_W, HEIGHT*TILE_Y);
for(int y = 0; y < HEIGHT; y++)
for(int x = 0; x < WIDTH; x++)
board.add(getMatrix()[x][y], x, y);
return board;
}
public Board(){
setMatrix();
}
}
java javafx children gridpane
I made a Chess Board using JavaFX, and set all the tiles to be children of the GridPane, is there a way I can acess these tiles and change their properties using the GridPane index?
I tried to change the color property of one tile by acessing it by the matrix of tiles, but it doesn't change the tile displayed in the GridPane.
The .getChildren() method returns a list of nodes, and I can't acess the methods of the tile object once it became a node.
Here's my Tile Class:
package chess.Board;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Tile extends Rectangle{
private final int width = TILE_W;
private final int height = TILE_Y;
private final int x;
private final int y;
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor(){
return this.color;
}
public Tile(Color c, int x, int y){
this.color = c;
this.x = x;
this.y = y;
setFill(c);
setHeight(this.height);
setWidth(this.width);
relocate(x*TILE_W, y*TILE_Y);
}
}
And Here is my Board class:
package chess.Board;
import static chess.Chess.HEIGHT;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import static chess.Chess.WIDTH;
import javafx.scene.Parent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
public class Board {
private Tile tilesMatrix;
private void setMatrix(){
Tile tilesCreator = new Tile[WIDTH][HEIGHT];
for(int y = 0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
if(y%2 == 0){
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
else
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
}
else{
if(x%2 == 0)
tilesCreator[x][y] = new Tile(Color.DARKGREEN, x, y);
else
tilesCreator[x][y] = new Tile(Color.ANTIQUEWHITE, x, y);
}
}
}
this.tilesMatrix = tilesCreator;
}
public Tile getMatrix(){
return this.tilesMatrix;
}
public Parent getBoard(){
GridPane board = new GridPane();
board.setPrefSize(WIDTH*TILE_W, HEIGHT*TILE_Y);
for(int y = 0; y < HEIGHT; y++)
for(int x = 0; x < WIDTH; x++)
board.add(getMatrix()[x][y], x, y);
return board;
}
public Board(){
setMatrix();
}
}
java javafx children gridpane
java javafx children gridpane
asked Nov 11 at 18:55
Enzo Ferrari
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Accessing the tiles by the matrix is OK, but using getChildren() is OK too: you just have to type cast the Node to a Tile. The color of the tiles won't change unless you change the fill property of the Tile (inherited from Shape). The constructor of Tile does call setFill(), but setColor() doesn't.
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252078%2fhow-can-i-manipulate-childrens-of-a-gridpane-in-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Accessing the tiles by the matrix is OK, but using getChildren() is OK too: you just have to type cast the Node to a Tile. The color of the tiles won't change unless you change the fill property of the Tile (inherited from Shape). The constructor of Tile does call setFill(), but setColor() doesn't.
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
add a comment |
up vote
0
down vote
accepted
Accessing the tiles by the matrix is OK, but using getChildren() is OK too: you just have to type cast the Node to a Tile. The color of the tiles won't change unless you change the fill property of the Tile (inherited from Shape). The constructor of Tile does call setFill(), but setColor() doesn't.
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Accessing the tiles by the matrix is OK, but using getChildren() is OK too: you just have to type cast the Node to a Tile. The color of the tiles won't change unless you change the fill property of the Tile (inherited from Shape). The constructor of Tile does call setFill(), but setColor() doesn't.
Accessing the tiles by the matrix is OK, but using getChildren() is OK too: you just have to type cast the Node to a Tile. The color of the tiles won't change unless you change the fill property of the Tile (inherited from Shape). The constructor of Tile does call setFill(), but setColor() doesn't.
answered Nov 11 at 23:47
Benjámin Budai
587
587
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
add a comment |
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
Thank You! I've been struggling so much that I was not paying atention enought xD
– Enzo Ferrari
Nov 13 at 0:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252078%2fhow-can-i-manipulate-childrens-of-a-gridpane-in-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown