User:Kushwahar

== CountDown Timer == Create a CountDown Timer for game in Swing, using with the help of Java JFrame & JPanel. This program to helps you for displaying counter in top right side of the window frame. This is useful for timer based animation, computer adapting tests etc.

import java.awt.*;

import javax.swing.*;

import javax.swing.Timer;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyListener;

import java.awt.event.KeyEvent;

import java.awt.geom.Rectangle2D;

import java.text.NumberFormat;

/**
 * @author rajesh kumar
 * @conatct: tutorialdb@gmail.com
 * @conatct: tutorialdb@gmail.com

The following examples using for showing how the countdown timer/watch work in java with the help of JFrame and JPanel.

public class countDownTimer {	   public static void main(String[] args) {       new timerFrame; } }

class timerFrame extends JFrame {   public JFrame masterFrame;    //ORCog_Panel panel; timerPanel panel; public timerFrame {       try {             masterFrame = new JFrame("Welcome"); masterFrame.setBackground(Color.BLACK); panel = new timerPanel(this); masterFrame.getContentPane.add(panel); //add First Panel masterFrame.setUndecorated(true); masterFrame.setResizable(false); masterFrame.setSize(800,600); masterFrame.setVisible(true); masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }//end of try catch(Exception e)       { System.out.println(e.getMessage); }   }//end of constructor }//end of JFrame class

class timerPanel extends JPanel {     timerFrame mFrame; Timer timer; int iDelayTimer,totalTime; JLabel label; Font boldFont = new Font("Arial",Font.BOLD,22); Rectangle2D userRespB,header,footer; long remaining = 0; // How many milliseconds remain in the countdown. long lastUpdate = 0; // When count was last updated NumberFormat format; // Format minutes:seconds with leading zeros int minutes ; int seconds ; long now ; // current time in ms     long elapsed ; // ms elapsed since last update public timerPanel(timerFrame frame) {      Font boldFont;  //set label font boldFont = new Font("Arial",Font.BOLD,25); setBackground(Color.white); mFrame = frame; mFrame.masterFrame.addKeyListener(listenerKB); remaining = 5 * 60 * 1000; //5min format = NumberFormat.getNumberInstance; format.setMinimumIntegerDigits(2); // pad with 0 if necessary /*create header, footer & userResp box*/ header = new Rectangle2D.Double(0, 0, 800, 30); footer = new Rectangle2D.Double(0, 570, 800, 30); /*end of declaration*/ /*set timer with values*/ iDelayTimer = 1000; timer = new Timer(iDelayTimer, timerEventTest); timer.setInitialDelay(0); /*using for user response*/ label = new JLabel; label.setBackground (Color.white); label.setForeground (Color.red); label.setFont(boldFont); label.setHorizontalAlignment(JLabel.CENTER); label.setVerticalAlignment(JLabel.CENTER); label.setVerticalTextPosition(JLabel.CENTER); label.setHorizontalTextPosition(JLabel.CENTER); label.setBounds(200,-5,100,40); mFrame.masterFrame.add(label); }//end of constructor public void showPathImg(Graphics2D g)      { //other configuration g.setColor(Color.getHSBColor(165,240,150)); g.fill(header); g.setFont(boldFont); //new Font("Arial", Font.BOLD, 25) g.setFont(new Font("Arial", Font.BOLD, 25)); g.setColor(Color.BLUE); g.drawString("Remaining Time: ",10,25); minutes = (int)(remaining/60000); seconds = (int)((remaining%60000)/1000); g.drawString((format.format(minutes)+":"+""), 200, 25); if(minutes < 3) g.drawString((format.format(minutes)+" : "+format.format(seconds)), 200, 25); g.setColor(Color.getHSBColor(165,240,150)); g.fill(footer); }//end of method @Override protected void paintComponent(Graphics g)    { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); showPathImg(g2); }//end of paint method KeyListener listenerKB = new KeyListener {        public void keyPressed(KeyEvent e)         { int code = e.getKeyCode; switch(code) {                 case KeyEvent.VK_SPACE: lastUpdate = System.currentTimeMillis; timer.start; // Start the timer //System.out.println("Space key pressed: "+code); break; }//end of switch

}//end of function public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} }; //end of keylistener ActionListener timerEventTest = new ActionListener  { public void actionPerformed(ActionEvent evt) {       elapsed = now - lastUpdate; // ms elapsed since last update remaining -= elapsed; // adjust remaining time lastUpdate = now; // remember this update time if (remaining == 0) {             timer.stop; System.exit(0); }       else {             // current time in ms              now = System.currentTimeMillis; repaint; }       if (remaining < 0) remaining = 0; }//end of action performed function };//end of timer listener }//end of timerPanel