'Really Confused On How to Interact With GUI Input
I'm working on a nine men morris project for my CS class and I am not sure on how to interact with the GUI input via multiple classes. I have a morris board class that set ups the board with an actionListener that sets the color of the button when clicked, and I have a main game class that sets up the board. I'm not really sure how to articulate my question but basically I want to extend the actionListener to check if any mills are formed or what phase the game is currently in. I assumed the object-oriented approach to this would be putting a checkMillFormed() and checkPhase() method in the main game class as I want to make the board as dumb as possible, but I really not sure on how to go about doing this.
public class GameGUI extends JFrame implements MorrisGameListener {
private MorrisBoardGUI Morrisboard;
public GameGUI() {
this.setUpDisplay();
}
private void setUpDisplay() {
this.setSize(600, 700);
this.setResizable(false);
this.Morrisboard = new MorrisBoardGUI();
this.add( this.Morrisboard );
}
public boolean checkMillFormed(int row, int column) {
}
public boolean checkPhase() {
}
}
public class MorrisBoardGUI extends JPanel implements ActionListener {
private JButton[][] board;
private boolean player1;
private int index;
private JButton button7a;
private JButton button7d;
private JButton button7g;
private JButton button6b;
private JButton button6d;
private JButton button6f;
private JButton button5c;
private JButton button5d;
private JButton button5e;
private JButton button4a;
private JButton button4b;
private JButton button4c;
private JButton button4e;
private JButton button4f;
private JButton button4g;
private JButton button3c;
private JButton button3d;
private JButton button3e;
private JButton button2b;
private JButton button2d;
private JButton button2f;
private JButton button1a;
private JButton button1d;
private JButton button1g;
private JButton newGameButton;
private JButton quitButton;
public MorrisBoardGUI() {
player1 = true;
this.createBoard();
this.board = new JButton[7][7];
board[0] = new JButton[] {button7a, null, null, button7d, null, null, button7g};
board[1] = new JButton[] {null, button6b, null, button6d, null, button6f, null};
board[2] = new JButton[] {null, null, button5c, button5d, button5e, null, null};
board[3] = new JButton[] {button4a, button4b, button4c, null, button4e, button4f, button4g};
board[4] = new JButton[] {null, null, button3c, button3d, button3e, null, null};
board[5] = new JButton[] {null, button2b, null, button2d, null, button2f, null};
board[6] = new JButton[] {button1a, null, null, button1d, null, null, button1g};
}
private JButton createPoint(int x, int y) {
JButton button = new JButton();
button.setBounds(x, y, 20, 20);
button.setBackground(Color.LIGHT_GRAY);
this.add(button);
button.addActionListener(this);
return button;
}
private void createBoard() {
this.setBackground(new Color(250, 247, 158));
this.setSize(500, 500);
this.setLayout(null);
button7a = createPoint(50, 50);
button7d = createPoint(275, 50);
button7g = createPoint(500, 50);
button6b = createPoint(105, 150);
button6d = createPoint(275, 150);
button6f = createPoint(445, 150);
button5c = createPoint(200, 225);
button5d = createPoint(275, 225);
button5e = createPoint(350, 225);
button4a = createPoint(50, 275);
button4b = createPoint(105, 275);
button4c = createPoint(200, 275);
button4e = createPoint(350, 275);
button4f = createPoint(445, 275);
button4g = createPoint(500, 275);
button3c = createPoint(200, 325);
button3d = createPoint(275, 325);
button3e = createPoint(350, 325);
button2b = createPoint(105, 400);
button2d = createPoint(275, 400);
button2f = createPoint(445, 400);
button1a = createPoint(50, 500);
button1d = createPoint(275, 500);
button1g = createPoint(500, 500);
newGameButton = new JButton("NEW");
newGameButton.setBounds(235, 600, 50, 50);
newGameButton.setMargin( new Insets( 0, 0, 0, 0) );
this.add(newGameButton);
quitButton = new JButton("QUIT");
quitButton.setBounds(285, 600, 50, 50);
quitButton.setMargin( new Insets(0, 0, 0, 0));
this.add(quitButton);
}
public JButton[][] getBoard() {
return this.board;
}
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(60, 60, 450, 450);
g.drawRect(115, 160, 340, 250);
g.drawRect(210, 235, 150, 100);
g.drawLine(50, 285, 200, 285);
g.drawLine(350, 285, 500, 285);
g.drawLine(285, 60, 285, 235);
g.drawLine(285, 335, 285, 500);
}
@Override public void actionPerformed(ActionEvent e) {
for(int r = 0; r < this.board.length; r++) {
for(int c = 0; c < this.board[r].length; c++) {
JButton clickedButton = this.board[r][c];
if(clickedButton == e.getSource() && player1 == false) {
if(clickedButton.getBackground() == Color.LIGHT_GRAY) {
clickedButton.setBackground(Color.WHITE);
}
player1 = true;
}
else if(clickedButton == e.getSource() && player1 == true) {
if(clickedButton.getBackground() == Color.LIGHT_GRAY) {
clickedButton.setBackground(Color.BLACK);
}
player1 = false;
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
