package GuessingGame; import java.awt.Color; import java.awt.FlowLayout; import
ID: 3690398 • Letter: P
Question
package GuessingGame;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
//import java.lang.Math;
//import javax.swing.JOptionPane;
public class GuessGameFrame extends JFrame
{
private static Random generator = new Random();
private int number;// created number
final int MAX = 1000; //sets max number
private String userGuess;
private FlowLayout layout;
private Container container;
private int guess; //user guess
private int guessCount; //number of guesses
private int lastDistance; //distance between last guess and number
private int currentDistance; //distance that sets background color
private JTextField guessInputJTextField;// field for user guess
private JLabel prompt1JLabel;// indicator to user
private JLabel prompt2JLabel; //indicator to user
private JLabel messageJLabel; //shows message of game status
private JButton newGameJButton; //starts a new game
private Color background; //background color
// GuessGameFrame constructor set up GUI and initialize values
public GuessGameFrame()
{
super ( "Guessing Game" );
guessCount = 0; //sets guess to zero
background = Color.MAGENTA; //sets background to magenta
prompt1JLabel = new JLabel( " I have a number between 1 and 1000."); //describe game
prompt2JLabel = new JLabel( " Can you guess my number? Enter your Guess:"); //prompt user
guessInputJTextField = new JTextField ( 10 ); //to enter guesses
guessInputJTextField.addActionListener( new GuessHandler() );
messageJLabel = new JLabel ( "Guess result appears here." );
layout = new FlowLayout();
container = getContentPane();
setLayout( layout );
newGameJButton = new JButton( "New Game" ); //Creates a new game button
newGameJButton.addActionListener
( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
guessCount = 0; background = Color.MAGENTA;
prompt1JLabel = new JLabel( " I've a number between 1 and 1000.");// describe game
prompt2JLabel = new JLabel( " Can you guess my number? Enter your Guess:");// prompt user 0
revalidate();
repaint();
} // end method actionPerfomed
} // end anonymous inner class
); //end call to addActionListener
layout.setAlignment( FlowLayout.CENTER );
layout.layoutContainer( container );
add(prompt1JLabel); // add prompt1JLabel to JFrame
add(prompt2JLabel); // add prompt2JLabel to JFrame
add(guessInputJTextField) ; // add InputJTextField to JFrame
add(messageJLabel); // add messageJLabel to JFrame
add ( newGameJButton ); //add new game button to JFrame
theGame();
}// end GuessGameFrame constructor
public void theGame() //choose a new random number
{
int guess = 0;
int number = 0;
Random generator = new Random();
number = generator.nextInt(MAX) + 1;
}
// change background color
public void paint ( Graphics g )
{
super.paint( g );
getContentPane().setBackground( background );
//set background
} //end method paint
//react to new guess
public void react( int guess )
{
guessCount++; //increment guesses
lastDistance = 1000;
//first guess if ( guessCount == 1)
{
lastDistance = Math.abs( guess - number );
if ( guess > number )
messageJLabel.setText( "Too High. Try a lower number.");
else messageJLabel.setText( "Too Low. Try a higher number.");
} //end if else
{
currentDistance = Math.abs( guess - number );
if ( guess > number )
{
messageJLabel.setText(" Too High. Try a lower number.");
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.BLUE;
lastDistance = currentDistance;
}// end if
else if ( guess < number )
{
messageJLabel.setText( "Too low. Try a higher number.");
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.ORANGE;
lastDistance = currentDistance;
}// end else if
else //guess is correct
{ messageJLabel.setText ( "Correct "); background = ( currentDistance <= lastDistance ) ? Color.RED : Color.GREEN;
} //end else
repaint();
}// end else
} //end method react
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String inputString; inputString = guessInputJTextField.getText();
guess = Integer.parseInt( inputString );
react(guess);
}// end method actionPerformed
}// end inner class GuessHandler
} //end class GuessGameFrame
/*Main*/
package GuessingGame;
import javax.swing.JFrame;
public class GuessGame
{ public static void main( String args[] )
{
GuessGameFrame guessGameFrame = new GuessGameFrame();
guessGameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
guessGameFrame.setSize( 300, 150 ); //set frame size guessGameFrame.setVisible( true ); //display frame
}// end main
}// end class GuessGame
1. Just start the game the background color of JFram window is magenta. The first line of the window contains JLabel messages "I have a number between 1 and 1000.”. The 2nd line contain “Can you guess my number? Enter your first Guess:." JLabel message. A JText filed should be at the beginning of third line and follow by a “Guess result appears here." JLabel, and a “New Game” Jbutton. 2. As each guess is input, the background color should change to either red or orange. Red indicates that the user is getting closer (“warmer”) to the answer and orange indicates that the user is getting farther (“colder”) to the answer. At this stage the number guessed should be displayed at the top of the window. The second line should still display “Can you guess my number? Please enter your first guess.” JLabel message. A JText filed should be at the beginning of third line and follow by a “Too high..” or “To low ..” JLabel message, and a “New Game” Jbutton. 3. When the number is guessed the background will be in green color. The first line of the window displays "After # trials you get the number # correct." JLabel message. The 2nd line displays "You are great! Try again? Click "New Game" button.”. :." A JText filed should be at the beginning of third line and follow by a “Correct." JLabel, and a “New Game” Jbutton. 4. When the “New Game” Jbutton clicked the window will be in yellow color. The first line of the window contains JLabel messages "I have a number between 1 and 1000.”. The 2nd line contain “Can you guess my number? Enter your first Guess:." JLabel message. A JText filed should be at the beginning of third line and follow by a “Guess result appears here." JLabel, and a “New Game” Jbutton. Create a driver to display the capability of the program designed.
???? Even I entered 1 as a first guess, the title would still too high. After the first guess, the color didnt change (still magenta) to either orange or red. The program stop at the third guess. I'm not sure if it change to green. Does this program contains include this statement if the answer is correct? I want to display message for each guess ex: Please enter your 3 ... 4 .. after you guess wrong?
Explanation / Answer
package GuessingGame;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
public class GuessGameFrame extends JFrame
{
private static Random generator = new Random();
private int number;
final int MAX = 1000;
private String userGuess;
private FlowLayout layout;
private Container container;
private int guess; //user guess
private int guessCount; //number of guesses
private int lastDistance; //distance between last guess and number
private int currentDistance; //distance that sets background color
private JTextField guessInputJTextField;// field for user guess
private JLabel prompt1JLabel;// indicator to user
private JLabel prompt2JLabel; //indicator to user
private JLabel messageJLabel; //shows message of game status
private JButton newGameJButton; //starts a new game
private Color background; //background color
// GuessGameFrame constructor set up GUI and initialize values
public GuessGameFrame()
{
super ( "Guessing Game" );
guessCount = 0; //sets guess to zero
background = Color.MAGENTA; //sets background to magenta
prompt1JLabel = new JLabel( " I have a number between 1 and 1000."); //describe game
prompt2JLabel = new JLabel( " Can you guess my number? Enter your Guess:"); //prompt user
guessInputJTextField = new JTextField ( 10 ); //to enter guesses
guessInputJTextField.addActionListener( new GuessHandler() );
messageJLabel = new JLabel ( "Guess result appears here." );
layout = new FlowLayout();
container = getContentPane();
setLayout( layout );
newGameJButton = new JButton( "New Game" ); //Creates a new game button
newGameJButton.addActionListener
( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
guessCount = 0; background = Color.MAGENTA;
prompt1JLabel = new JLabel( " I've a number between 1 and 1000.");// describe game
prompt2JLabel = new JLabel( " Can you guess my number? Enter your Guess:");// prompt user 0
revalidate();
repaint();
} // end method actionPerfomed
} // end anonymous inner class
); //end call to addActionListener
layout.setAlignment( FlowLayout.CENTER );
layout.layoutContainer( container );
add(prompt1JLabel); // add prompt1JLabel to JFrame
add(prompt2JLabel); // add prompt2JLabel to JFrame
add(guessInputJTextField) ; // add InputJTextField to JFrame
add(messageJLabel); // add messageJLabel to JFrame
add ( newGameJButton ); //add new game button to JFrame
theGame();
}// end GuessGameFrame constructor
public void theGame() //choose a new random number
{
int guess = 0;
int number = 0;
Random generator = new Random();
number = generator.nextInt(MAX) + 1;
}
// change background color
public void paint ( Graphics g )
{
super.paint( g );
getContentPane().setBackground( background );
//set background
} //end method paint
//react to new guess
public void react( int guess )
{
guessCount++; //increment guesses
lastDistance = 1000;
//first guess if ( guessCount == 1)
{
lastDistance = Math.abs( guess - number );
if ( guess > number )
messageJLabel.setText( "Too High. Try a lower number.");
else messageJLabel.setText( "Too Low. Try a higher number.");
} //end if else
{
currentDistance = Math.abs( guess - number );
if ( guess > number )
{
messageJLabel.setText(" Too High. Try a lower number.");
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.BLUE;
lastDistance = currentDistance;
}// end if
else if ( guess < number )
{
messageJLabel.setText( "Too low. Try a higher number.");
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.ORANGE;
lastDistance = currentDistance;
}// end else if
else //guess is correct
{ messageJLabel.setText ( "Correct "); background = ( currentDistance <= lastDistance ) ? Color.RED : Color.GREEN;
} //end else
repaint();
}// end else
} //end method react
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String inputString; inputString = guessInputJTextField.getText();
guess = Integer.parseInt( inputString );
react(guess);
}// end method actionPerformed
}// end inner class GuessHandler
} //end class GuessGameFrame
/*Main*/
package GuessingGame;
import javax.swing.JFrame;
public class GuessGame
{ public static void main( String args[] )
{
GuessGameFrame guessGameFrame = new GuessGameFrame();
guessGameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
guessGameFrame.setSize( 300, 150 ); //set frame size guessGameFrame.setVisible( true ); //display frame
}// end main
}// end class GuessGame
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.