JavaFX - 没有按钮可见的图像按钮(提供图片)

时间:2017-03-24 04:37:38

标签: java arrays image button javafx

我有一个随机生成的彩色瓷砖阵列,我将其添加到游戏的网格窗格中。到目前为止,除了切换按钮之外,实际上没有任何功能。这是一个 snippet.

我为令人讨厌的蓝色背景道歉。我添加它只是为了进一步澄清我的问题。

您看到的是我的平铺图像应用于按钮。我想要的是按钮边界(最好)不存在,或以某种方式消失。

我只想将我的图块图像视为按钮本身。

我试过摆弄边框,剪辑,不透明等等......我无法得到任何确定的东西。

假设所有必要的导入都存在,并且TileArray中的randomPopulate方法在另一个类中被调用...

Tile.java

public class Tile {

/* ----- ATTRIBUTES ----- */

private TileColor color;
private ImageView tileImg = new ImageView();
private ToggleButton tileButton = new ToggleButton(null, new ImageView());

// Constructor used in TileArray
public Tile(TileColor t, ToggleButton b) {

    color = t;
    tileButton = b;
}

// Getter
public ToggleButton getTileButton() {

    return tileButton;
}

// The enumeration used to determine which color tile is being used
// Much easier for debugging to see the color name
enum TileColor {

White,
Black,
Green,
Red
}

TileArray.java

public class TileArray {

/* ----- ATTRIBUTES ----- */

// Initialize the array of tiles
public Tile[][] arrayOfTiles = new Tile[5][5];

private Image whiteTileImg = new Image("image/white_tile.jpg");
private Image blackTileImg = new Image("image/black_tile.jpg");
private Image greenTileImg = new Image("image/green_tile.jpg");
private Image redTileImg = new Image("image/red_tile.jpg");


// Method to randomly populate the array with colored tile objects
// The if statements are intentionally specific, and not 1/4 for each
// Approximates 11 white tiles, 9 black, 3 green and 2 red
public void randomlyPopulate() {

    for(int i = 0; i < arrayOfTiles.length; i++) {

        for(int j = 0; j < arrayOfTiles.length; j++) {

            double iRand = Math.random();

            if (iRand <= .44) {

                // Assign an ImageView to the tile and adjust its size
                ImageView view = new ImageView(whiteTileImg);
                view.setFitWidth(90);
                view.setFitHeight(90);

                // Assign a white tile to the array index   
                arrayOfTiles[i][j] = new Tile(TileColor.White, new 
                ToggleButton(null, view));
            }

            if (iRand > .44 && iRand <= .80) {

                // Assign an ImageView to the tile and adjust its size
                ImageView view = new ImageView(blackTileImg);
                view.setFitWidth(90);
                view.setFitHeight(90);

                // Adjust the color of the black tiles
                // Otherwise they are hard to distinguish
                // JavaFX did not take well to my png
                ColorAdjust adjustTileColor = new ColorAdjust();
                adjustTileColor.setBrightness(.14);
                view.setEffect(adjustTileColor);

                // Assign a black tile to the array index   
                arrayOfTiles[i][j] = new Tile(TileColor.Black, new 
                ToggleButton(null, view));
            }

            if (iRand > .80 && iRand <= .92) {

                // Assign an ImageView to the tile and adjust its size
                ImageView view = new ImageView(greenTileImg);
                view.setFitWidth(90);
                view.setFitHeight(90);

                // Adjust the color of the green tiles
                // Otherwise they are hideous
                // JavaFX did not take well to my png
                ColorAdjust adjustTileColor = new ColorAdjust();
                adjustTileColor.setBrightness(-.35);
                adjustTileColor.setSaturation(-.4);
                view.setEffect(adjustTileColor);

                // Assign a green tile to the array index   
                arrayOfTiles[i][j] = new Tile(TileColor.Green, new 
                ToggleButton(null, view));
            }

            if (iRand > .92) {

                // Assign an ImageView to the tile and adjust its size
                ImageView view = new ImageView(redTileImg);
                view.setFitWidth(90);
                view.setFitHeight(90);

                // Assign a red tile to the array index 
                arrayOfTiles[i][j] = new Tile(TileColor.Red, new     
                ToggleButton(null, view));
            }
        }
    }   
}

感谢您的帮助,非常感谢。

-Bagger

1 个答案:

答案 0 :(得分:0)

尝试在ToggleButton

上拨打此电话
setPadding(Insets.EMPTY);