Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Tic tac Toe Getting the Images to work for the buttons

    Hi. I've been working on this too long, and I can't figure out how to get this area fixed under Who's Turn it Is. I keep getting errors. The teacher said that I need to "Create an Icon variable to store the X or the O Icon that you already instantiated in the constructor"- But everything I try doesn't work. Thank You.

    Java Code:
     public Java_game() //this is a constructor
       {
           setLayout(new GridLayout(4,3));//a 4 by 3 grid
            Icon X = new ImageIcon(getClass().getResource("X.gif"));
            Icon O = new ImageIcon(getClass().getResource("O.gif"));
    Java Code:
      //private inner class for event handling of buttons
                             private class MessageListener implements ActionListener{
    
                                 public void actionPerformed(ActionEvent event)
                                 {
                                      count++;
    
                                             //Who's Turn It is
                                             if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
                                             {
    
                                    Icon = ("X.gif");
    
                                             }
                                             else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                                             {
    
                                          Icon = ("O.gif");
    
    
    
    
            }
                                     if(event.getSource() == button0)
                                     {
                                     button0.setIcon();
                                     button0.setText(null);
                                     button0.setEnabled(false);





    Here's My Full Code:
    Java Code:
    /* HomeWork 4 Tic Tac Toe
    By: Amanda Nicolau
    File Name: Java_game.java
    Function: To Play Tic Tac Toe*/
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
     public class Java_game extends JFrame
         {
             private JButton button0;
             private JButton button1;
             private JButton button2;
             private JButton button3;
             private JButton button4;
             private JButton button5;
             private JButton button6;
             private JButton button7;
             private JButton button8;
             private JButton playagain;
             private JButton PlayerXScore;
             private JButton PlayerOScore;
             private  Icon X;
             private  Icon O;
             private String letter = "";
             private int count = 0;
             private int XWins, OWins = 0;
             private boolean win = false;
    
    
       public Java_game() //this is a constructor
       {
           setLayout(new GridLayout(4,3));//a 4 by 3 grid
            Icon X = new ImageIcon(getClass().getResource("X.gif"));
            Icon O = new ImageIcon(getClass().getResource("O.gif"));
    
    
    
                  //Add a button
                   button0 = new JButton();//create a button
                   button0.setText("0");//text on button
                   button0.addActionListener(new MessageListener());
                   add(button0); //add button to our window
    
    
                    //ADD another Button
                     button1 = new JButton();//create a button
                     button1.setText("1");//text on button
                     button1.addActionListener(new MessageListener());
                     add(button1); //add button to our window
    
    
                  //ADD another Button
                  button2 = new JButton();//create a button
                  button2.setText("2");//text on button
                  button2.addActionListener(new MessageListener());
                  add(button2); //add button to our window
    
    
                    //ADD another Button
                    button3 = new JButton();//create a button
                    button3.setText("3");//text on button
                    button3.addActionListener(new MessageListener());
                    add(button3); //add button to our window
    
    
                   //ADD another Button
                   button4 = new JButton();//create a button
                   button4.setText("4");//text on button
                   button4.addActionListener(new MessageListener());
                   add(button4); //add button to our window
    
                   //ADD another Button
                    button5 = new JButton();//create a button
                    button5.setText("5");//text on button
                    button5.addActionListener(new MessageListener());
                    add(button5); //add button to our window
    
    
                    //ADD another Button
                     button6 = new JButton();//create a button
                     button6.setText("6");//text on button
                     button6.addActionListener(new MessageListener());
                     add(button6); //add button to our window
    
    
                      //ADD another Button
                      button7 = new JButton();//create a button
                      button7.setText("7");//text on button
                      button7.addActionListener(new MessageListener());
                      add(button7); //add button to our window
    
    
                       //ADD another Button
                       button8 = new JButton();//create a button
                       button8.setText("8");//text on button
                       button8.addActionListener(new MessageListener());
                       add(button8); //add button to our window
    
    
    
                                  //Play Again Button
                                  playagain = new JButton();//create a button
                                  playagain.setText("Play Again");//text on button
                                  playagain.addActionListener(new MessageListener());
                                  add(playagain); //add button to our window
    
                                 //Player X Score
                                  PlayerXScore = new JButton();//create a button
                                  PlayerXScore.setText("Player X Score");//text on button
                                  PlayerXScore.addActionListener(new MessageListener());
                                  add(PlayerXScore); //add button to our window
    
                                  //Player O Score
                                  PlayerOScore = new JButton();//create a button
                                  PlayerOScore.setText("Player O Score");//text on button
                                  PlayerOScore.addActionListener(new MessageListener());
                                  add(PlayerOScore); //add button to our window
    
    
    
                             }
    
                             //private inner class for event handling of buttons
                             private class MessageListener implements ActionListener{
    
                                 public void actionPerformed(ActionEvent event)
                                 {
                                      count++;
    
                                             //Who's Turn It is
                                             if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
                                             {
    
    
    
                                              Icon = ("X.gif");
    
                                             }
                                             else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                                             {
    
    
    
                                            Icon = ("O.gif");
    
    
    
    
            }
                                     if(event.getSource() == button0)
                                     {
                                     button0.setIcon(Icon);
                                     button0.setText(null);
                                     button0.setEnabled(false);
    
    
    
                                     CheckForWin();
    
                                 }
    
                                 else if (event.getSource() == button1)
                                 {
    
                                     button1.setText(null);
                                     button1.setIcon(Icon);
                                     button1.setEnabled(false);
                                     CheckForWin();
                                 }
    
                          else if (event.getSource() == button2)
                          {
    
                            button2.setText(null);
                            button2.setIcon(Icon);
                            button2.setEnabled(false);
                            CheckForWin();
                          }
    
                         else if (event.getSource() == button3)
                                               {
    
                        button3.setText(null);
                        button3.setIcon(Icon);
                        button3.setEnabled(false);
                        CheckForWin();
    
                     }
                     else if (event.getSource() == button4)
                                           {
    
                                             button4.setText(null);
                                             button4.setIcon(Icon);
                                             button4.setEnabled(false);
                                             CheckForWin();
                     }
    
                     else if (event.getSource() == button5)
                                           {
    
                                             button5.setText(null);
                                             button5.setIcon(Icon);
                                             button5.setEnabled(false);
                                             CheckForWin();
                     }
    
                     else if (event.getSource() == button6)
                                           {
    
    
                                             button6.setText(null);
                                             button6.setIcon(Icon);
                                             button6.setEnabled(false);
                                             CheckForWin();
                     }
    
                     else if (event.getSource() == button7)
                                           {
    
                                             button7.setText(null);
                                             button7.setIcon(Icon);
                                             button7.setEnabled(false);
                                             CheckForWin();
                     }
                     else if (event.getSource() == button8)
                                           {
    
                                             button8.setText(null);
                                             button8.setIcon(Icon);
                                             button8.setEnabled(false);
                                             CheckForWin();
                     }
    
                     else if (event.getSource() == playagain)
                             {
                                 int answer = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Confirm", JOptionPane.YES_NO_OPTION);
                                 if(answer == JOptionPane.NO_OPTION)
                                 {
                                     System.exit(0);
                                 }
                                 else
                                 {
                                     reset();
                                     count = 0;
                               }
    
    
          }
                 else if (event.getSource() == PlayerXScore)
                 {
                         JOptionPane.showMessageDialog(null, "Player X's number of Wins" + " " + XWins);
    
                 }
                 else if (event.getSource() == PlayerOScore)
                 {
                         JOptionPane.showMessageDialog(null, "Player O's number of Wins" + " " + OWins);
                 }
    
        }
    
    }
             public void CheckForWin()
               {
                    //Checking who wins
                    //Horizontal
                    if (button0.getIcon() == button1.getIcon() && button1.getIcon() == button2.getIcon() && button1.getIcon() != null)
                    {
                        win = true;
                        Win();
                    }
                    else if (button3.getIcon() == button4.getIcon() && button4.getIcon() == button5.getIcon() && button3.getIcon() != null)
                    {
                        win = true;
                        Win();
                    }
                    else if (button6.getIcon() == button7.getIcon() && button7.getIcon() == button8.getIcon() && button6.getIcon() != null)
                    {
                        win = true;
                        Win();
                    }
    
                //Win vertically
                else if (button0.getIcon() == button3.getIcon() && button3.getIcon() == button6.getIcon() && button0.getIcon() != null)
                {
                    win = true;
                    Win();
                }
                else if (button1.getIcon() == button4.getIcon() && button4.getIcon() == button7.getIcon() && button1.getIcon() != null)
                {
                    win = true;
                    Win();
                }
                else if (button2.getIcon() == button5.getIcon() && button5.getIcon() == button8.getIcon() && button2.getIcon() != null)
                {
                    win = true;
                    Win();
                }
    
                //Win diagonally
                else if (button0.getIcon() == button4.getIcon() && button4.getIcon() == button8.getIcon() && button0.getIcon() != null)
                {
                    win = true;
                    Win();
                }
                else if (button2.getIcon() == button4.getIcon() && button4.getIcon() == button6.getIcon() && button2.getIcon() != null)
                {
                    win = true;
                    Win();
                }
                else
                {
                    win = false;
                    Win();
                }
    
    
       }//END OF CHECKWIN FUNCTION
    
        public void Win()
           {
    
               letter = "X";
    
    
               if (win == true)
               {
                System.out.println("Win Called");
                   JOptionPane.showMessageDialog(null, letter +" " + "Wins!");
    
                   if(letter.equalsIgnoreCase("X"))
                   {
                    XWins++;
                   }
                   else
                   {
                    OWins++;
                   }
    
    
    
               }
               else if (count == 9 && win == false)
               {
                   JOptionPane.showMessageDialog(null, "Tie game!!");
    
    
               }
        }
    
     public void reset()
        {
            button0.setText("0");
            button0.setIcon(null);
            button0.setEnabled(true);
            button1.setText("1");
            button1.setIcon(null);
            button1.setEnabled(true);
            button2.setText("2");
            button2.setIcon(null);
            button2.setEnabled(true);
            button3.setText("3");
            button3.setIcon(null);
            button3.setEnabled(true);
            button4.setText("4");
            button4.setIcon(null);
            button4.setEnabled(true);
            button5.setText("5");
            button5.setIcon(null);
            button5.setEnabled(true);
            button6.setText("6");
            button6.setIcon(null);
            button6.setEnabled(true);
            button7.setText("7");
            button7.setIcon(null);
            button7.setEnabled(true);
            button8.setText("8");
            button8.setIcon(null);
            button8.setEnabled(true);
            playagain.setText("Play Again");
            playagain.setEnabled(true);
            PlayerXScore.setText("Player X's number of Wins");
            PlayerXScore.setEnabled(true);
            PlayerOScore.setText("Player O's number of Wins");
            PlayerOScore.setEnabled(true);
            win = false;
            count = 0;
        }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Tic tac Toe Getting the Images to work for the buttons

    Can you explain what your problem is?
    If you need to figure out how to put an image in a button, write an very small program that reads an image and shows it in a button. Nothing more.

    Post the test program if you need help with it.

    This is confusing coding:
    button0.setIcon(Icon);

    using a variable name with uppercase first letter that looks like the name of a class or interface.
    You should use a variable name there.
    Last edited by Norm; 03-14-2012 at 04:28 PM.

Similar Threads

  1. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 01:15 AM
  2. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 10-17-2011, 09:40 PM
  3. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 06:19 AM
  4. Replies: 2
    Last Post: 04-30-2010, 03:06 AM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 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
  •