Home Answers Viewqa Java-Beginners Running problem with NoughtsAndCrossesGame in blank

 
 


Dexter
Running problem with NoughtsAndCrossesGame in blank
0 Answer(s)      a year and 2 months ago
Posted in : Java Beginners

Hi i was having problem created NoughtsAndCrossesGame in end the it works but i runs the gui in blank. Its another way to solve it

/*
 * 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();
        }
        }
View Answers









Related Pages:
Running problem with NoughtsAndCrossesGame in blank
Running problem with NoughtsAndCrossesGame in blank  Hi i was having problem created NoughtsAndCrossesGame in end the it works but i runs the gui in blank. Its another way to solve it /* * To change this template, choose
PHP Prevent blank upload
PHP Prevent blank upload  In my small PHP application, i am posting a data to server using POST method in PHP, which is working fine. But the problem is that it also accepts blank data as well as duplicate value. Is there any way
problem running a servlet on tomcat.
problem running a servlet on tomcat.  i have followed the steps given... suppose that should mean that my tomcat server is up and running. but whn i try... be the problem? please help me out
Validaton(Checking Blank space) - Spring
Validaton(Checking Blank space)  Hi i am using java springs .I am having problem in doing validation. I am just checking blank space. This is my controller. I named it salescontroller. package controller; import
Problem in running first hibernate program.... - Hibernate
Problem in running first hibernate program....  Hi...I am using eclipse ganymede and mysql.I have already configured mysql and did few sample..., It seems that your class has not been found.There may be problem in your
Problem
(); echoSocket.close(); } } this is my code, compiled with no errors, but when running giving... that the server (something like EchoServer) is up and running.   Thanx i got the answer, i just turned off the firewall & its running fine
Problem
with no errors, but when running giving "java.net.connectexception connection...) is up and running
UItextfield blank
UItextfield blank  Hi, How I can check if UITextfield is blank and then display error to the user? Thanks   Hi, Following code can be used to validate the user input in iPhone and iPad applications. if([loginID.text
applet running but no display - Applet
applet running but no display  Hai, Thanks for the post. I have applied the codebase url in the page and executed. Now, when accessed the page from a client, the page appears with a blank applet part (just whitescreen
Running and deploying Tomcat Server
Running and deploying Tomcat Server  HI Somebody has given the solution but it doesn't work out. kindly tell the solution for my problem which i... the problem to get the correct solution
NoughtsAndCrossesGame play button doesn't work
NoughtsAndCrossesGame play button doesn't work   /* * To change this template, choose Tools | Templates * and open the template in the editor... NoughtsAndCrossesGameFrame noughtsAndCrossesGame; noughtsAndCrossesGame = new
NoughtsAndCrossesGame play button doesn't work
NoughtsAndCrossesGame play button doesn't work   /* * To change this template, choose Tools | Templates * and open the template in the editor... NoughtsAndCrossesGameFrame noughtsAndCrossesGame; noughtsAndCrossesGame = new
About running the Tomcat Server
About running the Tomcat Server   HI I want to run a simple program on servlet and the application is a simple program Hello world... is not there SO kindly tell me why it so happened and rectify the problem to get
Server running Button - Java Beginners
Server running Button  Hi there, I have a created a GUI using NetBeans IDE 6.1. The aim of this interface is to display the messages that have.... The problem is with the display , when I run the GUI and click on the play button
Java Compilation and running error - RMI
Java Compilation and running error   The following set of programs... some problem and gives following on compilation"java uses unchecked or unsafe... showing problem. Please tell me why I am unable to run this program with Java
Struts2 blank application - Struts
Struts2 blank application  Hi I am new to struts2 and i am trying to run the strutsblank application by following the following Link; http://www.roseindia.net/struts/struts2/index.shtml As per the instructions given
How to design a blank CD, design a blank CD, blank CD
How to design a blank CD       This is a blank CD example, You will learn here how to make a blank CD by taking help of this example. New File: Take a new file with any
Running threads in servlet only once - JSP-Servlet
Running threads in servlet only once  Hi All, I am developing... process while mail thread is running. these two separate threads has to run... them self immediately. how can resolve this problem. Thanks
app crashed in ipad 2 running iOS 5.0.1
app crashed in ipad 2 running iOS 5.0.1  i developed a universal... in ipad 2 running iOS 5.0.1, witch is not in compliance with the App Store Review Guidelines." Can anyone tell me how to solve this problem?.. or do i need iPad
java web application without running tomcat
java web application without running tomcat  I did a project by using... up file by using NSIS tool..but the problem when i will the project to some... is there any way to run the web application(only in single system)without running
error while running the applet - Java Beginners
); ++num; } } } } i have problem while running the code , error... is correct but u have problem in running the program. You follow the following steps...error while running the applet  import java.applet.Applet; import
Running & Testing on WAMP server
Running & Testing on WAMP server In this section we will learn how... red, it means no service is currently running, click on it and a popup menu... change to white that means all services are running, if the color of the icon
Problem with loginbean.jsp
Problem with loginbean.jsp  http://www.roseindia.net/jsp/loginbean.shtml - I am getting an error in loginbean.jsp.There... is login.java is accessed or executed while running
tomcat problem
tomcat problem  error like requested(rootdirectory/urlpattern) resources are not available getting while running servlet on tomcatserver
MySQL Blank Field
MySQL Blank Field This example illustrates how to find Blank field by the MySQL Query. In the table 'example' two t_count fields are blank and we find both field which has some value by query 'select * from example where t_count
solve this problem
blank page is coming the text is not displaying and row is also inserted in table please tell me the solution for thid problem import java.io.*; import
Servlet problem
problem from last three month and now i hope rose india developers... at a hosting site . Servlet page give blank page and exception "java.sql.SQLException... connectivity code it works but problem is with servlet page. My servlet code
hibernate problem
:12) i am getting this prob while running my hibernate program in Eclips please
error of HTTP Status 404 while running servlet on apache tomcat server
error of HTTP Status 404 while running servlet on apache tomcat server ... the apache tomcat page. It mean that my tomcat server is up and running. but whn i... be the problem
tomcat server problem
placing a problem with tomcat5.5 server. when i am trying to execute my project I... login page of the project and when i entered the username and password the blank page is appeared. please resolve my problem as soon as possible. thanking you
blank space in input field - Java Beginners
blank space in input field  I retrieved the fields from my mysql table...If the field contains null value i want to display a blank space in input field(text box)..but i am displaying null how to overcome this.. u r help
Running JUnit
Running JUnit  Hi sir How can we run JUnit or Run test case Using JUnit using command prompt or CMD? Is there any alternate way of running test cases?   ou can run JUnit test case by running following command on CMD
Running JUnit
Running JUnit  Hi sir How can we run JUnit or Run test case Using JUnit using command prompt or CMD? Is there any alternate way of running test cases?   Hi friend You can run JUnit test case by running following
Javascript problem - WebSevices
before submitting the form with the condition of being empty. PROBLEM:-->...) { if(x.value.length==0) { alert("This field should not be blank... friend, Please specify the problem : You want to return back same page
Creating Blank DOM Document
Creating Blank DOM Document       This tutorial shows you how to create blank DOM document. JAXP (Java... of the class DocumentBuilder is used to create a blank document. The newDocument
Problem in uploading java application
Problem in uploading java application  I have uploaded my java... is running in local machine properly.But while trying to access the hosted... this problem
instalation problem - Java Beginners
instalation problem  i try to install java ver 3-4-5 but. when progres going on that cant configuration. its stop when the indicator running in 1/4 progres.. thanx
installation problem - IDE Questions
installation problem  A configuratio error occured during startup.please verify the performance field with the prompt:Tomcat JDK name This is error i s coming while running project in myeclipse
iphone mail sending problem
the NSInvalidArgumentException. Now as i have changed and left it blank to enter..., Set up recipients is left blank in the new code. that works fine. Thanks
Problem in show card in applet.
Problem in show card in applet.  The following link contained... the Cards contained) in suitable place. 4) now I am running CardDemo.java file... other code will be add in the example or how can I solve my problem. please
Run time problem
Run time problem  when i run a project,it shows an exception like "unable to create MIDlet".It also shows "running with locale:English_united States.1252 running in the identified third party security domain" "please help
Problem with open connection - Hibernate
Problem with open connection  Hi Team, I am running one hibernate application and the database is ORACLE 10g.I am getting the below error.I connected to the database by using JDBC(with same driver and url).Please tell me
Problem with external DTD - XML
Problem with external DTD  Hi, This class generate an XML file.But while I am running that generated xml file its giving error as can not locate 'users.dtd'. Where should I place this 'usres.dtd' so that it'll validate
database problem - JDBC
workstation are able to connect to another ORACLE server that is running on NT, I am
problem at the time of execution - JSP-Servlet
problem at the time of execution  when i was running web applications the exception i.e 404 resource is not available what it means and where it occures what is the solution   Hi Friend, This error occurs when
running time watch
running time watch   how to set running watch in applycation with ajax
applet is running but no display
applet is running but no display  applet is running but no display.. its showing white screen with applet started
code related problem
code related problem  this code is compiling but not running please help import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Mybutton11 extends JFrame { JTextField text1 = new JTextField(20
problem with starting tomcat - IDE Questions
problem with starting tomcat  I'm running Eclipse 3.1with Tomcat 5.5 When I start Tomcat inside of Eclipse using the "Servers" tab, the "Console" window shows that Tomcat is running but the "Servers" window shows "starting

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.