Page 1 of 4 123 ... LastLast
Results 1 to 20 of 74
  1. #1
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default Solving Clear Button

    I have a tic tac toe game all setup with no errors and everything works. What im stuck on is the clear button which is meant clear the board so i can start a new game over. I've seen examples of these but can't seem to fit it into my code. Here are a couple of snippets from my code if you guys need more feel free to post.

    Java Code:
    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);
    Java Code:
    }
      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
        }
      }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    And your problem is?

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

    Default

    The button appears fine in the menu bar but when clicked nothing happens

  4. #4
    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 can't be sure what the board is made up of, so I can't really give you much advice. If it is an array of something, loop through the array and clear each item(whether it's a label, button, etc)

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

    Default

    maybe this will help :)

    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);
       }
    }

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

    Default

    Loop through the array of cells, and clear each cell in the array.

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

    Default

    is there any easy way of doing that..or is it going to take alot of lines? can you give me an example :)

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Just create a new array.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            p.add(cells[i][j] = new Cell());
    Move that into a method and call it whenever you need a new board.

  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

    creating a new array and repainting the panel works as well.

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

    Default

    so junky if i used your example would i have to repaint as well...or would re painting me much easier

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

    Default

    Easiest way to check is to try it out.

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

    Default

    If i put that example under my public class TicTacToe1 then how would i call it? What would i put underneath this to call the method.

    Java Code:
    if (arg.equals("Clear"))

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Oh come on. You know how to call methods.

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

    Default

    Java Code:
    if(arg.equals("Clear")){
      loop
        loop
          set each item in array to be a new cell
        end inner loop
      end loop
    end if

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

    Default

    if i just leave it where it is can i do something like this: ? I dont know if the name "Cell" is right in this situation

    clear = TicTacToe1.Cell('');

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

    Default

    That makes no sense. Did you try adding junky's code into the action listener?

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

    Default

    Like this

    Java Code:
    if (arg.equals("Clear")){
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());

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

    Default

    ya, what happens when you run it?

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    NO, don't add it to the action listener. Create a separate method. The actionlistener calls the method.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Clear/Reset Button Problem
    By Ryan10 in forum New To Java
    Replies: 10
    Last Post: 04-12-2011, 03:04 PM
  2. Solving a maze in java
    By kuro in forum New To Java
    Replies: 4
    Last Post: 04-11-2011, 11:41 PM
  3. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 05:21 PM
  4. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 12:02 AM
  5. I need help solving this problem.
    By Felicia in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 08:40 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
  •