Okay, so I wanted to learn GUI on Java, and on this book I used to have (It's 6 years old), I tried to copy a sample of their TicTacToe.java
Whether the statements used are a bit old, or I need to know some things before going out and trying these stuffs, I really couldn't figure out why nothing appears on the screen.
Here's my code (forgive the odd names and texts):
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FranzTicTacToe extends JFrame implements ActionListener
{
private Container c = getContentPane();
private JButton[][] gameBoard = new JButton[3][3];
private ImageIcon imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
private ImageIcon imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
private String turn = "Aloysius";
boolean isGameOver = false;
public void TicTacToe()
{
c = getContentPane();
gameBoard = new JButton[3][3];
imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
isGameOver = false;
c.setLayout(new GridLayout(3,3));
setTitle("Harambe");
setBounds(250, 250, 300, 300);
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
gameBoard[column][row] = new JButton();
c.add(gameBoard[column][row]);
gameBoard[column][row].addActionListener(this);
}
}
}
public void actionPerformed(ActionEvent e)
{
if(isGameOver)
return;
JButton pressedButton = (JButton)e.getSource();
int pressedColumn = -1, pressedRow = -1;
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row] == pressedButton)
{
pressedColumn = column;
pressedRow = row;
}
}
}
//Game START
if(gameBoard[pressedColumn][pressedRow].getIcon() != null)
{
return;
}
if(turn.equals("Aloysius"))
{
gameBoard[pressedColumn][pressedRow].setIcon(imagex);
turn = "Johpit";
}
else
{
gameBoard[pressedColumn][pressedRow].setIcon(imageo);
turn = "Aloysius";
}
if(checkRow(pressedRow, imageo) || checkColumn(pressedColumn, imageo) || checkDiagonals(imageo))
{
isGameOver = true;
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Aloysius is skrub", "Result", JOptionPane.INFORMATION_MESSAGE);
turn = "Johpit";
playAgain();
}
if(checkRow(pressedRow, imagex) || checkColumn(pressedColumn, imagex) || checkDiagonals(imagex))
{
isGameOver = true;
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Johpit is skrub", "Result", JOptionPane.INFORMATION_MESSAGE);
turn = "Aloysius";
playAgain();
}
if(!isGameOver)
{
boolean tie = true;
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row].getIcon() == null)
{
tie = false;
break;
}
}
}
if(tie)
{
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Both of you are fukin skrubs", "Result", JOptionPane.INFORMATION_MESSAGE);
isGameOver = true;
playAgain();
}
}
if(!isGameOver)
{
setTitle(turn + "'s turn");
}
}
boolean checkRow(int row, Icon player)
{
for(int column=0; column < 3; column++)
{
if(gameBoard[column][row].getIcon() != player)
{
return false;
}
}
return true;
}
boolean checkColumn(int column, Icon player)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row].getIcon() != player)
{
return false;
}
}
return true;
}
boolean checkDiagonals(Icon player)
{
if(gameBoard[1][1].getIcon() != player)
return false;
if(gameBoard[0][0].getIcon() == player && gameBoard[2][2].getIcon() == player)
{
return true;
}
return false;
}
//Game Restarting Option
void playAgain()
{
Object[] options = {"Yes", "No"};
int n = JOptionPane.showOptionDialog(FranzTicTacToe.this, "Pusta again?", "Query", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(n == JOptionPane.YES_OPTION)
{
dispose();
FranzTicTacToe game = new FranzTicTacToe();
game.setVisible(true);
}
else
{
System.exit(0);
}
}
public static void main(String[] args)
{
FranzTicTacToe game = new FranzTicTacToe();
game.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
game.setVisible(true);
}
}
I made "Aloysius" a value for x and "Johpit" a value for o out of curiosity and fun.
Anyway, I'm new to GUI's in Java and I wanted to try this so that I could then learn each part of it slowly. All this code shows is a blank window. Any ideas?
Thank you :)
You are missing the constructor. Right now it uses the default constructor which essentially does nothing. It's probably a spelling error, but for the sake of understanding. If there is no constructor declared it will use the default constructor from Object
class.
A constructor also needs to have the exact name as the class. So change your TicTacToe
method to FranzTicTacToe
constructor
public void TicTacToe() // needs to be public FranzTicTacToe()
{
c = getContentPane();
gameBoard = new JButton[3][3];
imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
isGameOver = false;
c.setLayout(new GridLayout(3,3));
setTitle("Harambe");
setBounds(250, 250, 300, 300);
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
gameBoard[column][row] = new JButton();
c.add(gameBoard[column][row]);
gameBoard[column][row].addActionListener(this);
}
}
}