I wish to create a simple TicTacToe GUI Game by using Swing;
I created JFrame called TicTacToe
and put JPanel in it called gamePanel
with GridLayout(3,3)
. Then I created a button array called buttons[][]
for my buttons, in which I'll put icon of X
or O
. Then I generated 9 buttons and
added logic. JPanel code:
public class gamePanel extends JPanel {
XOButton buttons[][] = new XOButton[3][3];
boolean isX;
public gamePanel() {
setLayout(new GridLayout(3, 3));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++) {
buttons[i][j] = new XOButton();
buttons[i][j].setIcon(null);
add(buttons[i][j]);
}
}
isX = true;
}
private class XOButton extends JButton {
ImageIcon X, O;
public XOButton() {
X = new ImageIcon(gamePanel.this.getClass().getResource("X.png"));
O = new ImageIcon(gamePanel.this.getClass().getResource("O.png"));
addActionListener(this::actionPerformed);
}
public int getValue(XOButton btn){
if(btn.getIcon().toString().equals(null)){
return 0;
}
if(btn.getIcon().toString().equals(X.toString())){
return 1;
}
if(btn.getIcon().toString().equals(O.toString())){
return 2;
}
return 0;
}
public void actionPerformed(ActionEvent e) {
if(getIcon() != null) return;
if (isX) {
setIcon(X);
isX = false;
checkWinner();
} else {
setIcon(O);
isX = true;
checkWinner();
}
}
public void checkWinner(){
for(int i = 0; i < 3; i++){
if(getValue(buttons[i][0]) != 0 && getValue(buttons[i][0]) == getValue(buttons[i][1])
&& getValue(buttons[i][1]) == (getValue(buttons[i][2]))) {
if (getValue(buttons[i][0]) == 1) {
JOptionPane.showMessageDialog(null, X);
return;
} else if (getValue(buttons[i][0]) == 2) {
JOptionPane.showMessageDialog(null, O);
return;
}
}
}
}
}
}
I was wondering if I can get an answer for the problem (please!):
As you see, i created procedure called checkWinner
and yet it is only checking if button icons are equal in one row. But when I use this code, it works properly only for first row. The second and third are ignoring until I will fill the first two buttons of first row. When code works, it should show message with an image of X or O (depends on who is winner).
Image where it works fine in the first row and
Image where it is not working (until I will fill first row for a half)
When I'll fill first row in second image it will display both two messageDialogs
with X
and O
image.
What am I doing wrong? I'll also appreciate answers and comments about my code, it's terrible and I want to improve my skills. Thanks for paying attention.
Edit: I get this exception when pressing any of the button:
`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gamePanel$XOButton.getValue(gamePanel.java:34)
at gamePanel$XOButton.checkWinner(gamePanel.java:61)
at gamePanel$XOButton.actionPerformed(gamePanel.java:51)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6589)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6354)
at java.desktop/java.awt.Container.processEvent(Container.java:2260)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2318)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4906)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2304)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
`