Reputation: 1
**My JLayeredPane's overlap the way I want them to, but I can't seem to create a transparent background for the top layer.
I'm trying to create a game, called Kamisado, where the board 8x8 has different color grids and figures on top of it.
I created a board and figures in separated classes as JLabels
, then added each of them to separate JLayeredPanes
, which were later added to the main JFrame
"board".
Although you can see layers, the front layer with figures isn't transparent to see the layer with the board behind it.
How can I make a front layer to be transparent to the board behind it?
In separate classes, I create a JLabel
, set Bounds
, Icon
, Visibility(true)
, and Opaque(true)
. For the tile class, I set the background color as well.
public class Board extends JFrame implements MouseListener {
static Container cont;
static Board board;
static Tile[][] tile;
static Monster[][] monster;
static int height = 800;
static int width = 900;
int i = 8;
int j = 8;
static JLayeredPane pTile = new JLayeredPane();
static JLayeredPane pMon = new JLayeredPane();
Board() {
super("Kamisado");
// pane = this.getLayeredPane();
this.setSize(new Dimension(height, width));
this.setLocationRelativeTo(null);
pMon.setLayout(new GridLayout(8, 8));
pTile.setLayout(new GridLayout(8, 8));
tile = new Tile[8][8];
monster = new Monster[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
tilePane(i,j);
monsterPane(i,j);
}
}
this.add(pTile);
this.add(pMon);
// this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board.board = this;
}
static void monsterPane(int i, int j) {
Monster m = addMonster(i, j);
pMon.add(m);
pMon.setVisible(true);
pMon.setOpaque(true);
pMon.repaint();
}
static public Monster addMonster(int i, int j) {
monster[i][j] = new Monster(i, j, board);
return monster[i][j];
}
static void tilePane(int i, int j) {
Tile m = addTile(i, j);
pTile.add(m);
pTile.setVisible(true);
pTile.repaint();
}
static public Tile addTile(int i, int j) {
tile[i][j] = new Tile(i, j, board);
return tile[i][j];
}
Upvotes: 0
Views: 104