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
*******************************************************************

Set JSlider tick color

Complete source code below will show you, how to set JSlider tick color. I have tried to find method in JSlider class to set JSlider tick color...but, i can't find it. So i tried to create UI Delegate only for SliderUI that change JSlider tick color. You can try to compile both source code below. Make sure both java file below locate at the same location. JSlider's tick color is set in MyNewMetalSliderUI.java.You can try to change it's color follow what color that you want base on RGB value. After you compile SetJSliderTickColor.java and MyNewMetalSliderUI.java, you try execute SetJSliderTickColor.java. This is because, main method contain in this file.Oooo...before i forget, it's only handle horizontal slider. If you want to make it can use for vertical slider, you can try overwrite method paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) and paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) in MetalSliderUI class. You can review it in Java SE API for MetalSliderUI class.

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


import javax.swing.*;

import java.awt.*;

import javax.swing.plaf.ColorUIResource;

import javax.swing.plaf.metal.MetalSliderUI;

import javax.swing.plaf.ComponentUI;

public class MyNewMetalSliderUI extends MetalSliderUI
{
// Create our own slider UI
public static ComponentUI createUI(JComponent c )
{
return new MyNewMetalSliderUI();
}

//*******************HORIZONTAL MAJOR TICK*******************
public void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=255,GREEN=0,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(255,0,0);

//Set color that will use to draw MAJOR TICK using created color
g.setColor(majorTickColor);

//Draw MAJOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height);
}
}
//*******************HORIZONTAL MAJOR TICK*******************

//*******************HORIZONTAL MINOR TICK*******************
public void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=12,GREEN=255,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(12,255,0);

//Set color that will use to draw MINOR TICK using created color
g.setColor(majorTickColor);

//Draw MINOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height/2);
}
}
//*******************HORIZONTAL MINOR TICK*******************
}


**********************************************************************
JUST COMPILE
**********************************************************************

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

import javax.swing.*;

import java.awt.*;

public class SetJSliderTickColor
{
public SetJSliderTickColor()
{
//Create slider using JSlider
JSlider mySlider=new JSlider();

//Set major tick spacing to 10
mySlider.setMajorTickSpacing(10);

//Set minor tick spacing to 1
mySlider.setMinorTickSpacing(1);

//Make slider's numbers visible
mySlider.setPaintLabels(true);

//Make slider's ticks visible
mySlider.setPaintTicks(true);

//Create a window using JFrame with title ( Set JSlider tick color )
JFrame frame=new JFrame("Set JSlider tick color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add created JSlider into JFrame
frame.add(mySlider);

//Set JFrame's default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,300);

//Set JFrame locate at center of screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}

public static void main(String[]args)
{
/***********Set our own UI Delegate for Slider*************/
UIManager uim=new UIManager();

//MyNewMetalSliderUI contain settings that use to change tick color
uim.put("SliderUI","MyNewMetalSliderUI");
/***********Set our own UI Delegate for Slider*************/

SetJSliderTickColor sjstc=new SetJSliderTickColor();
}
}


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

Set JOptionPane warning icon

Complete source code below will show you, how to set warning icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneWarningIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Warning Icon-text in message box
//WARNING-JOptionPane title
//JOptionPane.WARNING_MESSAGE-Set warning icon that will be show in message box
JOptionPane.showMessageDialog(null,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE);
}
}


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

Set JOptionPane error icon

Complete source code below will show you, how to set error icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneErrorIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Error Icon-text in message box
//ERROR-JOptionPane title
//JOptionPane.ERROR_MESSAGE-Set error icon that will be show in message box
JOptionPane.showMessageDialog(null,"Error Icon","ERROR",JOptionPane.ERROR_MESSAGE);
}
}


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

Set JOptionPane information icon

Complete source code below will show you, how to set information icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneInformationIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Information Icon-text in message box
//INFORMATION-JOptionPane title
//JOptionPane.INFORMATION_MESSAGE-Set information icon that will be show in message box
JOptionPane.showMessageDialog(null,"Information Icon","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
}
}


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

Set JOptionPane question icon

Complete source code below will show you, how to set question icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneQuestionIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Question Icon-text in message box
//QUESTION-JOptionPane title
//JOptionPane.QUESTION_MESSAGE-Set question icon that will be show in message box
JOptionPane.showMessageDialog(null,"Question Icon","QUESTION",JOptionPane.QUESTION_MESSAGE);
}
}


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

Create simple digital clock in java

Complete source code below, will show you, how to create simple digital clock in java.

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


import javax.swing.*;

import java.awt.*;

import java.util.Date;

public class SimpleDigitalClock extends JPanel
{
JFrame frame;

int currentHour;
int currentMinute;
int currentSecond;

//Font that will be use to show digital clock
Font myFont=new Font("Tahoma",Font.BOLD+Font.ITALIC,20);

//Color that will be use to show digital clock
Color myColor=new Color(255,0,0);

//Font metrics that will use to store font informations
//For example, width of a character
FontMetrics fm;

public SimpleDigitalClock()
{
//Create a window using JFrame with title ( Simple Digital Clock )
frame=new JFrame("Simple Digital Clock");

//add(this) mean add created panel into JFrame
//Which panel ?
//See line 81 and 7 (I hope you understand it)
frame.add(this);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(300,300);

//Make JFrame locate at center
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);

/*
*Loop that will make sure panel show current time
*like current second, current minute and current hour.
*/
while(true)
{
//It will do all code in method paint(See line 67)
repaint();

try
{
Thread.sleep(900);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}

public void paint(Graphics g)
{
super.paint(g);

/*
*Create current Date object. It means it store information
*about current hour, minute and second.
*/
Date myDate=new Date();

currentHour=myDate.getHours();
currentMinute=myDate.getMinutes();
currentSecond=myDate.getSeconds();

//Set font that will use to draw digital number
g.setFont(myFont);

//Information about distance between number in digital clock
fm=g.getFontMetrics();
int hourXCoordinate=20;
int minuteXCoordinate=hourXCoordinate+(fm.getMaxAdvance()*2);
int secondXCoordinate=hourXCoordinate+(fm.getMaxAdvance()*4);

//Set color that will use to draw digital number
g.setColor(myColor);

//Draw hour, draw (:) between number, draw minute and draw second.
g.drawString(Integer.toString(currentHour),hourXCoordinate,20);
g.drawString(":",(hourXCoordinate+minuteXCoordinate)/2,20);
g.drawString(Integer.toString(currentMinute),minuteXCoordinate,20);
g.drawString(":",(secondXCoordinate+minuteXCoordinate)/2,20);
g.drawString(Integer.toString(currentSecond),secondXCoordinate,20);
}

public static void main(String[]args)
{
SimpleDigitalClock sdc=new SimpleDigitalClock();
}
}


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

Get current second

Complete source code below, will show you how to get current computer second and print the value. The second value that it get is second value at program executing time.

>>Get current minute
>>Get current hour
>>Get current computer time in java

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


import java.util.Date;

public class GetCurrentSecond
{
public static void main(String[]args)
{
Date myDate=new Date();

//Get current second
int currentSecond=myDate.getSeconds();

//Print current second that we get
System.out.println("Current second is : "+currentSecond);
}
}


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

Get current minute

Complete source code below will print current computer minute.

>>Get current second
>>Get current hour
>>Get current computer time in java

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


import java.util.Date;

public class GetCurrentMinute
{
public static void main(String[]args)
{
Date myDate=new Date();

//Get current minute
int currentMinute=myDate.getMinutes();

//Print current minute that we get
System.out.println("Current minute is : "+currentMinute);
}
}


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

Get current hour

Complete source code below will print current computer hour in soldier time format.Soldier time format is 24 hour format.

>>Get current minute
>>Get current second
>>Get current computer time in java

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


import java.util.Date;

public class GetCurrentHour
{
public static void main(String[]args)
{
Date myDate=new Date();

//Get current hour in soldier time format
int currentHour=myDate.getHours();

//Print current hour that we get
System.out.println("Current hour is : "+currentHour);
}
}


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

RELAXING NATURE VIDEO