別のJPanelの上にJpanelを表示しようとしていますが、下のパネルは非アクティブになっていますが、上のパネルに画像を配置したり描画したりできます。私が試してみました
setOpaque(false);
そして私は試しました
Color transparent = new Color(0,0,0,0);
パネルの2次元配列を描画するBoardクラスがあります。次に、パネルの2次元配列を描画するPlayerBoardクラスがあります。これは、グラフの表現でもあります。PlayerBoard透明にしたいので、ボードクラスを上書き/編集せずにPlayerBoardクラスにプレーヤーを追加できます。
ボードクラスのコードは次のとおりです。
package project;
import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Board extends JPanel {
private JPanel boardPanel;
private Grid[][] grid;
public Board(int x, int y){
//Create a new Player
//Create a Panel to hold the board
boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(x,y,2,2));
//Create a grid to hold each panel square
grid = new Grid[x][y];
for(int row = 0; row < 6; row++){
for(int column = 0; column < 6; column++){
grid[row][column] = new Grid(x,y,true, Color.BLACK);
if((row==5 && column==5) || (row==4 && column==4) || (row==4 && column==5) || (row==5 && column==4) || (row==1 && column==2) || (row==1 && column==3) || (row==0 && column==2) || (row==0 && column==3) || (row==4 && column==0) || (row==5 && column==0) || (row==5 && column==1) || (row==4 && column==1)){
grid[row][column].changeColor(Color.YELLOW);
}
boardPanel.add(grid[row][column]);
}
}
//add boardPanel to Board
add(boardPanel);
}
}
これがPlayerBoardのクラスです
package project;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JPanel;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
@SuppressWarnings("serial")
public class PlayerBoard extends JPanel{
private JPanel boardPanel;
private Grid[][] board;
//private Player steve;
public PlayerBoard(int x, int y){
UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
//Vertex of Graph
String vertex = "";
//Create a 2-d array of strings to hold the grid of the borad
board = new Grid[x][y];
//Create the boardPanel
boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(x,y,2,2));
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
if(i==0){
vertex = Integer.toString(j+1);
board[i][j] = new Grid(i,j,true, new Color(0,0,0,0));
//board[i][j].changeColor(new Color(0,0,0,0));
g.addVertex(vertex);
}
else{
vertex = Integer.toString(((x*i)+(j+1)));
board[i][j] = new Grid(i,j,true,new Color(0,0,0,0));
//board[i][j].changeColor(new Color(0,0,0,0));
g.addVertex(vertex);
}
boardPanel.add(board[i][j]);
}
}
add(boardPanel);
//Add the edges for the board graph
g.addEdge("1", "2");
g.addEdge("1", "7");
g.addEdge("2", "8");
g.addEdge("5", "11");
g.addEdge("5", "6");
g.addEdge("6", "12");
g.addEdge("7", "13");
g.addEdge("7", "8");
g.addEdge("8", "14");
g.addEdge("11", "17");
g.addEdge("11", "12");
g.addEdge("12", "18");
g.addEdge("13", "14");
g.addEdge("13", "19");
g.addEdge("14", "20");
g.addEdge("14", "15");
g.addEdge("15", "21");
g.addEdge("15", "16");
g.addEdge("16", "17");
g.addEdge("16", "22");
g.addEdge("17", "23");
g.addEdge("17", "18");
g.addEdge("18", "24");
g.addEdge("19", "20");
g.addEdge("20", "21");
g.addEdge("21", "22");
g.addEdge("21", "27");
g.addEdge("22", "28");
g.addEdge("22", "23");
g.addEdge("23", "24");
g.addEdge("27", "28");
g.addEdge("27", "33");
g.addEdge("28", "34");
/*
* Print out the board
*/
for(int i1 = 0; i1 < 6; i1++){
for(int j = 0; j < 6; j++){
System.out.print(board[i1][j]);
}
System.out.println();
}
System.out.println(g.toString());
//steve = new Player("1", "bin/stuff/images/player.png");
//add(steve);
setOpaque(false);
}
public UndirectedGraph<String, DefaultEdge> getGraph(){
return null;
}
}
これが、PlayerBoardをBoardクラスの上に配置したい私のゲームクラスです。このようにして、ボードクラスを台無しにすることなく、PlayerBoardクラスに画像を配置できます。
package project;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class Game extends JFrame implements MouseListener{
private PlayerBoard playerboard;
private Board board;
private Side side;
private JPanel boardPanel, playerPanel;
public Game(){
Color transparent = new Color (0, 0, 0, 0);
//create a boardPanel
boardPanel = new JPanel();
boardPanel.setLayout(new BorderLayout());
playerPanel = new JPanel();
playerPanel.setLayout(new BorderLayout());
//Create a board that is x,y
board = new Board(6,6);
playerboard = new PlayerBoard(6,6);
playerboard.setOpaque(false);
playerPanel.add(playerboard);
playerPanel.setBackground(transparent);
//Create a Side Panel
side = new Side();
//add the side and board to Game
boardPanel.add(board, BorderLayout.CENTER);
playerPanel.add(playerboard, BorderLayout.CENTER);
playerPanel.setOpaque(false);
getContentPane().setLayout(new BorderLayout());
getContentPane().setBackground(Color.BLUE);
getContentPane().add(side, BorderLayout.EAST);
getContentPane().add(boardPanel,BorderLayout.CENTER);
getContentPane().add(playerPanel, BorderLayout.CENTER);
pack();
//Important
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
}
}
事前に助けてくれてありがとう