Friday, 18 November 2016

Java Programming source code for implementing a TIMER..

So guys, as always another source code for JAVA. This one is meant for implementing a timer which could be utilized to basically determine the point in a running program when an action is to be implemented.

This is coded in Netbeans IDE so the codes might look a little complicated. I this helps someone.

SOURCE CODE



package Timing_app;

import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author DAVID ORJI
 *
 *For those that do not know the slash
 * and asterisks used in this are
 *multiline comments and have no effects
 *in the program...
 */

/*
    Additional notes
    This is done with the netbeans IDE, so basically you just need to create
    a class and create a JFrame within the class with two JLabels namely:
   
    1. seconds
    2. minutes
*/
public class Timer_app extends javax.swing.JFrame {

 

    int secondsPassed;
    int minutesPassed;
   
       Timer timer  = new Timer();
        TimerTask task = new TimerTask(){
            @Override
            public void run(){
                secondsPassed++;
                seconds.setText(secondsPassed + " seconds passed:");
               
                if(secondsPassed % 60 == 0){
                    minutesPassed++;
                    if(minutesPassed == 1){
                         minutes.setText(minutesPassed + " minute passed ");
                       }
                    else{
                    minutes.setText(minutesPassed + " minutes passed ");
                    }
                       
                }
         }
       
        };
         
 
    public Timer_app() {
        initComponents();
        timer.scheduleAtFixedRate(task, 1000, 1000);
        setVisible(true);
       
        /*The above code specifies ; the Timertask to process,
        delay time/immediacy before the process begins ,
        and the difference in time between each increments*/
       
        //1000 of course equals 1 second.
                      }

     // The following below are Netbeans GUI generated code so pls ignore all in red
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        seconds = new javax.swing.JLabel();
        minutes = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(seconds, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minutes, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(seconds, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(minutes, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      


    public static void main(String args[]) {


            Timer_app run = new Timer_app();
           
    }

    // Variables declaration - do not modify                    
    private javax.swing.JLabel minutes;
    private javax.swing.JLabel seconds;
    // End of variables declaration                  
}

No comments:

Post a Comment