Results 1 to 11 of 11
  1. #1
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default Clear/Reset Button Problem

    Alright this is the last piece of the puzzle for my program to work. Straight to the point im trying to create a button so when it is it clicked it resets or clears the game so i can start a new one. I have the button all set up but there is no action involved. I have some ideas that might make it work, i dont know if i need to call a certain variable or class to = true; so it does reset. But any help would be appreciated. Here's the full code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import java.awt.Color;
    
    public class TicTacToe1 extends JApplet implements ActionListener{
    
    
    	private char whoseTurn = 'X';// Shows which player has a turn
    	private Cell[][] cells = new Cell[3][3];// Create the cells here
    	private JLabel jlblStatus = new JLabel("X's turn to play");// Create and initialize a status label
    	private JFrame window = new JFrame("Tic-Tac-Toe");
    
    
    public TicTacToe1() { // Initialize User Interface)
    
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
        for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            p.add(cells[i][j] = new Cell());
    
    p.setBorder(new LineBorder(Color.black, 1));  // Set line borders on the cells panel and the status label
        jlblStatus.setBorder(new LineBorder(Color.black, 1));
        add(p, BorderLayout.CENTER);//position the Border Layout
        add(jlblStatus, BorderLayout.SOUTH);
    
    
    JMenuBar menuBar = new JMenuBar();//Drop down menu (File)
      setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
           menuBar.add(fileMenu);
    
    JMenuItem exitAction = new JMenuItem("Exit");//Exit item
      fileMenu.add(exitAction);
        exitAction.setActionCommand("Exit");
          exitAction.addActionListener(this);
    
    JMenuItem aboutAction =  new JMenuItem("About");//About item
      fileMenu.add(aboutAction);
        aboutAction.setActionCommand("About");
          aboutAction.addActionListener(this);
    
    JMenuItem clearAction = new JMenuItem("Clear");//Clear Item
    	fileMenu.add(clearAction);
         clearAction.setActionCommand("Clear");
          clearAction.addActionListener(this);
    
    }
      public void actionPerformed(ActionEvent e){//When action is clicked the app will exit
         String arg = e.getActionCommand();{
         if (arg.equals("Exit")){
         System.exit(0);
    }
    
    if (arg.equals("Clear"))
    
    
    
    {
    
    
    }
    
    if (arg == "About"){
        String message = "Ryan's TicTacToe \nStructured Programming\n2011";
        JOptionPane.showMessageDialog(null,message,
        "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);//About message when "about" is clicked
        }
      }
    }
    
    
    public boolean isFull() {  /** Determine if the cells are all occupied */
       for (int i = 0; i < 3; i++)
       for (int j = 0; j < 3; j++)
       if (cells[i][j].getToken() == ' ')
       return false;
       return true;
    }
    
    public boolean isWon(char token) {  /** Determines if the player with the specified token wins*/
    
       for (int i = 0; i < 3; i++)
         if ((cells[i][0].getToken() == token)
         && (cells[i][1].getToken() == token)
         && (cells[i][2].getToken() == token)) {
         return true;
    }
       for (int j = 0; j < 3; j++)
         if ((cells[0][j].getToken() == token)
         && (cells[1][j].getToken() == token)
         && (cells[2][j].getToken() == token)) {
         return true;
    }
         if ((cells[0][0].getToken() == token)
         && (cells[1][1].getToken() == token)
         && (cells[2][2].getToken() == token)) {
         return true;
    }
         if ((cells[0][2].getToken() == token)
         && (cells[1][1].getToken() == token)
         && (cells[2][0].getToken() == token)) {
         return true;
    }
         return false;
    }
    
    
    public class Cell extends JPanel { // An inner class for a cell//// Token used for this cell
         private char token = ' ';
    
    
    public Cell() {
    
    	setBorder(new LineBorder(Color.red, 1)); // Set cell's border
    	addMouseListener(new MouseListener()); // Register listener
    }
    
    
    public char getToken() {  // Return token
        return token;
    
    }
    
    
    public void setToken(char c) { // Set a new token
        token = c;
        repaint();
    
    }
    
    protected void paintComponent(Graphics g) {  // Paint the cell
    super.paintComponent(g);
    
    if (token == 'X') {
       g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
       g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
    }
    
    else if (token == 'O') {
       g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
    
     }
    }
    
    
    private class MouseListener extends MouseAdapter {/// Handle mouse click on a cell
    public void mouseClicked(MouseEvent e) {
    
    if (token == ' ' && whoseTurn != ' ') {  // If cell is empty and game is not over
        setToken(whoseTurn); // Set token in the cell
    
    if (isWon(whoseTurn)) { // Check game status
        jlblStatus.setText(whoseTurn + " won! The game is over");
        whoseTurn = ' '; // Game is over
    }
    else if (isFull()) {
         jlblStatus.setText("Draw! The game is over");
         whoseTurn = ' '; // Game is over
    }
    else {
         whoseTurn = (whoseTurn == 'X') ? 'O': 'X'; // Change the turn
         jlblStatus.setText(whoseTurn + "'s turn"); // Display whose turn
    
            }
          }
        }
      }
    }
    
    public static void main(String[] args) {
    // Create a frame
         JFrame frame = new JFrame("TicTacToe");
           TicTacToe1 applet = new TicTacToe1();
         frame.add(applet, BorderLayout.CENTER);
    
    // Display the frame
          frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
       }
    }

  2. #2
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    it cant be that hard really..but i just can't see the simple concept
    Last edited by Ryan10; 04-11-2011 at 07:38 PM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    What is "............" for?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I do hope it isn't a form of *bump*, as that would imply the OP was expecting a reply within 30 minutes.

  5. #5
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    just seeing if anyone has any ideas to this problem

  6. #6
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    can anyone help me with this random problem

  7. #7
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    anyoneeeeeeee??

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    This place works better if you try something and then ask for help if there is a problem.
    It doesn't work so well if you are asking for code suggestions.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am confused. I believe we helped you out quite a bit in a previous thread. What errors are you running into? etc, etc.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In case you didn't quite understand everything I would love to give you a quick summary of some important things.

    In the previous thread you set up the board in the constructor. The board is initially set to contain empty panels. Since clear is doing the same thing(setting up blank panels) Junky suggested you write a method in the class to do this setup for you. This method would set the panels to be "fresh" and draw anything you need on them to be initialized with. If they have borders, draw the borders in this method. Anything you want the board to look like on start up should be done in this method.

    This method should be separate from anything else.
    Java Code:
    public class TicTacToe{
      do stuff
      public TicTacToe(){
        do stuff
        set up board method call
      }
      setUpBoardMethod(){
        do board set up call stuff
      }
    }
    So now you made a method to do this for you. Any time you wanted to set the board to look new(be cleared, initialized, etc) you can just call this method.

    Now you want an action listener on the button object. You went with having the main tictactoe class implement actionListener so it should look like this to add the listener

    Java Code:
    JButton clear = new JButton("Clear");
    clear.addActionListener(this);
    Since you used the keyword "this" it will use the actionPerformed method in this class(this is all stuff you had already, but I am explaining it again)

    Now inside of the public method actionPerformed, you want to do stuff based on the ActionEvents action word.

    You captured the Action word correctly, and then you compare it to string values. If the action word matches the string "Clear" you would like to clear the board to the original setting.

    Wouldn't it be handy to have a method which clears and sets up the board for you? If you do have one you want to call it now

    Java Code:
    public void actionPerformed(ActionEvent e){
      capture action word
      if(action word equals "blah"){
        do blah
      }
      else if(action word equals "clear"){
        set the board up to look as it initially di
      }
      do other stuff
    }
    I hope this helps you out. How is "head first java" treating you?

  11. #11
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    Havent gotten the book yet..its almost here...anyways when you mean capture action word do i have to write out the whole line: clear.addActionListener(this); ?

Similar Threads

  1. Radio Buttons - Reset - clear dot
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 06-11-2014, 06:40 AM
  2. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 05:21 PM
  3. Reset form button and duplicated data
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 04:40 AM
  4. <html:reset> button in struts
    By Satheeshhere in forum Web Frameworks
    Replies: 1
    Last Post: 07-15-2008, 03:14 PM
  5. in struts reset was problem
    By sureshBabu in forum Web Frameworks
    Replies: 1
    Last Post: 05-07-2008, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •