Can't get game to work

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1373 Views - Last Post: 22 February 2011 - 10:04 AM Rate Topic: -----

#1 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Can't get game to work

Posted 16 February 2011 - 11:19 AM

I can't seem to get this program to run. Can someone assist me? I keep getting an error in my main.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class RPS extends JFrame  {




    private static final int FRAME_WIDTH = 1300;
    private static final int FRAME_HEIGHT = 800;
    private static final int AREA_ROWS = 10;
    private static final int AREA_COLUMNS = 30;

    private JButton Rock, Paper, Scissor, Reset, Simu;
    private JPanel  cpuSide, playerSide, buttonPanel, gamePanel, outcomePanel;
    private JPanel playerWinsPanel,cpuWinsPanel,tiesPanel,Computer, Player, Tie;
    private JTextArea resultArea;
    private JLabel label, label1, label2, label3, label4, label5, label6;
    
    Container contentPane;





    public RPS ()
    {
    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
    resultArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(resultArea);



    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Rock, Paper, Scissors");



    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.setLayout(new BorderLayout(5,5));




        Rock = new JButton("Rock");
        Paper = new JButton("Paper");
        Scissor = new JButton("Scissor");
        Reset = new JButton("Reset ScoreBoard");
        Simu = new JButton("Run Simulation");



        ButtonListener listener = new ButtonListener();

        Rock.addActionListener(listener);
        Paper.addActionListener(listener);
        Scissor.addActionListener(listener);
        Reset.addActionListener(listener);
        Simu.addActionListener(listener); 




        label = new JLabel ("Make your Selection");




    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4,3)); 
    buttonPanel.setBorder(BorderFactory.createTitledBorder("Buttons"));
    buttonPanel.setBackground(Color.BLUE);
    buttonPanel.add(Rock);
    buttonPanel.add(Paper);
    buttonPanel.add(Scissor);
       


    label1= new JLabel("");
    playerSide = new JPanel();
    playerSide.setBorder(BorderFactory.createTitledBorder("Player"));
    playerSide.setLayout(new GridLayout(3,0));
    playerSide.setBackground(Color.GRAY);
    playerSide.add(label1);
    label1.setFont(new Font("Areil", Font.BOLD, 16)); 
        

    label2 = new JLabel("");
    cpuSide = new JPanel();
    cpuSide.setBorder(BorderFactory.createTitledBorder("Computer"));
    cpuSide.setLayout(new GridLayout(1,0));
    cpuSide.setBackground(Color.GRAY);
    label2.setFont(new Font("Areil", Font.BOLD, 16));

    label3 = new JLabel("");
    outcomePanel = new JPanel();
    outcomePanel.setLayout(new GridLayout(4,0));
    outcomePanel.setBackground(Color.GREEN);
    JLabel labelResult =new JLabel ("No Results");
    outcomePanel.add(labelResult);
    label3.setFont(new Font("Areil", Font.BOLD, 16));


    label4= new JLabel("");
    playerWinsPanel= new JPanel();
    playerWinsPanel.setLayout(new GridLayout(2,0));
    playerWinsPanel.setBackground(Color.WHITE);
    JLabel labelPW = new JLabel ("Player Wins");
    playerWinsPanel.add(labelPW);
    playerWinsPanel.add(label4);


    label5= new JLabel("");
    cpuWinsPanel= new JPanel();
    cpuWinsPanel.setLayout(new GridLayout(2,0));
    cpuWinsPanel.setBackground(Color.WHITE);
    JLabel labelCW = new JLabel ("Computer Wins");
    cpuWinsPanel.add(labelCW);
    cpuWinsPanel.add(label5);


    label6= new JLabel("");
    tiesPanel= new JPanel();
    tiesPanel.setLayout(new GridLayout(2,0));
    tiesPanel.setBackground(Color.WHITE);
    JLabel labelTie = new JLabel ("Draw");
    tiesPanel.add(labelTie);
    tiesPanel.add(label6);

    contentPane.add(cpuSide, BorderLayout.EAST);
    contentPane.add(playerSide, BorderLayout.WEST);
    contentPane.add(gamePanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    contentPane.add(scrollPane);
    
    gamePanel = new JPanel();
    gamePanel.setBackground(Color.black);
    gamePanel.setLayout(new GridLayout(0,3));
    gamePanel.add(label);
    gamePanel.add(buttonPanel);
    gamePanel.add(playerSide);
    gamePanel.add(cpuSide);
    gamePanel.add(playerWinsPanel);
    gamePanel.add(cpuWinsPanel);
    gamePanel.add(Reset);
    gamePanel.add(Simu);
  
    }

public class ButtonListener implements ActionListener     {

      //-----------------------------------------------
       // Determines which button was pushed and outputs text accordingly

      //-----------------------------------------------
       Random generate= new Random();

       int Computer = 0, Player =0, Tie = 0, compic, playpic;



       public void actionPerformed (ActionEvent event)

      {



            compic=generate.nextInt(3)+1;



           // Computer Choice

           if (compic== 1)

             label2.setText("                    Rock                   ");

           else  if ( compic== 2)

             label2.setText("                    Paper                   ");

           else

             label2.setText("                  Scissor                   ");




          // Player Choice

           if (event.getSource () == Rock)

               label1.setText("                  Rock                   ");

           else if (event.getSource () == Paper)

               label1.setText("                  Paper                   ");
           else

              label1.setText("                Scissor                   ");



            if (event.getSource () == Rock)

               playpic = 1;

           else if (event.getSource () == Paper)

               playpic = 2;

           else

               playpic = 3;







          }


    //-------------------------------------------------------------------------------------------------------------------------

    // Compares Computer and Players choice

    //-------------------------------------------------------------------------------------------------------------------------

         {

            switch (playpic)

                 {

                     case 1:

                     {

                         if (compic==2)

                         {

                             label3.setText
                             ("Paper covers Rock - computer wins");

                             Computer++;

                          }

                        else if (compic==3)

                         {

                              label3.setText
                              ("Rock breaks Scissors - player wins");

                             Player++;

                          }
                          else

                          {

                              label3.setText  ("Tie");

                              Tie++;

                           }

                         break;

                     }



                     case 2:

                     {

                         if (compic==1)

                         {

                              label3.setText
                              ("Paper covers Rock - player wins");

                             Player++;

                          }

                         else if (compic==3)

                         {

                              label3.setText
                               ("Scissors cut Paper - computer wins");

                             Computer++;

                          }

                          else

                          {

                             label3.setText  ("Tie");

                             Tie++;

                          }

                         break;

                     }

                     case 3:

                     {

                         if (compic==1)

                         {

                              label3.setText
                                 ("Rock breaks Scissors - computer wins");

                             Computer++;

                          }

                         else if (compic==2)

                         {

                              label3.setText
                                ("Scissors cuts Paper - player wins");

                             Player++;

                         }

                         else
                         {
                             label3.setText("Tie");

                             Tie++;

                         

                        break;

                      }



                   }

             }

         }

     }

 }



import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class RPSTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("ROCK");
        frame.getContentPane().add(new RPS());
        
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}


This post has been edited by jumptrooper: 16 February 2011 - 11:46 AM
Reason for edit:: closed code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Can't get game to work

#2 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: Can't get game to work

Posted 16 February 2011 - 11:32 AM

What are you getting errors?
Was This Post Helpful? 0
  • +
  • -

#3 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 11:40 AM

this is the error "
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:927)
at RPS.<init>(RPS.java:154)
at RPSTest.main(RPSTest.java:20)
Java Result: 1"
Was This Post Helpful? 0
  • +
  • -

#4 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Can't get game to work

Posted 16 February 2011 - 11:49 AM

What's happening is that you're trying to add gamePanel to the content pane before you initialize it.

notice lines 155 and 159 in your post above.

This post has been edited by jumptrooper: 16 February 2011 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 11:51 AM

how do i correct that?
Was This Post Helpful? 0
  • +
  • -

#6 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Can't get game to work

Posted 16 February 2011 - 11:53 AM

View PostADK2011, on 16 February 2011 - 10:51 AM, said:

how do i correct that?

Initialize gamePanel before you add it to contentPane. See line 159? move that somewhere before line 155 - preferable at the beginning of the constructor. You can't add something that doesn't exist yet. :)

Actually, you'd want to move that whole segment (159 down) where you're initializing the gamePanel...

This post has been edited by jumptrooper: 16 February 2011 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 11:58 AM

I did that and got the same error
Was This Post Helpful? 0
  • +
  • -

#8 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Can't get game to work

Posted 16 February 2011 - 12:05 PM

View PostADK2011, on 16 February 2011 - 10:58 AM, said:

I did that and got the same error


If you look at the error message, it should say something like "can't add Frame to a container".
Notice that RPS extends JFrame - you don't want to then try and stuff that JFrame inside another JFrame (which is what you're doing in the main method.

if you chop out the JFrame stuff in your main and only have something like...
public static void main(String[] args){
  RPS rpsGame = new RPS();
  rpsGame.setVisible(true);
}


... you'll see that your window displays
Was This Post Helpful? 0
  • +
  • -

#9 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 12:12 PM

I've tried that already too. Same Error, no window. It's something with my contentpane
Was This Post Helpful? 0
  • +
  • -

#10 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Can't get game to work

Posted 16 February 2011 - 12:18 PM

View PostADK2011, on 16 February 2011 - 11:12 AM, said:

I've tried that already too. Same Error, no window. It's something with my contentpane

well... I ran your code and got it to work...
Here it is:
all I did was move where you put the initialization of gamePanel and change the main..
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class RPS extends JFrame  {




    private static final int FRAME_WIDTH = 1300;
    private static final int FRAME_HEIGHT = 800;
    private static final int AREA_ROWS = 10;
    private static final int AREA_COLUMNS = 30;

    private JButton Rock, Paper, Scissor, Reset, Simu;
    private JPanel  cpuSide, playerSide, buttonPanel, gamePanel, outcomePanel;
    private JPanel playerWinsPanel,cpuWinsPanel,tiesPanel,Computer, Player, Tie;
    private JTextArea resultArea;
    private JLabel label, label1, label2, label3, label4, label5, label6;
    
    Container contentPane;





    public RPS ()
    {
    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
    resultArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(resultArea);



    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Rock, Paper, Scissors");



    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.setLayout(new BorderLayout(5,5));




        Rock = new JButton("Rock");
        Paper = new JButton("Paper");
        Scissor = new JButton("Scissor");
        Reset = new JButton("Reset ScoreBoard");
        Simu = new JButton("Run Simulation");



        ButtonListener listener = new ButtonListener();

        Rock.addActionListener(listener);
        Paper.addActionListener(listener);
        Scissor.addActionListener(listener);
        Reset.addActionListener(listener);
        Simu.addActionListener(listener); 




        label = new JLabel ("Make your Selection");




    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4,3)); 
    buttonPanel.setBorder(BorderFactory.createTitledBorder("Buttons"));
    buttonPanel.setBackground(Color.BLUE);
    buttonPanel.add(Rock);
    buttonPanel.add(Paper);
    buttonPanel.add(Scissor);
       


    label1= new JLabel("");
    playerSide = new JPanel();
    playerSide.setBorder(BorderFactory.createTitledBorder("Player"));
    playerSide.setLayout(new GridLayout(3,0));
    playerSide.setBackground(Color.GRAY);
    playerSide.add(label1);
    label1.setFont(new Font("Areil", Font.BOLD, 16)); 
        

    label2 = new JLabel("");
    cpuSide = new JPanel();
    cpuSide.setBorder(BorderFactory.createTitledBorder("Computer"));
    cpuSide.setLayout(new GridLayout(1,0));
    cpuSide.setBackground(Color.GRAY);
    label2.setFont(new Font("Areil", Font.BOLD, 16));

    label3 = new JLabel("");
    outcomePanel = new JPanel();
    outcomePanel.setLayout(new GridLayout(4,0));
    outcomePanel.setBackground(Color.GREEN);
    JLabel labelResult =new JLabel ("No Results");
    outcomePanel.add(labelResult);
    label3.setFont(new Font("Areil", Font.BOLD, 16));


    label4= new JLabel("");
    playerWinsPanel= new JPanel();
    playerWinsPanel.setLayout(new GridLayout(2,0));
    playerWinsPanel.setBackground(Color.WHITE);
    JLabel labelPW = new JLabel ("Player Wins");
    playerWinsPanel.add(labelPW);
    playerWinsPanel.add(label4);


    label5= new JLabel("");
    cpuWinsPanel= new JPanel();
    cpuWinsPanel.setLayout(new GridLayout(2,0));
    cpuWinsPanel.setBackground(Color.WHITE);
    JLabel labelCW = new JLabel ("Computer Wins");
    cpuWinsPanel.add(labelCW);
    cpuWinsPanel.add(label5);


    label6= new JLabel("");
    tiesPanel= new JPanel();
    tiesPanel.setLayout(new GridLayout(2,0));
    tiesPanel.setBackground(Color.WHITE);
    JLabel labelTie = new JLabel ("Draw");
    tiesPanel.add(labelTie);
    tiesPanel.add(label6);

    gamePanel = new JPanel();
    gamePanel.setBackground(Color.black);
    gamePanel.setLayout(new GridLayout(0,3));
    gamePanel.add(label);
    gamePanel.add(buttonPanel);
    gamePanel.add(playerSide);
    gamePanel.add(cpuSide);
    gamePanel.add(playerWinsPanel);
    gamePanel.add(cpuWinsPanel);
    gamePanel.add(Reset);
    gamePanel.add(Simu);
    
    contentPane.add(cpuSide, BorderLayout.EAST);
    contentPane.add(playerSide, BorderLayout.WEST);
    contentPane.add(gamePanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    contentPane.add(scrollPane);
    
   
  
    }

public class ButtonListener implements ActionListener     {

      //-----------------------------------------------
       // Determines which button was pushed and outputs text accordingly

      //-----------------------------------------------
       Random generate= new Random();

       int Computer = 0, Player =0, Tie = 0, compic, playpic;



       public void actionPerformed (ActionEvent event)

      {



            compic=generate.nextInt(3)+1;



           // Computer Choice

           if (compic== 1)

             label2.setText("                    Rock                   ");

           else  if ( compic== 2)

             label2.setText("                    Paper                   ");

           else

             label2.setText("                  Scissor                   ");




          // Player Choice

           if (event.getSource () == Rock)

               label1.setText("                  Rock                   ");

           else if (event.getSource () == Paper)

               label1.setText("                  Paper                   ");
           else

              label1.setText("                Scissor                   ");



            if (event.getSource () == Rock)

               playpic = 1;

           else if (event.getSource () == Paper)

               playpic = 2;

           else

               playpic = 3;







          }


    //-------------------------------------------------------------------------------------------------------------------------

    // Compares Computer and Players choice

    //-------------------------------------------------------------------------------------------------------------------------

         {

            switch (playpic)

                 {

                     case 1:

                     {

                         if (compic==2)

                         {

                             label3.setText
                             ("Paper covers Rock - computer wins");

                             Computer++;

                          }

                        else if (compic==3)

                         {

                              label3.setText
                              ("Rock breaks Scissors - player wins");

                             Player++;

                          }
                          else

                          {

                              label3.setText  ("Tie");

                              Tie++;

                           }

                         break;

                     }



                     case 2:

                     {

                         if (compic==1)

                         {

                              label3.setText
                              ("Paper covers Rock - player wins");

                             Player++;

                          }

                         else if (compic==3)

                         {

                              label3.setText
                               ("Scissors cut Paper - computer wins");

                             Computer++;

                          }

                          else

                          {

                             label3.setText  ("Tie");

                             Tie++;

                          }

                         break;

                     }

                     case 3:

                     {

                         if (compic==1)

                         {

                              label3.setText
                                 ("Rock breaks Scissors - computer wins");

                             Computer++;

                          }

                         else if (compic==2)

                         {

                              label3.setText
                                ("Scissors cuts Paper - player wins");

                             Player++;

                         }

                         else
                         {
                             label3.setText("Tie");

                             Tie++;

                         

                        break;

                      }



                   }

             }

         }

     }

 }


import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class RPSTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        JFrame frame = new JFrame("ROCK");
       new RPS().setVisible(true);
        
        
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setVisible(true);
    }

}



Was This Post Helpful? 0
  • +
  • -

#11 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 12:26 PM

Thanks, now i have two windows open, i should be able to figure out. And the game isn't working right, so i got figure out why.
Was This Post Helpful? 0
  • +
  • -

#12 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 16 February 2011 - 04:20 PM

oK, I revised the code, but i can't get it to run correctly, also i am trying have my textarea display the results of each game as a text string. Can you assist me with this.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class RPS extends JFrame  {




    private static final int FRAME_WIDTH = 1300;
    private static final int FRAME_HEIGHT = 800;
    private static final int AREA_ROWS = 10;
    private static final int AREA_COLUMNS = 30;

    private JButton Rock, Paper, Scissor, Reset, Simu;
    private JPanel  cpuSide, playerSide, buttonPanel, gamePanel, outcomePanel;
    private JPanel playerWinsPanel,cpuWinsPanel,tiesPanel;
    private JTextArea resultArea;
    private JLabel label, label1, label2, label3, label4, label5, label6;
    
    Container contentPane;





    public RPS ()
    {
    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
    resultArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(resultArea);



    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Rock, Paper, Scissors");



    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.setLayout(new BorderLayout(9,9));




        Rock = new JButton("Rock");
        Paper = new JButton("Paper");
        Scissor = new JButton("Scissor");
        Reset = new JButton("Reset ScoreBoard");
        Simu = new JButton("Run Simulation");



        ButtonListener listener = new ButtonListener();

        Rock.addActionListener(listener);
        Paper.addActionListener(listener);
        Scissor.addActionListener(listener);
        Reset.addActionListener(listener);
        Simu.addActionListener(listener); 




        label = new JLabel ("Make your Selection");

    


    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4,1));
    buttonPanel.setBorder(BorderFactory.createTitledBorder("Buttons"));
    buttonPanel.setBackground(Color.BLUE);
    buttonPanel.add(Rock);
    buttonPanel.add(Paper);
    buttonPanel.add(Scissor);
       


    label1= new JLabel("");
    playerSide = new JPanel();
    playerSide.setBorder(BorderFactory.createTitledBorder("Player"));
    playerSide.setLayout(new GridLayout(1,1));
    JLabel labelPc = new JLabel("Player's Choice");
    playerSide.add(labelPc);
    playerSide.setBackground(Color.GRAY);
    playerSide.add(label1);
    label1.setFont(new Font("Areil", Font.BOLD, 16)); 
        

    label2 = new JLabel("");
    cpuSide = new JPanel();
    cpuSide.setBorder(BorderFactory.createTitledBorder("Computer"));
    cpuSide.setLayout(new GridLayout(1,1));
    cpuSide.setBackground(Color.GRAY);
    JLabel labelcpu = new JLabel("Computer's Choice");
    cpuSide.add(labelcpu);
    cpuSide.add(label2);
    label2.setFont(new Font("Areil", Font.BOLD, 16));

    label3 = new JLabel("");
    outcomePanel = new JPanel();
    
    outcomePanel.setBackground(Color.GREEN);
    JLabel labelResult =new JLabel ("No Results");
    outcomePanel.add(labelResult);
    outcomePanel.add(label3);
    label3.setFont(new Font("Areil", Font.BOLD, 16));


    label4= new JLabel("");
    playerWinsPanel= new JPanel();
    playerWinsPanel.setBackground(Color.WHITE);
    JLabel labelPW = new JLabel ("Player Wins");
    playerWinsPanel.add(labelPW);
    playerWinsPanel.add(label4);


    label5= new JLabel("");
    cpuWinsPanel= new JPanel();
    cpuWinsPanel.setLayout(new GridLayout(2,1));
    cpuWinsPanel.setBackground(Color.BLACK);
    JLabel labelCW = new JLabel ("Computer Wins");
    cpuWinsPanel.add(labelCW);
    cpuWinsPanel.add(label5);


    label6= new JLabel("");
    tiesPanel= new JPanel();
    tiesPanel.setLayout(new GridLayout(2,0));
    tiesPanel.setBackground(Color.WHITE);
    JLabel labelTie = new JLabel ("Draw");
    tiesPanel.add(labelTie);
    tiesPanel.add(label6);

    gamePanel = new JPanel();
    gamePanel.setBackground(Color.RED);
    gamePanel.setLayout(new GridLayout(5,3));
    gamePanel.add(label);
    gamePanel.add(playerWinsPanel);
    gamePanel.add(cpuWinsPanel);
    gamePanel.add(Reset);
    gamePanel.add(Simu);
    gamePanel.add(scrollPane);
    

    contentPane.add(cpuSide, BorderLayout.EAST);
    contentPane.add(playerSide, BorderLayout.WEST);
    contentPane.add(gamePanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    

    }

public class ButtonListener implements ActionListener     {

      //-----------------------------------------------
       // Determines which button was pushed and outputs text accordingly

      //-----------------------------------------------
       Random generate= new Random();

       int Computer = 0, Player =0, Tie = 0, compic, playpic;



       public void actionPerformed (ActionEvent event)

      {



            compic=generate.nextInt(3)+1;



           // Computer Choice

           if (compic== 1)

             label2.setText("                    Rock                   ");

           else  if ( compic== 2)

             label2.setText("                    Paper                   ");

           else

             label2.setText("                  Scissor                   ");




          // Player Choice

           if (event.getSource () == Rock)

               label1.setText("                  Rock                   ");

           else if (event.getSource () == Paper)

               label1.setText("                  Paper                   ");
           else

              label1.setText("                Scissor                   ");



            if (event.getSource () == Rock)

               playpic = 1;

           else if (event.getSource () == Paper)

               playpic = 2;

           else

               playpic = 3;







          }


    //-------------------------------------------------------------------------------------------------------------------------

    // Compares Computer and Players choice

    //-------------------------------------------------------------------------------------------------------------------------

         {

            switch (playpic)

                 {

                     case 1:

                     {

                         if (compic == 2)

                         {

                             label3.setText
                             ("Paper covers Rock - computer wins");

                             Computer++;

                          }

                        else if (compic==3)

                         {

                              label3.setText
                              ("Rock breaks Scissors - player wins");

                             Player++;

                          }
                          else
                         {
                          

                              label3.setText  ("Tie");

                              Tie++;

                           }

                         break;

                     }



                     case 2:

                     {

                         if (compic==1)

                         {

                              label3.setText
                              ("Paper covers Rock - player wins");

                             Player++;

                          }

                         else if (compic==3)

                         {

                              label3.setText
                               ("Scissors cut Paper - computer wins");

                             Computer++;

                          }

                          else

                          {

                             label3.setText  ("Tie");

                             Tie++;

                          }

                         break;

                     }

                     case 3:

                     {

                         if (compic==1)

                         {

                              label3.setText
                                 ("Rock breaks Scissors - computer wins");

                             Computer++;

                          }

                         else if (compic==2)

                         {

                              label3.setText
                                ("Scissors cuts Paper - player wins");

                             Player++;

                         }

                         else
                         {
                             label3.setText("Tie");

                             Tie++;

                         

                        break;

                      }



                   }

             }

         }

     }

 }



This post has been edited by pbl: 16 February 2011 - 07:38 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#13 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 17 February 2011 - 06:26 AM

any ideas, to move me in the right direction?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

Re: Can't get game to work

Posted 17 February 2011 - 07:25 AM

1) "It isn't working" doesn't help us. What specifically isn't working?
2) See the JTextArea append() method.
Was This Post Helpful? 0
  • +
  • -

#15 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 17 February 2011 - 07:48 AM

it's not printing out who wins via the computer or the player or tie. its not keeping in score.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2