Help with Java Applet Hangman using Blue J

When applet is started it says applet not iniatlized

Page 1 of 1

11 Replies - 1456 Views - Last Post: 04 January 2010 - 07:31 PM Rate Topic: -----

#1 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Help with Java Applet Hangman using Blue J

Post icon  Posted 04 January 2010 - 06:19 PM

*Edited correctly formatted code follows in next topics (no need to duplicate it here)
*Edited unjustified "Intermediate" tag removed

This post has been edited by pbl: 04 January 2010 - 07:19 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Java Applet Hangman using Blue J

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 06:24 PM

Please, :code:
Also, please post your question in the body of your post and not the title, as your text gets truncated after so many characters.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 06:25 PM

Dude ::code: ! Nobody can read it as it is now!
Was This Post Helpful? 0
  • +
  • -

#4 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 06:41 PM

*Edited: uncorrectly taged code removed... good version in next topic

This post has been edited by pbl: 04 January 2010 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 06:57 PM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.util.Random;
import java.lang.String;

public class Hangman extends JApplet
	  implements ActionListener
{ 
   private JTextField wordbox, guessbox, guesslet, chances;
   private String word, wrongletters, starstring;
   private char[] stars;
   private int lengword, tries;
   private JRadioButton tletter,tword;
   
   
	  
   public void init()
   {
	  JPanel panel=new JPanel();
	  panel.setLayout(new GridLayout(4,2));
	  
	  panel.add(new JLabel("Word: "));
	  wordbox=new JTextField(5);
	  wordbox.setEditable(false);
	  panel.add(wordbox);
	  
	  tletter = new JRadioButton("Letter", true);
	  tword = new JRadioButton("Word");
	  ButtonGroup gr = new ButtonGroup();
	  gr.add(tletter);
	  gr.add(tword);
	  panel.add(guesslet);
	  panel.add(tword);
	  
	  panel.add(new JLabel("Guessed Letters: "));
	  guesslet=new JTextField(5);
	  guesslet.setEditable(false);
	  guesslet.setText("None");
	  panel.add(guesslet);
	  
	  JButton button=new JButton("Click to Guess");
	  button.addActionListener(this);
	  panel.add(button);
	  guessbox=new JTextField(5);
	  panel.add(guessbox);
	  
	  panel.add(new JLabel("Chances: "));
	  chances=new JTextField(5);
	  chances.setEditable(false);
	  panel.add(chances);
	  
	  JPanel pickpanel=new JPanel();
	  pickpanel.setLayout(new GridLayout(1,1));
	  
	  Container c=getContentPane();
	  c.add(panel, BorderLayout.NORTH);
	  c.add(pickpanel, BorderLayout.CENTER);
	 
   }
	 
   
   public void actionPerformed(ActionEvent e)
	{
	  String t, f ="";
	  t = guessbox.getText().trim().toLowerCase();
	  if (tletter.isSelected())
	  {
		 int i;
		 char letter;
		 boolean found=false;
		 
		 letter = t.charAt(0);
		 for (i=0; i<lengword; i++)
		 {
			if (letter == word.charAt(i))
			{
			  stars[i] = letter;
			  found = true;
			}
		 }
		   
		 if (found==false)
		 {
			wrongletters+=letter;
			tries++;
		 }
		 
		 
		
		 
			starstring = String.copyValueOf(stars);
			wordbox.setText(starstring);
			guesslet.setText(wrongletters);
			guessbox.setText("");
	   
	   }
	   
	   else // word.isSelected
	   {
		 
		  if (f.equals(word))
		  {
			guessbox.setText("You Win");
			guessbox.setEditable(false);
			

		  }  
	   }
	  
	   
			   
	}
	
	
	public void setup()
	{
	   int i;
	   word = getaword();
	   lengword = word.length();
	   stars = new char[lengword];
	   wrongletters = "";
	   for (i=0; i<lengword; i++)
	   stars[i] = '*';
	   starstring = String.copyValueOf(stars);
	   wordbox.setText(starstring);
	   

	}  
	
	public String getaword()
	{
	   Random r = new Random();
	   int num;
	   
	   String[] words = new String[20];
	   words[0]= "crew";
	   words[1]= "track";
	   words[2]= "field";
	   words[3]= "zune";
	   words[4]= "blackberry";
	   words[5]= "toyota";
	   words[6]= "gateway";
	   words[7]= "batman";
	   words[8]= "superman";
	   words[9]= "smallville";  
	   words[10]= "supernatural";
	   words[11]= "gryffindor";
	   words[12]= "hogwarts";
	   words[13]= "voldemort";
	   words[14]= "coxswain";
	   words[15]= "hudson";
	   words[16]= "nike";
	   words[17]= "pilgrims";
	   words[18]= "garcia";
	   words[19]= "string";
	   
	   
	   
	   num = r.nextInt(20);   
	   return words[num];
	}
}	



*Edited: code tags fixed

This post has been edited by pbl: 04 January 2010 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Re: Help with Java Applet Hangman using Blue J

Post icon  Posted 04 January 2010 - 07:05 PM

[ code]
your code here
[ /code]

without the space after the first [

*Edited duplicated merged code removed

This post has been edited by pbl: 04 January 2010 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:06 PM

Your Applet is not initialized because your init() method crashes on Null Pointer Exception at that line

panel.add(guesslet);

guesslet is not initialized
Was This Post Helpful? 0
  • +
  • -

#8 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:13 PM

View Postpbl, on 4 Jan, 2010 - 06:06 PM, said:

Your Applet is not initialized because your init() method crashes on Null Pointer Exception at that line

panel.add(guesslet);

guesslet is not initialized

After I changed it, the applet worked but it showed up messy. The words and the boxs were messed up.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:14 PM

View Postmacosxnerd101, on 4 Jan, 2010 - 09:24 PM, said:

Please, :code:
Also, please post your question in the body of your post and not the title, as your text gets truncated after so many characters.


In case you didn't read this in the other thread you opened.

Also, don't open duplicate topics. :angry:

This post has been edited by macosxnerd101: 04 January 2010 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:14 PM

My stack trace shows that your application is barfing here:
panel.add(guesslet);



For a NullPointerException. My guess is that you need tyo move that line after
guesslet=new JTextField(5);


Was This Post Helpful? 0
  • +
  • -

#11 tfo1857  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 10

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:19 PM

View Postmacosxnerd101, on 4 Jan, 2010 - 06:14 PM, said:

View Postmacosxnerd101, on 4 Jan, 2010 - 09:24 PM, said:

Please, :code:
Also, please post your question in the body of your post and not the title, as your text gets truncated after so many characters.


^^ In case you didn't read this in the other thread you opened. ^^

Also, don't open duplicate topics. :angry:

I am sorry, as I am new to the site.

View PostDogstopper, on 4 Jan, 2010 - 06:14 PM, said:

My stack trace shows that your application is barfing here:
panel.add(guesslet);



For a NullPointerException. My guess is that you need tyo move that line after
guesslet=new JTextField(5);


I do not understand what that means but I did change something that made the screen messy.
[/ tletter = new JRadioButton("Letter", true);
tword = new JRadioButton("Word");
ButtonGroup gr = new ButtonGroup();
gr.add(tletter);
gr.add(tword);
panel.add(tletter);
panel.add(tword);]
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Help with Java Applet Hangman using Blue J

Posted 04 January 2010 - 07:31 PM

View PostDogstopper, on 4 Jan, 2010 - 06:14 PM, said:

My stack trace shows that your application is barfing here:
panel.add(guesslet);



For a NullPointerException. My guess is that you need tyo move that line after
guesslet=new JTextField(5);



What that mean is this:
 panel.add(guesslet); <-- Add a null guesslet here...
	  panel.add(tword);
	 
	  panel.add(new JLabel("Guessed Letters: "));
	  guesslet=new JTextField(5);
	  guesslet.setEditable(false);
	  guesslet.setText("None");
	  panel.add(guesslet); <-- Add an instantiated guesslet here...



Just remove the first line.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1