Check your speed in select the Checkboxes

In this game we check the speed because when we click on START button the time will start,and the counting will also start means how many checkboxes you have clicked in 30 second.

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class games extends JApplet implements ActionListener,Runnable,ItemListener/*runnable for multithreading,itemlistener for checkboxes and actionlistener for button*/

{

                JCheckBox chk[];   //checkboxes

                JLabel l,t1,l1,t2,name,nam;

                JFrame jf;

                JButton b1,b2,b3,ok;

                JTextField na;

                int b=0,r;

                Thread th;

                String st="Time Over ";

                Panel p;

                JDialog dia; //dialog box

                games()

                {

                jf=new JFrame("Game");

                t2=new JLabel("30");

                t1=new JLabel("");

                nam=new JLabel("");

                name=new JLabel("Enter Your Name");

                l1=new JLabel(" your Score is");

                b1=new JButton("Start");

                b2=new JButton("Reset");

                b3=new JButton("Exit");

                ok=new JButton("ok");

                ok.addActionListener(this);

                dia=new JDialog(jf,"welcome",true);

                dia.getContentPane().setBackground(Color.cyan);

                na=new JTextField(10);

                chk=new JCheckBox[100];

                p=new Panel();

                l=new JLabel("Ur Time   Remain ");

                Font f=new Font("Lucida Console",Font.BOLD,20);//set font to the textfield

                jf.getContentPane().setBackground(Color.cyan);

                t1.setFont(f);

                na.setFont(f);

                nam.setFont(f);

                nam.setForeground(Color.red);

                l.setFont(f);

                t1.setForeground(Color.yellow);

                t2.setForeground(Color.red);

                t2.setFont(f);

                for(r=0;r<100;++r)

                {

                chk[r]=new JCheckBox();

                jf.add(chk[r]);

                chk[r].setEnabled(false);

                chk[r].addItemListener(this);//item listener is used for checkboxes

                }

                p.add(name);p.add(na);p.add(ok);

                Container dcont=dia.getContentPane();

                dcont.add(p);

                dia.setSize(300,150);

   dia.show();    

                jf.add(nam);

                jf.add(l1);

                jf.add(t1);

                jf.add(l);jf.add(t2);                         

                jf.add(b1);jf.add(b2);jf.add(b3);

                jf.setLayout(new FlowLayout());

                jf.setSize(286,500);

                jf.setVisible(true);

                b1.addActionListener(this);

                b2.addActionListener(this);

                b3.addActionListener(this);

                }

                public void run()

                {

                try

                {

                for(int i=30;i>=0;i--)

                {

                   t2.setText(String.valueOf(i));

                   th.sleep(500);

                   if(i==0)

                                {

                                b3.setEnabled(true);

                                b2.setEnabled(true);

                                JOptionPane.showMessageDialog(null,st);

                                for(r=0;r<100;++r)

                                {

                                chk[r].setEnabled(false);

                                }

                                }

                }}

                catch(InterruptedException ee)

                {}

                }

                public void itemStateChanged(ItemEvent ef)   //item event performed on checkboxes

                {             

                for(r=0;r<=100;r++)

                {

                if(chk[r].isSelected())//checkboxes selected

                {              

                b=b+1;

                chk[r].setEnabled(false);

                t1.setText(String.valueOf(b));

                   break;               

                }            

                }}

                public void actionPerformed(ActionEvent ae)//for button b1,b2,b3

                {

                if(ae.getSource()==ok)

                {

                nam.setText(na.getText());

                dia.dispose();

                System.out.print("ok pressed");

                }

                   if(ae.getSource()==b1)

                {

         //when we click on b1 then b2,b3 set enable

                   b2.setEnabled(false);

                   b3.setEnabled(false);

                   b1.setEnabled(false);

                  

                th=new Thread(this,"first");

                th.start();

                for(r=0;r<100;++r)

                                {

                chk[r].setEnabled(true);

                }

                }

                   if(ae.getSource()==b2)

                {

                   for(r=0;r<100;++r)

                {

                   chk[r].setSelected( false );

                   chk[r].setEnabled(false);

                }

                   b=0;

                t1.setText(" ");

                t2.setText(" ");

                b1.setEnabled(true);

                if(th.isAlive())

                {

                th.stop();

                th=null;

                }

                }

                if(ae.getSource()==b3)

                {

                   System.exit(0);

                   }

                }

                                public static void main(String... ach)

                                {

                                games nok=new games();

                                }

}

select the checkboxes

Explanation….

When we compile this program the message box will appear and when we enter the name the name will show in the game(means who are playing).

In above code we use “start” button to start the game, when we click on this button the time(30 sec.)will start and also the counting will start (how many checkboxes click in 30 sec).By using “reset “ again the game will ready to start and when we click on start button the time will start.