(The instructions for this are actually below, but I just need the code.) Write
ID: 3538406 • Letter: #
Question
(The instructions for this are actually below, but I just need the code.)
Write an application that plays "guess the number" as follows:
Your application chooses the number to be guessed by selecting an integer at random in the range 1-1000. The application then displays the following in a label: I have a number between 1 and 1000. Can you guess my number? Please enter your first guess. A JTextField should be used to input the guess. 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, "colder". A JLabel should display either "Too High" or "Too Low" to help the user zero in. When the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for input should be changed to be uneditable. A JButton should be provided to allow the user to play the game again.When the JButton is clicked, a new random number should be generated and the input JTextField changed to be editable.
-----------------------------------------------
INSTRUCTIONS:
This exercise requires you to build just the GUI; no logic behind the GUI is required.
Start by creating a class that inherits from JFrame. I will leave it up to you to come up with the
naming conventions for your variables and such. In the constructor of your class that extends
the JFrame you will be creating five (5) JPanels to house your components.
1) Panel 1 - Create two checkboxes, a panel, set the layout for a GridLayout(2,1) and add the
two checkboxes to the panel.
2) Panel 2 - Create a label for the "X:", create a textfield for "8" with a width of 3, create a
panel, set the layout to FlowLayout; add the label and textfield to the panel.
3) Panel 3 - do the same thing as Panel 2 for this panel but make the label "Y: ". This is also a
FlowLayout.
4) Panel 4 - create another panel to house Panel 2 and 3. Set the layout to BorderLayout, add
Panel 2 and 3 to the panel using BorderLayout.NORTH and SOUTH.
5) Now, let's build the button panel, Panel 5. Create the three buttons "Ok", "Cancel", and
"Help". Create a panel, set the panel to GridLayout(3, 1, 10, 5); add the buttons to the
panel.
6) Lastly, add panels 1, 4, and 5 to the inherited JPanel for your class. HINT: you only have to
override the method so just use "add(your-panel-name);"
7) Create a test class to create an object of your GUI, set the default close operation to
EXIT_ON_CLOSE, set the size accordingly, and set the visibility to "true".
-----------------------------------------------------
1) Create a class, call it what you like, that inherits from JFrame. In the constructor, set the
layout immediately to BorderLayout (i.e. "setLayout( new BorderLayout );"
2) Create a combo box, add the item "RED" to the box; add the combo box to the frame by
overriding the add method "add (your-combo-box, BorderLayout.NORTH);"
3) Now, create a new JPanel. Add two check boxes "Background" and "Foreground". Add the
checkboxes to the panel ("panel.add(checkbox-name);"), add the panel, with the
checkboxes, to the JFrame you inherited (i.e. "add ( your-panel, BorderLayout.CENTER);").
4) Lastly, let's us create the two buttons "Ok" and "Cancel"; create a new panel, add the
buttons to the panel; add the panel to the JFrame of your class that you inherited by
overriding the add method (as in step 3) only this panel will be SOUTH.
Explanation / Answer
// Guess the number
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuessGame extends JFrame {
private int number, guessCount;
private int lastDistance;
private JTextField guessInput;
private JLabel prompt1, prompt2, message;
private JButton newGame;
private Color background;
Container container;
// set up GUI and initialize values
public GuessGame()
{
super( "Guessing Game" );
guessCount = 0;
background = Color.lightGray;
// create GUI components
prompt1 = new JLabel( "I have a number between 1 and 1000." );
prompt2 = new JLabel(
"Can you guess my number? Enter your Guess:" );
guessInput = new JTextField( 5 );
guessInput.addActionListener( new GuessHandler( ) );
message = new JLabel( "Guess result appears here." );
// button starts a new game
newGame = new JButton( "New Game" );
newGame.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
message.setText( "Guess Result" );
guessInput.setText( "" );
guessInput.setEditable( true );
background = Color.lightGray;
theGame();
repaint();
}
}
);
// add components to JFrame
container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt1 );
container.add( prompt2 );
container.add( guessInput );
container.add( message );
container.add( newGame );
setSize( 300, 150 );
setVisible( true );
theGame();
}
// choose a new random number
public void theGame()
{
number = ( int ) ( Math.random() * 1000 + 1 );
}
// change background color
public void paint( Graphics g )
{
super.paint( g );
container.setBackground( background );
}
// react to new guess
public void react( int guess ) {
guessCount++;
int currentDistance = 1000;
// first guess
if ( guessCount == 1 ) {
lastDistance = Math.abs( guess - number );
if ( guess > number )
message.setText( "Too High. Try a lower number." );
else
message.setText( "Too Low. Try a higher number." );
}
else {
currentDistance = Math.abs( guess - number );
// guess is too high
if ( guess > number ) {
message.setText( "Too High. Try a lower number." );
background = ( currentDistance < = lastDistance ) ?
Color.red : Color.blue;
lastDistance = currentDistance;
}
// guess is too low
else if ( guess < number ) {
message.setText( "Too Low. Try a higher number." );
background = ( currentDistance <= lastDistance ) ?
Color.red : Color.blue;
lastDistance = currentDistance;
}
// guess is correct
else {
message.setText( "Correct!" );
background = Color.lightGray;
guessInput.setEditable( false );
guessCount = 0;
}
repaint();
}
} // end method react
public static void main( String args[] )
{
GuessGame app = new GuessGame();
app.setDefaultCloseOperation( EXIT_ON_CLOSE );
}
// inner class acts on user input
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
int guess = Integer.parseInt( guessInput.getText() );
react( guess );
}
} // end inner class GuessHandler
} // end class GuessGame
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.