A small Java Catch Game with Source Code

Source Code : 

4 Files : 

  1. Ball.gif
  2. CatchBox.form
  3. CatchBox.java
  4. Main.java

CatchBox.form
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
  <NonVisualComponents>
    <Menu class="javax.swing.JMenuBar" name="jMenuBar1">
      <SubComponents>
        <Menu class="javax.swing.JMenu" name="jMenu3">
          <Properties>
            <Property name="text" type="java.lang.String" value="File"/>
          </Properties>
          <SubComponents>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem11">
              <Properties>
                <Property name="text" type="java.lang.String" value="RESTART"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem11ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem13">
              <Properties>
                <Property name="text" type="java.lang.String" value="PAUSE"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem13ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem12">
              <Properties>
                <Property name="text" type="java.lang.String" value="EXIT"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem12ActionPerformed"/>
              </Events>
            </MenuItem>
          </SubComponents>
        </Menu>
        <Menu class="javax.swing.JMenu" name="jMenu1">
          <Properties>
            <Property name="text" type="java.lang.String" value="Level"/>
          </Properties>
          <Events>
            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenu1ActionPerformed"/>
          </Events>
          <SubComponents>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem1">
              <Properties>
                <Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
                  <KeyStroke key="1"/>
                </Property>
                <Property name="text" type="java.lang.String" value="Level 1"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem1ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem2">
              <Properties>
                <Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
                  <KeyStroke key="2"/>
                </Property>
                <Property name="text" type="java.lang.String" value="Level 2"/>
              </Properties>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem3">
              <Properties>
                <Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
                  <KeyStroke key="3"/>
                </Property>
                <Property name="text" type="java.lang.String" value="Level 3"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem3ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem4">
              <Properties>
                <Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
                  <KeyStroke key="4"/>
                </Property>
                <Property name="text" type="java.lang.String" value="Level 4"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem4ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem5">
              <Properties>
                <Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
                  <KeyStroke key="5"/>
                </Property>
                <Property name="text" type="java.lang.String" value="Level 5"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem5ActionPerformed"/>
              </Events>
            </MenuItem>
          </SubComponents>
        </Menu>
        <Menu class="javax.swing.JMenu" name="jMenu2">
          <Properties>
            <Property name="text" type="java.lang.String" value="Time"/>
          </Properties>
          <SubComponents>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem6">
              <Properties>
                <Property name="text" type="java.lang.String" value="20 Seconds"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem6ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem7">
              <Properties>
                <Property name="text" type="java.lang.String" value="30 Seconds"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem7ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem8">
              <Properties>
                <Property name="text" type="java.lang.String" value="40 Seconds"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem8ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem9">
              <Properties>
                <Property name="text" type="java.lang.String" value="1 Minutes"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem9ActionPerformed"/>
              </Events>
            </MenuItem>
            <MenuItem class="javax.swing.JMenuItem" name="jMenuItem10">
              <Properties>
                <Property name="text" type="java.lang.String" value="2 Mintutes"/>
              </Properties>
              <Events>
                <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem10ActionPerformed"/>
              </Events>
            </MenuItem>
          </SubComponents>
        </Menu>
      </SubComponents>
    </Menu>
  </NonVisualComponents>
  <Properties>
    <Property name="defaultCloseOperation" type="int" value="0"/>
  </Properties>
  <SyntheticProperties>
    <SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/>
    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
  </SyntheticProperties>
  <Events>
    <EventHandler event="windowClosing" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="formWindowClosing"/>
  </Events>
  <AuxValues>
    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
  </AuxValues>

  <Layout>
    <DimensionLayout dim="0">
      <Group type="103" groupAlignment="0" attributes="0">
          <Component id="jPanel1" alignment="0" max="32767" attributes="0"/>
      </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
      <Group type="103" groupAlignment="0" attributes="0">
          <Component id="jPanel1" alignment="1" max="32767" attributes="0"/>
      </Group>
    </DimensionLayout>
  </Layout>
  <SubComponents>
    <Container class="javax.swing.JPanel" name="jPanel1">
      <Properties>
        <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
          <Color blue="99" green="99" red="99" type="rgb"/>
        </Property>
        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
          <Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
            <LineBorder roundedCorners="true" thickness="3"/>
          </Border>
        </Property>
      </Properties>
      <AuxValues>
        <AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
      </AuxValues>

      <Layout>
        <DimensionLayout dim="0">
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace min="-2" pref="24" max="-2" attributes="0"/>
                  <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Component id="jLabel2" min="-2" pref="32" max="-2" attributes="0"/>
                  <EmptySpace pref="303" max="32767" attributes="0"/>
                  <Component id="jLabel3" min="-2" pref="69" max="-2" attributes="0"/>
                  <EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
              </Group>
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace min="-2" pref="299" max="-2" attributes="0"/>
                  <Component id="jLabel4" min="-2" max="-2" attributes="0"/>
                  <EmptySpace max="-2" attributes="0"/>
                  <Component id="jLabel5" min="-2" max="-2" attributes="0"/>
                  <EmptySpace pref="201" max="32767" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
        <DimensionLayout dim="1">
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" attributes="0">
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Group type="102" attributes="0">
                          <EmptySpace min="-2" pref="21" max="-2" attributes="0"/>
                          <Group type="103" groupAlignment="3" attributes="0">
                              <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
                              <Component id="jLabel2" alignment="3" min="-2" pref="14" max="-2" attributes="0"/>
                              <Component id="jLabel3" alignment="3" min="-2" pref="36" max="-2" attributes="0"/>
                          </Group>
                          <EmptySpace min="-2" pref="55" max="-2" attributes="0"/>
                          <Component id="jLabel5" min="-2" max="-2" attributes="0"/>
                      </Group>
                      <Group type="102" alignment="0" attributes="0">
                          <EmptySpace min="-2" pref="98" max="-2" attributes="0"/>
                          <Component id="jLabel4" min="-2" pref="64" max="-2" attributes="0"/>
                      </Group>
                  </Group>
                  <EmptySpace pref="198" max="32767" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
      </Layout>
      <SubComponents>
        <Component class="javax.swing.JLabel" name="jLabel1">
          <Properties>
            <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
              <Font name="Aharoni" size="12" style="1"/>
            </Property>
            <Property name="text" type="java.lang.String" value="TOTAL HITS:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel2">
          <Properties>
            <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
              <Font name="Aharoni" size="18" style="1"/>
            </Property>
            <Property name="text" type="java.lang.String" value="    "/>
          </Properties>
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel3">
          <Properties>
            <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
              <Font name="Aharoni" size="24" style="1"/>
            </Property>
          </Properties>
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel4">
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel5">
          <Properties>
            <Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
              <Image iconType="3" name="/catchgame/ball.gif"/>
            </Property>
          </Properties>
          <Events>
            <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jLabel5MouseClicked"/>
          </Events>
        </Component>
      </SubComponents>
    </Container>
  </SubComponents>
</Form>



CatchBox.java
package catchgame;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;


class BGFlicker implements Runnable
{
    
      JPanel p;
    public BGFlicker(JPanel p) {
        this.p=p;        
    }

    public void run()
    {
         for(;;){
                          try {
                              p.setBackground(new Color(153,153,153));
                                 Thread.sleep(200);
                              p.setBackground(Color.red);
                              Thread.sleep(200);
                              p.setBackground(Color.BLACK);
                              Thread.sleep(200);
                         } catch (Exception e) {
                           //  JOptionPane.showMessageDialog(null, e);
                         }
                }
    }
}
public class CatchBox extends javax.swing.JFrame implements Runnable{
Thread t,red1;
int hits;
int seconds=20;
int delay=300;
int Cseconds=20;
BGFlicker o;
public  void calTimeThread()
{
    for(;seconds>=0;seconds--)
                     {
                           if(seconds<5 && !red1.isAlive())
                        {
                            try {
                                           red1.start();
                            } catch (Exception e) {

                            t.stop();
                            jLabel5.setEnabled(false);
                            red1.stop();

                            }

                        }
                         try {
                             Thread.sleep(1000);
                             jLabel3.setText(""+seconds);

                         } catch (Exception e) {
                            t.stop();
                            jLabel5.setEnabled(false);
                            red1.stop();

                         }


                     }
                       t.stop();
                       red1.stop();
                       jLabel5.setEnabled(false);
                       jPanel1.setBackground(new Color(153,153,153));
                       JOptionPane.showMessageDialog(null, "Total Hits ="+hits);
            }






Thread calTime=new Thread(new Runnable() {
    public void run() {
        calTimeThread();
            }
        });


void centerAlign()
{
    Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
    int x=(d.width-getWidth())/2;int y=(d.height-getHeight())/2;
    setLocation(x, y);
}


    /** Creates new form CatchBox */
    public CatchBox() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        initComponents();
        seconds=Cseconds;
        o=new BGFlicker(jPanel1);
        red1=new Thread(o);
        centerAlign();
        jLabel2.setText(""+hits);
        t=new Thread(this);
            
        calTime.start();
        t.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));

        jLabel1.setFont(new java.awt.Font("Aharoni", 1, 12));
        jLabel1.setText("TOTAL HITS:");

        jLabel2.setFont(new java.awt.Font("Aharoni", 1, 18));
        jLabel2.setText("    ");

        jLabel3.setFont(new java.awt.Font("Aharoni", 1, 24));

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/catchgame/ball.gif"))); // NOI18N
        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel5MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 303, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(299, 299, 299)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addContainerGap(201, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(55, 55, 55)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(198, Short.MAX_VALUE))
        );

        jMenu3.setText("File");

        jMenuItem11.setText("RESTART");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem11);

        jMenuItem13.setText("PAUSE");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem13);

        jMenuItem12.setText("EXIT");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem12);

        jMenuBar1.add(jMenu3);

        jMenu1.setText("Level");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, 0));
        jMenuItem1.setText("Level 1");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, 0));
        jMenuItem2.setText("Level 2");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, 0));
        jMenuItem3.setText("Level 3");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, 0));
        jMenuItem4.setText("Level 4");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, 0));
        jMenuItem5.setText("Level 5");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Time");

        jMenuItem6.setText("20 Seconds");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setText("30 Seconds");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);

        jMenuItem8.setText("40 Seconds");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem8);

        jMenuItem9.setText("1 Minutes");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem9);

        jMenuItem10.setText("2 Mintutes");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem10);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
          delay=500;seconds=Cseconds;
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
         delay=450;seconds=Cseconds;
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        delay=400;seconds=Cseconds;
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
       delay=300;seconds=Cseconds;
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
          delay=200;seconds=Cseconds;
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
           seconds=Cseconds=20;
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
          seconds=Cseconds=30;
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
         seconds=Cseconds=40;
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
         seconds=Cseconds=60;
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
  seconds=Cseconds=60*2;
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
         if(JOptionPane.showConfirmDialog(this, "ARE U WANT TO EXIT")==0)
         {
             System.exit(0);
         }

    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
          if(JOptionPane.showConfirmDialog(this, "ARE U WANT TO EXIT")==0)
         {
             System.exit(0);
         }
    }//GEN-LAST:event_formWindowClosing

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        seconds=Cseconds;hits=0;
        jLabel5.setEnabled(true);
        t=new Thread(this);
        red1=new Thread(o);
        jLabel2.setText("0");
         calTime=new Thread(new Runnable() {
    public void run() {
        calTimeThread();
            }
        });
        calTime.start();
        t.start();
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
         if(jMenuItem13.getText().equalsIgnoreCase("Pause"))
         {try {
            t.suspend();
            red1.suspend();
            calTime.suspend();
            jMenuItem13.setText("Resume");
            jLabel5.setEnabled(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error on Pause "+e);
        }}
         else if(jMenuItem13.getText().equalsIgnoreCase("Resume"))
         {try {            
            jLabel5.setEnabled(true);
              red1.resume();
            calTime.resume();
            t.resume();

            jMenuItem13.setText("Pause");
        } catch (Exception e) {
              JOptionPane.showMessageDialog(this, "Error on Start "+e);
        }}
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel5MouseClicked
           hits++;
        jLabel2.setText(""+hits);

    }//GEN-LAST:event_jLabel5MouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CatchBox().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    public javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

     public void run() {
         Random r=new Random();
         int i=0;
            for(;;)
            {
                 int x=r.nextInt(jPanel1.getWidth()-jLabel5.getWidth());
                 int y=r.nextInt(jPanel1.getHeight()-jLabel5.getHeight());
                 jLabel5.setLocation(x,y);
                 if(i==10)
                 {
                     i=0;
                     try {
                    t.sleep(delay*2);
                    i++;
                } catch (Exception e) {
                }
                 }
                 else{
                 try {
                    t.sleep(delay);
                    i++;
                } catch (Exception e) {
                }
                 }
            }
    }

}




Main.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package catchgame;

/**
 *
 * @author Dhiman
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new CatchBox().setVisible(true);
    }

}




Comments

Popular Posts