package guessinggame;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
//create class GuessingGame that extends to JFrame
public class GuessingGame extends JFrame {
public static void main(String args[])
{
//define variables
private JLabel instruction1JLabel;
private JLabel instruction2JLabel;
private JLabel instruction3JLabel;
private JLabel outputJLabel;
private JTextField inputJTextField;
private JButton playAgainJButton;
private int myGuess = 0;
private int previousDistance = 0;
private int currentDistance = 0;
private int randomNumber = ((int)((Math.random() * 1000) + 1));
//Constructor
public GuessingGame()
{
super( "Guessing Game" );
setLayout( new FlowLayout() );
System.out.println(myGuess);
System.out.println(currentDistance);
System.out.println(previousDistance);
System.out.println("random number is " + randomNumber);
JLabel instruction1JLabel = new JLabel( "I have a number between 1 and 1000." );
add( instruction1JLabel );
JLabel instruction2JLabel = new JLabel( "Can you guess my number?" );
add ( instruction2JLabel);
JLabel instruction3JLabel = new JLabel( "Please enter your first guess\n");
add( instruction3JLabel );
inputJTextField = new JTextField( 12 );
add( inputJTextField );
outputJLabel = new JLabel( " " );
add( outputJLabel );
JButton playAgainJButton = new JButton( "Play Again" );
add ( playAgainJButton );
InputTextFieldHandler inputTextFieldHandler = new InputTextFieldHandler();
inputJTextField.addActionListener( inputTextFieldHandler );
PlayAgainButtonHandler playAgainButtonHandler = new PlayAgainButtonHandler();
playAgainJButton.addActionListener( playAgainButtonHandler );
}
// inner class for input event handling
private class InputTextFieldHandler implements ActionListener
{
// handle input event
public void actionPerformed( ActionEvent event )
{
myGuess = Integer.parseInt(inputJTextField.getText());
currentDistance = Math.abs(myGuess - randomNumber);
previousDistance = currentDistance;
inputJTextField.setBackground( Color.WHITE );
outputJLabel.setText( " " );
inputJTextField.setText( "" );
inputJTextField.setEditable( false );
inputJTextField.requestFocus();
if ( previousDistance != 0 )
{
if ( currentDistance == previousDistance )
inputJTextField.setBackground( Color.WHITE );
else if ( currentDistance < previousDistance )
inputJTextField.setBackground( Color.RED );
else
inputJTextField.setBackground( Color.BLUE );
}
if ( myGuess == randomNumber )
{
outputJLabel.setText( "Correct!" );
inputJTextField.setBackground( Color.BLUE );
inputJTextField.setEditable( true );
}
else if ( myGuess < randomNumber )
outputJLabel.setText( "Too Low" );
else
outputJLabel.setText( "Too High" );
inputJTextField.selectAll();
} // end method actionPerformed
} // end private inner class InputTextFieldHandler
// inner class for playagainbutton event handling
private class PlayAgainButtonHandler implements ActionListener
{
// handle button event
public void actionPerformed( ActionEvent event )
{
myGuess = 0;
previousDistance = 0;
currentDistance = (myGuess - randomNumber);
inputJTextField.setBackground( Color.WHITE );
outputJLabel.setText( " " );
inputJTextField.setText( "" );
inputJTextField.setEditable( true );
inputJTextField.requestFocus();
} //end method actionPerformed
} // end private inner class PlayAgainButtonHandler
}// end class TextFieldFrame
import javax.swing.JFrame;
public class GuessingGameTest
{
public static void main(String args[])
{
GuessingGame guessingGame = new GuessingGame();
guessingGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessingGame.setSize(250, 200);
guessingGame.setVisible(true);
}
}
kylealb007 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.