나는 체스 보드가 있으며 보드에 조각을 추가하려고합니다. 보드의 모든 부분은 Rectangle이므로 조각을 추가하는 가장 좋은 방법은 조각을 얻는 각 Rectangle에 ImagePattern을 추가하는 것입니다. 내가 직면 한 문제는 ImagePattern을 Rectangle에 추가했을 때 ImagePattern이 추가되기 전에 색상이 무엇 이었는지에도 불구하고 그 Rectangle의 배경을 흰색으로 만듭니다. 그래서 내 질문은 ImagePattern 추가 한 후 사각형의 배경색을 유지할 수있는 방법이 있습니까? 데모 용으로 내 코드는 보드에 하나만 추가합니다.Rectangle 배경색을 유지하면서 ImagePattern을 사각형에 추가하는 방법
public class ChessBoard extends Application {
GridPane root = new GridPane();
final int size = 8;
public void start(Stage primaryStage) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
Rectangle square = new Rectangle();
Color color;
if ((row + col) % 2 == 0)
color = Color.WHITE;
else
color = Color.BLACK;
square.setFill(color);
root.add(square, col, row);
if(row == 4 && col == 3){
Image p = new Image("Peices/Black/0.png");
ImagePattern pat = new ImagePattern(p);
square.setFill(pat);
}
square.widthProperty().bind(root.widthProperty().divide(size));
square.heightProperty().bind(root.heightProperty().divide(size));
square.setOnMouseClicked(e->{
square.setFill(Color.BLUE);
});
}
}
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
감사합니다. 나는 이것을 잠시 동안 연구하고 있었고 나는 그것을 할 수있을 것이라고 생각하지 않았지만 나는 단지 확신하고 싶었다. StackPane 사용에 대한 여러분의 제안을 정말 좋아합니다. – theAnon
fabian, I Haven이 잠시 동안이 코드를 뒤범벅하고 있습니다. 각 개별 사각형에 MouseListener를 추가하고 해당 사각형이 특정 조각에 대한 법적 이동인지 확인하려는 경우 빠른 질문을했습니다. 리스너를 추가하는 가장 좋은 장소가 되겠습니까? 나는 지난 며칠 동안 그것을 알아 내려고 애 쓰고 있었고 시도한 것은 아무것도 작동하지 않는 것 같습니다. 이 예제를 위해 고맙습니다. 이미 톤을 배웠습니다! – theAnon