플레이어 격자를 나타내는 JButton의 다차원 배열이있는 전함 게임을 만들어야합니다. 기본 UI 클래스에서 JButton을 초기화해야하지만 플레이어 그리드를 보유하는 별도의 클래스에서 선언해야합니다. 단추를 추가하려고 시도했습니다.get 메소드를 사용하여 다른 클래스에 선언 된 JButton의 다차원 배열 초기화
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 10; j++){
playerOnePanel.add(playerOne.getBoard());
}
}
그러나 null 포인터 예외를 반환합니다.
public class Player {
private Color shipColor;
private String userName;
private boolean isFirst;
private JButton[][] buttonBoard = new JButton[rows][cols];
private final static int rows = 10;
private final static int cols = 10;
public Player(String name){
initComponents();
}
private void initComponents(){
for(int i = 0; i <= rows; i++){
for(int j = 0; j <= cols; j++){
buttonBoard[i][j] = new JButton();
}
}
}
public JButton[][] getBoard(){
return buttonBoard;
}
}
내가있는 객체를 사용하고자하는 코드는 다음과 같습니다 : 나는 참조하기 위해 노력하고있어 클래스는이 작업을 수행 할 수있는 방법이
public class BattleshipUI extends JFrame {
private JMenuBar menuBar;
private JMenu gameMenu;
private JMenu optionMenu;
private JMenuItem playerPlayer;
private JMenuItem playerComputer;
private JMenuItem computerComputer;
private JMenuItem exit;
private JMenuItem game;
private JMenuItem player;
private JButton deploy;
private JPanel shipLayoutPanel;
private JPanel playerOnePanel;
private JComboBox shipCb;
private JComboBox directionCb;
// Data arrays for various components on the UI
private String[] rowLetters = {" ","A","B","C","D","E","F","G","H","I","J"};
private String[] columnNumbers = {" ","1","2","3","4","5","6","7","8","9","10"};
private String[] ships = {"Carrier","Battleship","Submarine","Destroyer", "Patrol Boat"};
private String[] direction = {"Horizontal","Vertical"};
private static final int PLAYER_ONE = 0;
private static final int PLAYER_TWO = 1;
private Player playerOne;
private Player playerTwo;
private Player[] players = new Player[2];
private Color[] color = {Color.cyan, Color.green, Color.yellow, Color.magenta, Color.pink, Color.red, Color.white};
public BattleshipUI(){
initComponents();
initObjects();
}
public BattleshipUI getThisParent(){
return this;
}
private void initObjects(){
playerOne = new Player("Player One");
playerTwo = new Player("Player Two");
players[1] = playerOne;
players[2] = playerTwo;
}
private void initComponents(){
this.setTitle("Battleship");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500,500));
this.setMinimumSize(new Dimension(500,500));
menuBar = new JMenuBar();
gameMenu = new JMenu("Game");
optionMenu = new JMenu("Option");
menuBar.add(gameMenu);
menuBar.add(optionMenu);
playerPlayer = new JMenuItem("Player vs. Player");
playerComputer = new JMenuItem("Player vs. Computer");
computerComputer = new JMenuItem("Computer vs. Computer");
exit = new JMenuItem("Exit");
gameMenu.add(playerPlayer);
gameMenu.add(playerComputer);
gameMenu.add(computerComputer);
gameMenu.add(exit);
playerPlayer.setEnabled(false);
computerComputer.setEnabled(false);
game = new JMenuItem("Game Options");
player = new JMenuItem("Player Options");
optionMenu.add(game);
optionMenu.add(player);
shipLayoutPanel = new JPanel();
shipLayoutPanel.setBorder(BorderFactory.createTitledBorder("Select Ship and Direction"));
shipCb = new JComboBox(ships);
directionCb = new JComboBox(direction);
deploy = new JButton("DEPLOY");
deploy.setEnabled(false);
shipLayoutPanel.add(shipCb);
shipLayoutPanel.add(directionCb);
shipLayoutPanel.add(deploy);
playerOnePanel = new JPanel(new GridLayout(11,11));
playerOnePanel.setMinimumSize(new Dimension(400,400));
playerOnePanel.setPreferredSize(new Dimension(400,400));
playerOnePanel.setBorder(BorderFactory.createTitledBorder("Player One"));
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 10; j++){
playerOnePanel.add(playerOne.getBoard());
}
}
this.setJMenuBar(menuBar);
this.add(shipLayoutPanel, BorderLayout.NORTH);
this.add(playerOnePanel, BorderLayout.WEST);
this.setVisible(true);
}
}
있습니까? 나열된 메소드와 변수를 사용해야합니다.
나는 메소드를 호출하는 것에 집중하여, 메소드를 잘못된 순서로 호출했는지 확인하지도 않았다. 당신의 도움을 주셔서 감사합니다! –