Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Need a little insight as to why this does not work (My code has issues)
Need a little insight as to why this does not work [message #933049] Thu, 04 October 2012 13:33 Go to next message
Ronda Wilson is currently offline Ronda Wilson
Messages: 1
Registered: October 2012
Junior Member

Hi guys I am having a problem with my Java code. I was hoping maybe someone here could help me out with it. I am totally new to programming with java so don't be too harsh.
This is supposed to be a guessing game where the user guesses the number. Here are the specifics:
Your program chooses the number to be guessed by selecting an integer at random in the range 11000. The program then displays the following in a label.

I have a number between 1 and 1000 -- can you guess my number?

Please enter your guess: ____________________

A JTextField should be used to input the guess. A JButton must be provided to allow the user to press each time a guess is entered. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting warmer, and blue indicates that the user is getting colder. A JLabel should display either Too High or Too Low to help the user zero in on the correct answer. When the user gets the correct answer, Correct! should be displayed, and the JTextField used for input should be cleared and changed to be uneditable. Also, a JButton should be provided to allow the user to play the game again. When the New Game JButton is clicked, a new random number should be generated and the input JTextField changed to be editable. A JButton must be provided to allow the user to exit the application. Also provide a count of the number of guesses the user entered when the correct number is guessed.

This is the code I have put together:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuessGame extends JFrame
{
	private static JFrame mainFrame;
	private static JButton tryButton;
	private static JButton newgameButton;
	private static JButton exitButton;
	private static JTextField guessField;
	private static JLabel guessLabel;
	private static JLabel headerLabel;
	private static JTextField results;
	private static JPanel panel;
	private static int randomNumber;
	
	
	public GuessGame() 
	{
		mainFrame = new JFrame ("Guessing Game");
		tryButton = new JButton ("Try the number");
		exitButton = new JButton ("Exit");
		newgameButton = new JButton ("New Game");
		guessLabel = new JLabel ("Enter your guess:");
		headerLabel = new JLabel(
				"<html>I have a number between 1 and 1000,<br>can you guess the number?</html>");
		guessField = new JTextField(5);
		results = new JTextField(25);
		panel = new JPanel();
		

		mainFrame.addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{
				System.exit(0);
			}
			
		});

		TryButtonHandler tbHandler = new TryButtonHandler();
		tryButton.addActionListener(tbHandler);

		ExitButtonHandler eHandler = new ExitButtonHandler();
		exitButton.addActionListener(eHandler);

		NewGameButtonHandler ngHandler = new NewGameButtonHandler();
		newgameButton.addActionListener(ngHandler);

		tryButton.setMnemonic('T');
		exitButton.setMnemonic('E');
		newgameButton.setMnemonic('N');

		results.setBackground(Color.WHITE);
		results.setEditable(false);
		results.setText("Too high/Too low message will go here!");

		Container c = mainFrame.getContentPane();
		c.setLayout(new BorderLayout());
		panel.setLayout(new FlowLayout(FlowLayout.CENTER));

		panel.add(headerLabel);
		panel.add(guessLabel);
		panel.add(guessField);
		panel.add(tryButton);
		panel.add(exitButton);
		panel.add(newgameButton);
		panel.add(results);
		c.add(panel);

		mainFrame.setSize(325, 210);
		mainFrame.setVisible(true);
	}

	public class TryButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			int userInput=0;
			int diff;
			int Difference;
			
			try 
			{
				userInput = Integer.parseInt(
						guessField.getText().trim());
			} 
			
			catch (Exception ex)
			{
				results.setText("Enter a VALID number!");
				return;
			}
			
			if (userInput == randomNumber){
				JOptionPane.showMessageDialog(null, "CONGRATULATIONS! You got it!!",
						"Random Number: " + randomNumber, JOptionPane.INFORMATION_MESSAGE);
				randomNumber = new Random().nextInt(1000) + 1;
				return;
			}
			
			if (userInput > randomNumber){
				results.setText( "Too High. Try a lower number." );
				diff=userInput - randomNumber;
				Difference=Math.abs(diff);
			} 
			
			else 
			{
				results.setText( "Too Low. Try a higher number." );
				diff=randomNumber - userInput;
				Difference=Math.abs(diff);
			}
			
			if(Difference<=25)
			{
				results.setText("You're getting colder!");
				setBackground(Color.blue);
				repaint();
			}

			if(Difference<=10){
				results.setText("You're getting warmer!");
				setBackground(Color.red);
				repaint();
			}

			
		}
		
	}

	static class ExitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			System.exit(0);

		}

	}

	public class NewGameButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			SwingUtilities.invokeLater(new Runnable() 
			{
				public void run() 
				{
					GuessGame GuessNumber = new GuessGame();
					GuessNumber.setVisible(true);
				}
				
			});
		}

	}

	private void setBackgroundColor(Color color)
	{
		panel.setBackground(color);
		panel.setBackground(color);
		panel.setBackground(color);
	}
	
	
	public static void main(String[] args)
	{
		new GuessGame();
	}
			
		
}
	


I don't get any errors but it's not behaving right. For instance I don't believe the random number is generating and the colors of the panel don't change when they are supposed to.

I really appreciate any help I can get on this.
Thank you


I am not awesome because I have never failed...
I am awesome because failure has never stopped me.
Re: Need a little insight as to why this does not work [message #934974 is a reply to message #933049] Sat, 06 October 2012 10:28 Go to previous message
Bernard SARTER is currently offline Bernard SARTER
Messages: 60
Registered: August 2011
Location: Paris, France
Member
Hi,

Not sure that your posting in the right forum ... having said that:

1) your random number is never set, add eg.
randomNumber = (int) (Math.random() * 1000d) in your constructor and newGame stuff, and remove the 'static' statement.

2) You have to precise the object on which you want to change the background colour: eg panel.setBackground(Color.blue) or tryButton.setBackground(Color.blue) or ...

Hope that helps,
Bernard.
Previous Topic:issue - print messages in eclipse console
Next Topic:SubClipse Question
Goto Forum:
  


Current Time: Thu Apr 17 14:41:45 EDT 2014

Powered by FUDForum. Page generated in 0.01647 seconds