Showing posts with label timer. Show all posts
Showing posts with label timer. Show all posts

Java simple timer

Complete source code below will show you, how to create simple timer using java. Actually it has a problem. The problem that i mean is, it seem more slow than real timer.If you find how to solve it, i hope you can share with me by put a comment.

*******************************************************************
COMPLETE SOURCE CODE FOR : JavaSimpleTimer.java
*******************************************************************


import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class JavaSimpleTimer extends JPanel implements ActionListener
{
int miliseconds=0;
int seconds=0;
int minutes=0;

Timer myTimer;

Font timerFont=new Font("Verdana",Font.BOLD,24);

public JavaSimpleTimer()
{
myTimer=new Timer(10,this);

JFrame myFrame=new JFrame("Simple Timer");

myFrame.add(this);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(500,500);
myFrame.setVisible(true);

myTimer.setInitialDelay(0);
myTimer.start();
}

public void paint(Graphics g)
{
super.paint(g);
String mili=Integer.toString(miliseconds);
String sec=Integer.toString(seconds);
String min=Integer.toString(minutes);

if(mili.length()==1)
{
mili="0"+mili;
}
if(sec.length()==1)
{
sec="0"+sec;
}
if(min.length()==1)
{
min="0"+min;
}

g.setFont(timerFont);

g.drawString(min+" :",20,20);
g.drawString(sec+" :",80,20);
g.drawString(mili,140,20);
}

public void actionPerformed(ActionEvent event)
{
miliseconds=miliseconds+1;
if(miliseconds==100)
{
miliseconds=0;
seconds=seconds+1;
}
if(seconds==60)
{
seconds=0;
minutes=minutes+1;
}
repaint();
}

public static void main(String[]args)
{
JavaSimpleTimer myTest=new JavaSimpleTimer();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Simple Shutdown Timer For Windows XP

Source code below will show you how to create simple shutdown timer for Windows XP.

************************************************
COMPLETE SOURCE CODE FOR : ShutdownComputer.java
************************************************


import javax.swing.JOptionPane;

public class ShutdownComputer
{
public static void main(String[]args)
{
int wholeTimeBeforeShutdown=0;

int hour=0;
int minute=0;
int second=0;

try
{
String a=JOptionPane.showInputDialog(null,"HOUR","HOUR BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);
String b=JOptionPane.showInputDialog(null,"MINUTE","MINUTE BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);
String c=JOptionPane.showInputDialog(null,"SECOND","SECOND BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);

if((a==null)||(a.equals("")))
{
a="0";
}

if((b==null)||(b.equals("")))
{
b="0";
}

if((c==null)||(c.equals("")))
{
c="0";
}

int e=Integer.parseInt(a);
int f=Integer.parseInt(b);
int g=Integer.parseInt(c);

wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+((e*60)*60);
wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(f*60);
wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(g);

wholeTimeBeforeShutdown=wholeTimeBeforeShutdown*1000;

Thread.sleep(wholeTimeBeforeShutdown);

Runtime.getRuntime().exec("shutdown -s -t 00 -f");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


************************************************
JUST COMPILE AND EXECUTE IT
************************************************

RELAXING NATURE VIDEO