hi pliz help me put a shape in this code such that when the answer is equal and right it shuld change the color of the background and puts a shape. pliz help its urgent
import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Random; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import java.awt.Color; import java.awt.Container;
//****************** public class MathsClass extends JFrame { private JPanel components; private JPanel buttonPanel; private JLabel number,square; private JFormattedTextField numberField; private JFormattedTextField squareField; private JButton okButton; private NumberFormat numberFormatter; private JPanel panel1; private JButton button; Random randomNumber=new Random(); int randomNO; int randomSquare;
//********************** //MathsClass constructor public MathsClass() {
super("Mathematical Tutor"); setLayout(new FlowLayout());//set frame layout setFormats(); //its meant to set the background color of the frame Container winnie = getContentPane(); winnie.setBackground(Color.blue); components=new JPanel(new GridLayout(2,3,0,3)); components.setBorder(new EmptyBorder(20,20,20,20)); components.setPreferredSize(new Dimension(350,100)); buttonPanel=new JPanel(); buttonPanel.setBorder(new EmptyBorder(20,0,0,10));//set size of the EmptyBorder number= new JLabel("Number:"); square= new JLabel("Number Squared:"); numberField=new JFormattedTextField(); numberField.setEditable(false);//only gets input from the computer numberField.setValue((generateRandom())); squareField=new JFormattedTextField(numberFormatter);//put only integers in the textfield okButton=new JButton("Ok"); okButton.addActionListener ( //******************************************************************* new ActionListener() { public void actionPerformed(ActionEvent evt) { int num=((Number)numberField.getValue()).intValue(); int input=((Number)squareField.getValue()).intValue(); //calculation of squares of the random numbers randomSquare=(num*num); if(input==(randomSquare)) { //display the color of the OptionPane background UIManager.put("OptionPane.background", Color.cyan); JOptionPane.showMessageDialog(MathsClass.this,"Huraah, the answer is right", "The Square of the number.",JOptionPane.INFORMATION_MESSAGE ); } else { //remove the background color UIManager.put("OptionPane.background", "null"); //used to display the message JOptionPane.showMessageDialog(MathsClass.this,"Sorry the answer is Wrong . " + "The right answer is:" + randomSquare, "The Square of the number.",JOptionPane.INFORMATION_MESSAGE ); } numberField.setValue((generateRandom())); // to clear automatically squareField.setValue(null); } } ); components.add(number);//add number to JFrame components.add(numberField);//add numberfield to JFrame components.add(square);//add square to JFrame components.add(squareField);//add squarefield to JFrame buttonPanel.add(okButton);// add the ok button to JFrame components.setBackground(Color.magenta);//set background color of the components buttonPanel.setBackground(Color.blue);//set background color of the frame winnie.add(components);//add components to JFrame winnie.add(buttonPanel);//add buttonPanel to JFrame }
//********************* //to generate random numbers private int generateRandom() { //numbers between 1 and 10 randomNO=1+ randomNumber.nextInt(9); return randomNO; }
//setFormats constructor private void setFormats() {
numberFormatter=(DecimalFormat)NumberFormat.getNumberInstance(); numberFormatter.setParseIntegerOnly(true);//convert string into int values in the calculation }
//*********************** //main program public static void main(String[] args) {
MathsClass mathsClass = new MathsClass();//create MathsClass mathsClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mathsClass.setSize(400,350);//set frame size mathsClass.setResizable(false);//inability to minimize mathsClass.setVisible(true);//display frame } }