【发布时间】:2017-03-24 04:37:37
【问题描述】:
我有一个随机生成的彩色图块数组,我将它们添加到游戏的网格窗格中。到目前为止,除了作为切换按钮的图块之外,实际上还没有任何功能。这是一个 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));
}
}
}
}
感谢您的帮助,非常感谢。
-巴格
【问题讨论】:
标签: java arrays image button javafx