Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Program to run as Applet & Application - need fresh eye's simple mistake somewhere

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Program to run as Applet & Application - need fresh eye's simple mistake somewhere

    Basically this is a rock paper scissors game in a simple gui. The program works in applet form, but when I tried this method to run as an application, nothing happened.

    This is the applet (below is my method to run as an application.)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    import javax.swing.JApplet;
     
     
    /** rock paper scissors class */
    public class JRockPaperScissors extends JApplet{
          	// Variables declaration
     
     
          private JLabel titleLbl;
          private JLabel selectionLbl;
       	private JLabel resultsLbl;
       	private JTextArea resultTextArea;
       	private JButton rockBn;
       	private JButton paperBn;
       	private JButton scissorBn;
       	private Container contentPane;
          private int cpu = 0;
          private int wins = 0;
          private int loses = 0;
          private int ties = 0;
          private final int CHOICE_MAX = 3;
          private final int ROCK = 0;
          private final int PAPER = 1;
          private final int SCISSORS = 2;
          private Random rand = new Random();
     
    	public JRockPaperScissors(){
      	initializeComponent();
    	}
            /** initializing componets */
    	private void initializeComponent()
    	{
    		titleLbl = new JLabel();
    		selectionLbl = new JLabel();
    		resultsLbl = new JLabel();
    		resultTextArea = new JTextArea();
    		rockBn = new JButton();
    		paperBn = new JButton();
    		scissorBn = new JButton();
    		contentPane = getContentPane();
     
    		//
    		// titleLbl
    		//
    		titleLbl.setText("Rock, Paper, Scissors");
                    titleLbl.setFont(new Font("Garrmond", Font.BOLD, 30));
    		//
    		// selectionLbl
    		//
    		selectionLbl.setText("Choose one");
                    selectionLbl.setFont(new Font("Arial",Font.BOLD,14));
    		//
    		// resultsLbl
    		//
    		resultsLbl.setText("*****Results*****");
                    selectionLbl.setFont(new Font("Arial",Font.BOLD,14));
    		//
    		// resultTextArea
    		//
     
     
     
    		//
    		// rockBn
    		//
    		rockBn.setText("Rock");
    		rockBn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				rockBn_actionPerformed(e);
    			}
     
    		});
    		//
    		// paperBn
    		//
    		paperBn.setText("Paper");
    		paperBn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				paperBn_actionPerformed(e);
    			}
     
    		});
    		//
    		// scissorBn
    		//
    		scissorBn.setText("Scissors");               
    		scissorBn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				scissorBn_actionPerformed(e);
    			}
     
    		});
    		//
    		// contentPane
    		//
    		contentPane.setLayout(null);
    		addComponent(contentPane, titleLbl, 5,9,370,47);
    		addComponent(contentPane, selectionLbl, 9,54,150,35);
    		addComponent(contentPane, resultsLbl, 9,93,144,38);
    		addComponent(contentPane, resultTextArea, 5,132,398,111);
    		addComponent(contentPane, rockBn, 162,58,78,31);
    		addComponent(contentPane, paperBn, 247,58,81,31);
    		addComponent(contentPane, scissorBn, 334,58,87,31);
    		contentPane.setSize(new Dimension(435, 290));
    		//
    		// JRockPaperScissors
    		//		
     
    		setSize(435, 290);
    		setVisible(true);
    	}
     
    	/** Add Component Without a Layout Manager (Absolute Positioning) */
    	private void addComponent(Container container,Component c,int x,int y,int width,int height)
    	{
    		c.setBounds(x,y,width,height);
    		container.add(c);
    	}
     
    	/** action event methods */
    	private void rockBn_actionPerformed(ActionEvent e)
    	{
                //play the game choosing rock
                play(ROCK);
    	}
     
    	private void paperBn_actionPerformed(ActionEvent e)
    	{
                //play the game choosing paper
                play(PAPER);
    	}
     
    	private void scissorBn_actionPerformed(ActionEvent e)
    	{
                //play the game choosing scissors
                play(SCISSORS);
    	}
            //method to play the game
            private void play(int pick){
                 String resultStr = "";                   
                 //random computer choice
                 cpu = rand.nextInt(CHOICE_MAX);
                 //nested if statments to determine winner loser or tie
                 if(pick == cpu){
                     ties++;
                     resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
                     resultStr += "\nWinner: Tie";
                     resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
                     resultTextArea.setText(resultStr);
                 }
                 else if( (pick == ROCK) && (cpu == SCISSORS) ){
                     wins++;
                     resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
                     resultStr += "\nWinner: You";
                     resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
                     resultTextArea.setText(resultStr);
                 }
                 else if( (pick == PAPER) && (cpu == ROCK) ){
                     wins++;
                     resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
                     resultStr += "\nWinner: You";
                     resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
                     resultTextArea.setText(resultStr);                 
                 }
                 else if( (pick == SCISSORS) && (cpu == PAPER) ){
                     wins++;
                     resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
                     resultStr += "\nWinner: You";
                     resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
                     resultTextArea.setText(resultStr);
                 }
                 else{
                     loses++;
                     resultStr = "You picked " + selection(pick) + " ---- Computer picked " + selection(cpu);
                     resultStr += "\nWinner: Computer";
                     resultStr += "\nYou: " + Integer.toString(wins) + "  " + "Computer: " + Integer.toString(loses) + " Ties: " + Integer.toString(ties);
                     resultTextArea.setText(resultStr);                 
                 }
     
            }
                //function returns paper rock or scissors as a string
                 public String selection(int choice){
                     String tempStr;
                     switch(choice){
                       case 0:
                             tempStr = "rock";
                             break;
                       case 1:
                             tempStr = "paper";
                             break;
                       case 2:
                             tempStr = "scissors";
                             break;
                       default:
                             tempStr = "invalid";
                      }
                      return tempStr;
                 }
     
    }

    I thought in theory, this would work to run it as an application.

    public class Game{
    	public static void main(String[] args)
    	{
    		JRockPaperScissors test = new JRockPaperScissors;
    	}
    }


    I am so stumped on what I did wrong...


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    You do need a main to run an application. In your main, you left out the parentheses:
    JRockPaperScissors test = new JRockPaperScissors();

    Also, you need to take out extends JApplet and replace it with extends JFrame (or whatever) and call the Super Constructor.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    iZigZag (May 8th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    Thank you so much!! I knew i needed fresh eyes on this. I had one question though for future reference. Now that everything runs correct, why does it hang for a bit? Or how can I figure that part out?

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    What do you mean by "hangs for a bit"? Does it just take some time to load, or does it stop responding for a few seconds and then comes back to life? If it is just taking its time, it could be your memory, or it could just be loading in the libraries and all of the overhead really slowly. If it stops responding, then we may have a threading problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    I think it is because i am getting errors when trying to use
    super(initializeComponent);
    right before
    public JRockPaperScissors(){
      	initializeComponent();
    	}
    Last edited by iZigZag; May 8th, 2012 at 04:35 PM.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    When you mention you are getting an error, it helps to tell us what kind of error. To call the JFrame Super Constructor, you can just say:
    public JRockPaperScissors(){
    super();
    initializeComponent();
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #7
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    java.lang.ClassCastException: JRockPaperScissors cannot be cast to java.applet.Applet
    	at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
    	at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
    	at sun.applet.AppletPanel.run(AppletPanel.java:368)
    	at java.lang.Thread.run(Thread.java:662)

    This is the error I get in debugging. I think this is the cause of the hanging.

  9. #8
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    This error I am getting, is it because JRockPaperScissors is not defined??

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    No, it still thinks its a JApplet. What line is the error being thrown from?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #10
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    java.lang.ClassCastException: JRockPaperScissors cannot be cast to java.applet.Applet
    	at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
    	at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
    	at sun.applet.AppletPanel.run(AppletPanel.java:368)
    	at java.lang.Thread.run(Thread.java:662)

    This is the error, and it seems like when it reads the JRockPaperScissors(). It does not error in complie, but it errors when you run as applet in debug.

  12. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    You can't run it as an applet. You have to run it as an application.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. #12
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    OHHHHHHHHHHHHHH!!!! Omg... I have been looking at this way to long....

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,322
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Program to run as Applet & Application - need fresh eye's simple mistake somewher

    A better way might be to move all the logic to a class that extends JPanel and then add an instance of the class to a class that extends JApplet or JFrame for the different ways to execute it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Fresh Meat
    By SerratedMind in forum Member Introductions
    Replies: 2
    Last Post: September 15th, 2011, 06:13 AM
  2. how come this program only loops twice? wheres my mistake?
    By Rainy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 3rd, 2011, 10:49 AM
  3. Replies: 2
    Last Post: March 23rd, 2011, 09:51 AM
  4. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM
  5. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:12 PM

Tags for this Thread