Results 1 to 8 of 8
  1. #1
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Noughts And Crosses v2 problem with running

    Hi i just wonder i have already done the code. when i try to run it it show the gui blank which its doesnt have game. its another make it way to find the solution.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     *
     * @author 000621812
     */
    
    public class NoughtsAndCrossesGamev2 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            NoughtsAndCrossesGameFrame noughtsAndCrossesGame;
    
            noughtsAndCrossesGame = new NoughtsAndCrossesGameFrame();
            noughtsAndCrossesGame.setVisible(true);
    
    
        }
    }
    
    class NoughtsAndCrossesGameFrame extends JFrame {
    
        NoughtsAndCrossesGamePanel gamePanel;
        NoughtsAndCrossesButtonPanel buttonPanel;
        private ButtonPlayActionHandle playHandler;
        private ButtonExitActionHandler exitHandler;
        private JButton playButton;
        private JButton exitButton;
    
    
    
        
    
        public NoughtsAndCrossesGameFrame() {
            
           gamePanel = new NoughtsAndCrossesGamePanel();
           buttonPanel = new NoughtsAndCrossesButtonPanel();
           
           
          
                this.setLayout(new BorderLayout());
                this.setLayout(new GridLayout(3, 0));
                this.setSize(200, 200);
                this.setVisible(true);
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setTitle("Noughts and Crosses");
    
                    
           
                   
                    
                    
        }
    
        /**
    * Asks the panel containing the game to reset the game.
    */
    
        private void resetGame() {
    
    
            
            //**
    //* The button panel
    //*/
        }
    
        class NoughtsAndCrossesButtonPanel extends JPanel {
        
          /**
    * The game panel
           *
    */ public NoughtsAndCrossesButtonPanel() {
             playButton=new JButton("Play");
             playHandler=new ButtonPlayActionHandle();
    	 playButton.addActionListener(playHandler);
    
             exitButton =new JButton("Exit");
             exitHandler=new ButtonExitActionHandler();
             exitButton .addActionListener(exitHandler);
             this.add(playButton);
             this.add(exitButton);
    
    }
    
    
    
    
    
    
    
    
    
    
        }
        }
    
        class NoughtsAndCrossesGamePanel extends JPanel {
    
           private int clickCount = 0;
        private JButton[] btnKeypad = new JButton[9];
            
    
            public NoughtsAndCrossesGamePanel() {
                /**
    * Resets the buttons and clicjCount ready for a new game
    */
                
                HandlePlayerShot shotHandler = new HandlePlayerShot();
                // Set some JFrame details
              
          
                
         
                // NOTE - In grid layout you provide the number of
                // rows and columns but columns are ignored and always set equal to rows
                // unless the rows are zero.
                //TODO
                //this.setLayout(.....);
                
    
                // Create the buttons and add them  to the JFrame
                for (int i = 0; i < 9; i++) {
                    btnKeypad[i] = new JButton(" ");
                    this.add(btnKeypad[i]);
                    btnKeypad[i].addActionListener(shotHandler);
    
                    //TODO
                }
    
                // Register the same listener for each button as the action needed is always the same.
                // Note - this could have been done in the above loop but is shown separately here to make
                // it clearer.
                shotHandler = new HandlePlayerShot();
    
                this.setSize(250, 250);
    
            }
    
            private void resetGame() {
               /**
    * Disables all the buttons when the game ends
                *
    */
                clickCount = 0;
                for (int i = 0; i < 9; i++){
                    btnKeypad[i].setText("");
                }
    
            }
    
            private void gameFinished() {
                /**
    * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
    * ie when a game button is pressed.
    */
            }
    
            class HandlePlayerShot implements ActionListener { //TODO  implements ????
                
                
    /**
    * Inner class of NoughtsAndCrossesGameFrame to respond to the Exit Button being
    * pressed. Dispose of the frame and exits the application
    */
                @Override
                /**
                 * Responds to the button press - ie player shot
                 */
                public void actionPerformed(ActionEvent e) {
                    JButton currentButton;
                    String winner;
    
                    clickCount++;
                    currentButton = (JButton) (e.getSource());
                    // If it is an odd click count (button press count) then it is "X"
                    // shot as "X" player always goes first
                    if (clickCount % 2 == 1) {
                        currentButton.setText("X");
                    } else {
                        currentButton.setText("0");
                    }
                    currentButton.setEnabled(false);
                    winner = checkForWinner();
                    if (winner != null) {
                        //Display a message showing who the winner is
                        //TODO
                        JOptionPane.showMessageDialog(null, "The winner is " + winner);
    
                       //NoughtsAndCrossesGamePanel.this.dispose();
                    } else {
                        // check that all possible shots have been had
                        if (clickCount == 9) {
                            //Display a message saying it is a drawn game
                            //TODO
                            JOptionPane.showMessageDialog(null, "Draw!");
                            //NoughtsAndCrossesGamePanel.this.dispose();
                        }
                    }
                }
    
                /**
                 * Checks if the button in btnKeypad array in positions i, j, k
                 * all have the same text.
                 * @param i - button number
                 * @param j - button number
                 * @param k - button number
                 * @return true if button i,j,k
                 */
                private boolean isAWinner(int i, int j, int k) {
                    return !btnKeypad[i].getText().equals(" ")
                            && btnKeypad[i].getText().equals(btnKeypad[j].getText())
                            && btnKeypad[i].getText().equals(btnKeypad[k].getText());
                }
    
                /**
                 * Checks for all possible winning scenarios
                 * @return null if there is no winner or the text on the button of the
                 * winner
                 */
                public String checkForWinner() {
                    /* Assumed mapping of button indexes to position in game is -
                     * 	012
                     * 	345
                     * 	678
                     */
                    int winner = -1; // -1 value means there was no winner
    
                    if (isAWinner(0, 1, 2)) {// Horizontal row 1
                        winner = 0;
                    } else if (isAWinner(3, 4, 5)) {	// Horizontal row 2
                        winner = 3;
                    } else if (isAWinner(6, 7, 8)) {	// Horizontal row 3
                        winner = 6;
                    } else if (isAWinner(0, 3, 6)) {	// Vertical row 1
                        winner = 0;
                    } else if (isAWinner(1, 4, 7)) {	// Vertical row 2
                        winner = 1;
                    } else if (isAWinner(2, 5, 8)) {	// Vertical row 3
                        winner = 2;
                    } else if (isAWinner(0, 4, 8)) {	// Diagonal 1
                        winner = 0;
                    } else if (isAWinner(2, 4, 6)) {	// Diagonal 2
                        winner = 2;
                    }
    
                    if (winner != -1) {
                        return btnKeypad[winner].getText();
                    } else {
                        return null;
                    }
                }
            }
    }
    
            class ButtonExitActionHandler implements ActionListener {
                
                /**
    * Inner class of NoughtsAndCrossesGameFrame to respond to the Play Button being
    * pressed. Asks the frame to reset the game by calling the frames resetGame
    * method
    */
    
                
                public void actionPerformed(ActionEvent e)
    		{
                    
    			System.exit(0);
    
    		}
               
            }
    
            class ButtonPlayActionHandle implements ActionListener {
                public void actionPerformed(ActionEvent e)
    		{
    			//resetGame();
    		}
            }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Noughts And Crosses v2 problem with running

    Inside your NoughtsAndCrossesGameFrame constructor you create the two JPanels but you don't add() them to the Frame.
    Please do not ask for code as refusal often offends.

  3. #3
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 problem with running

    meaning i have to add the code for two JPanels

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

    Default Re: Noughts And Crosses v2 problem with running

    No, I mean you haven't added them to the JFrame, at least as far as I can see.
    You create the JFrame, and the two JPanels, but I can't see where you add the panels to the frame itself.

    For example, inside the JPanels you add Jbuttons to the panels:
    Java Code:
    this.add(playButton);
    You need to do a similar thing in the JFrame to add those Panels to the Frame.
    Please do not ask for code as refusal often offends.

  5. #5
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 problem with running

    can you do as a example i kinda lost

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Noughts And Crosses v2 problem with running

    See my sig.
    You've written all the above, including the panels, so you should be able to add() the panels to the frame.
    Please do not ask for code as refusal often offends.

  7. #7
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 problem with running

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     *
     * @author 000621812
     */
    
    public class NoughtsAndCrossesGamev2 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            NoughtsAndCrossesGameFrame noughtsAndCrossesGame;
    
            noughtsAndCrossesGame = new NoughtsAndCrossesGameFrame();
            noughtsAndCrossesGame.setVisible(true);
    
    
        }
    }
    
    class NoughtsAndCrossesGameFrame extends JFrame {
    
        NoughtsAndCrossesGamePanel gamePanel;
        NoughtsAndCrossesButtonPanel buttonPanel;
        private ButtonPlayActionHandle playHandler;
        private ButtonExitActionHandler exitHandler;
        private JButton playButton;
        private JButton exitButton;
       
    
    
    
        
    
        public NoughtsAndCrossesGameFrame() {
            
           gamePanel = new NoughtsAndCrossesGamePanel();
           buttonPanel = new NoughtsAndCrossesButtonPanel();
           
                
                this.setLayout(new BorderLayout());
                this.setLayout(new GridLayout(4,3));
                this.setSize(450, 600);
                this.setVisible(true);
           
           
                this.setTitle("Noughts and Crosses");
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                 
                   this.add(playButton);
                this.add(exitButton);
                 
                   this.add(gamePanel);
                this.add(buttonPanel);
                
                resetGame();
          
             
             
             
                 
    
                    
           
                   
                    
                    
        }
    
        /**
    * Asks the panel containing the game to reset the game.
    */
    
        private void resetGame() {
    
           setVisible(true);
           
    
            
            //**
    //* The button panel
    //*/
        }
    
        class NoughtsAndCrossesButtonPanel extends JPanel {
        
          /**
    * The game panel
           *
    */ public NoughtsAndCrossesButtonPanel() {
             playButton=new JButton("Play");
             playHandler=new ButtonPlayActionHandle();
    	 playButton.addActionListener(playHandler);
    
             exitButton =new JButton("Exit");
             exitHandler=new ButtonExitActionHandler();
             exitButton .addActionListener(exitHandler);
             
             
            
             
             
             
    
    }
    
    
    
    
    
    
    
    
    
    
        }
        }
    
        class NoughtsAndCrossesGamePanel extends JPanel {
    
           private int clickCount = 0;
        private JButton[] btnKeypad = new JButton[9];
        
       
            
    
            public NoughtsAndCrossesGamePanel() {
                /**
    * Resets the buttons and clicjCount ready for a new game
    */
                
                HandlePlayerShot shotHandler = new HandlePlayerShot();
                // Set some JFrame details
              
             
                
         
                // NOTE - In grid layout you provide the number of
                // rows and columns but columns are ignored and always set equal to rows
                // unless the rows are zero.
                //TODO
                
                
                
               
                
                
    
                // Create the buttons and add them  to the JFrame
                for (int i = 0; i < 9; i++) {
                    btnKeypad[i] = new JButton(" ");
                    this.add(btnKeypad[i]);
                    btnKeypad[i].addActionListener(shotHandler);
                    
    
                    //TODO
                }
    
                // Register the same listener for each button as the action needed is always the same.
                // Note - this could have been done in the above loop but is shown separately here to make
                // it clearer.
                //shotHandler = new HandlePlayerShot();
              
              
    
                this.setSize(450, 600);
                this.setLayout(new BorderLayout());
                this.setLayout(new GridLayout(4,3));
            
                this.setVisible(true);
                
                
                
    
            }
    
            private void resetGame() {
               /**
    * Disables all the buttons when the game ends
                *
    */
                clickCount = 0;
                for (int i = 0; i < 9; i++){
                    char ch=(char)('0'+i+1);
                    btnKeypad[i].setText(""+ch);
                }
    
            }
    
            private void gameFinished() {
                /**
    * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
    * ie when a game button is pressed.
    */
            }
    
            class HandlePlayerShot implements ActionListener { //TODO  implements ????
                
                
    /**
    * Inner class of NoughtsAndCrossesGameFrame to respond to the Exit Button being
    * pressed. Dispose of the frame and exits the application
    */
                @Override
                /**
                 * Responds to the button press - ie player shot
                 */
                public void actionPerformed(ActionEvent e) {
                    JButton currentButton;
                    String winner;
    
                    clickCount++;
                    currentButton = (JButton) (e.getSource());
                    // If it is an odd click count (button press count) then it is "X"
                    // shot as "X" player always goes first
                    if (clickCount % 2 == 1) {
                        currentButton.setText("X");
                    } else {
                        currentButton.setText("0");
                    }
                    currentButton.setEnabled(false);
                    winner = checkForWinner();
                    if (winner != null) {
                        //Display a message showing who the winner is
                        //TODO
                        JOptionPane.showMessageDialog(null, "The winner is " + winner);
    
                       //NoughtsAndCrossesGamePanel.this.dispose();
                    } else {
                        // check that all possible shots have been had
                        if (clickCount == 9) {
                            //Display a message saying it is a drawn game
                            //TODO
                            JOptionPane.showMessageDialog(null, "Draw!");
                            //NoughtsAndCrossesGamePanel.this.dispose();
                        }
                    }
                }
    
                /**
                 * Checks if the button in btnKeypad array in positions i, j, k
                 * all have the same text.
                 * @param i - button number
                 * @param j - button number
                 * @param k - button number
                 * @return true if button i,j,k
                 */
                private boolean isAWinner(int i, int j, int k) {
                    return !btnKeypad[i].getText().equals(" ")
                            && btnKeypad[i].getText().equals(btnKeypad[j].getText())
                            && btnKeypad[i].getText().equals(btnKeypad[k].getText());
                }
    
                /**
                 * Checks for all possible winning scenarios
                 * @return null if there is no winner or the text on the button of the
                 * winner
                 */
                public String checkForWinner() {
                    /* Assumed mapping of button indexes to position in game is -
                     * 	012
                     * 	345
                     * 	678
                     */
                    int winner = -1; // -1 value means there was no winner
    
                    if (isAWinner(0, 1, 2)) {// Horizontal row 1
                        winner = 0;
                    } else if (isAWinner(3, 4, 5)) {	// Horizontal row 2
                        winner = 3;
                    } else if (isAWinner(6, 7, 8)) {	// Horizontal row 3
                        winner = 6;
                    } else if (isAWinner(0, 3, 6)) {	// Vertical row 1
                        winner = 0;
                    } else if (isAWinner(1, 4, 7)) {	// Vertical row 2
                        winner = 1;
                    } else if (isAWinner(2, 5, 8)) {	// Vertical row 3
                        winner = 2;
                    } else if (isAWinner(0, 4, 8)) {	// Diagonal 1
                        winner = 0;
                    } else if (isAWinner(2, 4, 6)) {	// Diagonal 2
                        winner = 2;
                    }
    
                    if (winner != -1) {
                        return btnKeypad[winner].getText();
                    } else {
                        return null;
                    }
                }
            }
    }
    
            class ButtonExitActionHandler implements ActionListener {
                
                /**
    * Inner class of NoughtsAndCrossesGameFrame to respond to the Play Button being
    * pressed. Asks the frame to reset the game by calling the frames resetGame
    * method
    */
    
                
                public void actionPerformed(ActionEvent e)
    		{
                    
    			System.exit(0);
    
    		}
               
            }
    
            class ButtonPlayActionHandle implements ActionListener {
                public void actionPerformed(ActionEvent e)
    		{
    			//resetGame();
    		}
            }
    done with a part however when i try to click the reset button i can't reset game i try put resetGame on class ButtonPlayActionHandle implements ActionListener to call the
    Java Code:
    private void resetGame() {
               /**
    * Disables all the buttons when the game ends
                *
    */
                clickCount = 0;
                for (int i = 0; i < 9; i++){
                    char ch=(char)('0'+i+1);
                    btnKeypad[i].setText(""+ch);
                }
    
            }
    and the i show error how to manage to solve it : here is my design atm
    Noughts And Crosses v2 problem with running-untitled.png

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Noughts And Crosses v2 problem with running

    Don't add new threads with the same code, and apparently a related question. That simply clutters up the forum. The thread you started a short while ago has been closed.

    You would probably get better help if you were to make your code more readable. That would require you to remove (1) inane, IDE-generated comments about the template etc. (2) absurd amounts of vertical whitespace - blank lines, in case you didn't understand that and (3) portions of the code unrelated to the specific problem for which help is sought. In other words, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem and only the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2012, 05:27 PM
  2. Replies: 7
    Last Post: 12-18-2011, 04:13 AM
  3. Java beginner- Noughts and Crosses help
    By Trevers89 in forum New To Java
    Replies: 10
    Last Post: 07-05-2011, 01:58 PM
  4. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  5. jsp running problem
    By bharanikumariyerjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:48 AM

Posting Permissions

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