Hi very New to Java,
struggling with a few fundamental principles. I have to write a noughts and crosses program, but there is a few thing I'm not quite understanding;
why some of my actionPerformed do the same thing?
how to select 'x' or '0' and then progress the game to start here's my code, might i add its incomplete areas highlighted red are the parts i'm struggling with the most , I have just added the things I will eventually need. hope its not too wishy washy. Any help would be appreciated thanks
public class NoughtsAndCrossesGame extends JFrame
{
private final int FRAME_WIDTH = 500;
private final int FRAME_HEIGHT = 500;
private int playingAreaWidth;
private int playingAreaHeight;
private JPanel controlPanel;
private JButton startButton;
private JTextArea instructionsTA;
private JButton xButton;
private JButton oButton;
private PlayingPanel gamePP;
private JMenuBar bar;
private JMenu fileMenu;
private JMenuItem startAgainMenu;
private JMenuItem exitMenu;
private int currentPlayer = 0; // 1 for player 1, 2 for player 2
// if 0 then game hasn't yet started
private String [] playerSymbols; // holds X or O as chosen by players.
private boolean gameUnderWay; // true once play has been pressed - until
// Start again is selected.
protected NoughtsAndCrossesBoard myNoughtsAndCrossesBoard;
protected NoughtsAndCrosses myNoughtsAndCrosses;
/**
* Creates a new instance of NoughtsAndCrossesGame
*/
public NoughtsAndCrossesGame(String title)
{
super(title);
setUpGui();
setUpGame();
}
private void setUpGui()
{
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
controlPanel = new JPanel();
startButton = new JButton("Start");
xButton = new JButton("X");
oButton = new JButton("O");
instructionsTA = new JTextArea("",5,30);
instructionsTA.setEditable(false);
playingAreaWidth = getWidth()-11;
playingAreaHeight = getHeight() - 150;
= new PlayingPanel();
controlPanel.add(instructionsTA);
controlPanel.add(xButton);
controlPanel.add(oButton);
controlPanel.add(startButton);
bar = new JMenuBar();
setJMenuBar(bar);
fileMenu = new JMenu("File");
startAgainMenu = new JMenuItem("Start again");
exitMenu = new JMenuItem("Exit");
bar.add(fileMenu);
fileMenu.add(startAgainMenu);
fileMenu.add(exitMenu);
xButton.addActionListener(new ButtonWatcher());
oButton.addActionListener(new ButtonWatcher());
startButton.addActionListener(new ButtonWatcher());
gamePP.addMouseListener(new MouseEventer());
startAgainMenu.addActionListener(new MenuSelection());
exitMenu.addActionListener(new MenuSelection());
add(controlPanel, BorderLayout.SOUTH);
add(gamePP, BorderLayout.CENTER);
}
private void setUpGame()
{
currentPlayer = 1;
gameUnderWay = false;
playerSymbols = new String [2] ;
myNoughtsAndCrosses = new NoughtsAndCrosses();
myNoughtsAndCrossesBoard = new NoughtsAndCrossesBoard(playingAreaWidth, playingAreaHeight);
setUpButtons();
}
private void setUpButtons()
{
xButton.setEnabled(true);
oButton.setEnabled(true);
xButton.setVisible(true);
oButton.setVisible(true);
startButton.setVisible(false);
startButton.setEnabled(false);
gameUnderWay = false;
instructionsTA.setText("First player - please choose 'O' or 'X'");
}
private class PlayingPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
myNoughtsAndCrossesBoard.paintComponent(g);
}
}
private void startingGameButtons()
{
xButton.setEnabled(false);
oButton.setEnabled(false);
xButton.setVisible(false);
oButton.setVisible(false);
startButton.setEnabled(true);
startButton.setVisible(true);
}
private void playInstructions()
{
instructionsTA.setText("Press Start to begin playing");
}
private class ButtonWatcher implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
Object buttonPressed = a.getSource();
if (buttonPressed.eqauls());
{
instructionsTA.setText("you have chosen" );
}
}
}
private class MouseEventer extends MouseAdapter
{
public void mouseClicked (MouseEvent m)
{
// incomplete
}
}
MenuSelection class
private class MenuSelection implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
Object buttonPressed = a.getSource();
if (buttonPressed == startAgainMenu);
{
// should restart game
}
if (buttonPressed.equals(exitMenu));
{
System.exit(0); }
}
}
}