Showing posts with label simple. Show all posts
Showing posts with label simple. Show all posts

Java reverse String and simplest palindrome checker

Today, i have learn something new and it's totally simple. Sometime when don't know about that, we will use our way that take a time to complete a simple task. What i learn today is, how to reverse String. The easiest way to reverse a String for example from "i love you" to "uoy evol i" is using method reverse() in StringBuffer class. It is also very useful especially when you want to create a palindrome checker program. Your program will be the shortest palindrome checker program in the world i think, hehehehe. I have also created a new one before, but it quit long. You can review it by click here. Now we will go to coding part. I will create two simple programs. Firstly to reverse "i love you" to "uoy evol i". Secondly i will create my shortest palindrome checker program that i ever made. Don't waste our time by hear what i said.

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


public class ReverseString
{
public static void main(String[]args)
{
String a="i love you";
StringBuffer b=new StringBuffer(a);

System.out.println(b.reverse());
}
}


*************************************************************
LIKE USUAL, JUST COMPILE AND EXECUTE IT
*************************************************************

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


import javax.swing.JOptionPane;

public class MySimplestPalindromeChecker
{
public static void main(String[]args)
{
String a=JOptionPane.showInputDialog(null,"What word that you want to check ?","Palindrome Checker",JOptionPane.QUESTION_MESSAGE);

if(a!=null)
{
String b=a;
StringBuffer c=new StringBuffer(b);
if(a.equals(c.reverse().toString()))
{
JOptionPane.showMessageDialog(null,"THIS IS A PALINDROME WORD");
}
else
{
JOptionPane.showMessageDialog(null,"THIS IS NOT A PALINDROME WORD");
}
}
}
}


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

Java simple rgb to hex converter

Complete source code below is a small java program that convert rgb value to hex value. Now i will share with you how to get hex value from rgb value. Before i go far, let we see a description about hex value structure.


VALUE THAT YOU GET AFTER DIVIDE OR MULTIPLY

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
HEX VALUE0123456789ABCDEF

TABLE A

For example :
FFFFFF is a hex value for white and it's rgb value is 255 255 255.First FF is value for red, second FF is value for green and last FF is for blue.So it mean, first 255 in rgb is equal to first FF in hex value.Now i will show you, how to get FF from 255.For this tutorial i show you only for red part. You can try other last two part for green and blue. Now take 255 and devide it with 16. You should get 15.9375. Take value before floating point. In this case we get 15. Look at TABLE A at above. 15 is equal to F. Now you get first F.For second F, take value after floating point and multiply it with 16. In this case you should multiply 0.9375 with 16. It's result is equal to 15 and it also hold F.Now you get hex value for red part.I hope you can understand what i tried to say.

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


import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;
import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RGBtoHEX implements ActionListener
{
JTextField r=new JTextField(10);
JTextField g=new JTextField(10);
JTextField b=new JTextField(10);

JLabel rLabel=new JLabel("R",JLabel.CENTER);
JLabel gLabel=new JLabel("G",JLabel.CENTER);
JLabel bLabel=new JLabel("B",JLabel.CENTER);

JPanel panelTextField=new JPanel();
JPanel panelLabel=new JPanel();

JButton button=new JButton("Calculate Now !");

JTextField result=new JTextField(30);

public RGBtoHEX()
{
button.addActionListener(this);

r.setHorizontalAlignment(JTextField.CENTER);
g.setHorizontalAlignment(JTextField.CENTER);
b.setHorizontalAlignment(JTextField.CENTER);

result.setHorizontalAlignment(JTextField.CENTER);
result.setBackground(new Color(255,255,255));
result.setEditable(false);

panelLabel.setLayout(new GridLayout(1,3));
panelLabel.add(rLabel);
panelLabel.add(gLabel);
panelLabel.add(bLabel);

panelTextField.setLayout(new GridLayout(1,3));
panelTextField.add(r);
panelTextField.add(g);
panelTextField.add(b);

JFrame myMainWindow=new JFrame("RGB to HEX");
myMainWindow.getContentPane().setLayout(new GridLayout(4,1));
myMainWindow.getContentPane().add(panelLabel);
myMainWindow.getContentPane().add(panelTextField);
myMainWindow.getContentPane().add(button);
myMainWindow.getContentPane().add(result);

myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setSize(300,130);
myMainWindow.setLocationRelativeTo(null);
myMainWindow.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
try
{
String hexValue="#";
r.setEditable(false);
g.setEditable(false);
b.setEditable(false);

double red=Double.parseDouble(r.getText());
double green=Double.parseDouble(g.getText());
double blue=Double.parseDouble(b.getText());

for(int i=1;i<=3;i++)
{
double temp=0;

if(i==1)
{
temp=red;
}
else if(i==2)
{
temp=green;
}
else if(i==3)
{
temp=blue;
}

double devideResult=temp/16;

String stringDevideResult=Double.toString(devideResult);

int pointIndexInString=stringDevideResult.indexOf(".");

String firstValue=stringDevideResult.substring(0,pointIndexInString);

double multiplySixteen=(devideResult-(Double.parseDouble(firstValue)))*16;

String stringMultiplySixteen=Double.toString(multiplySixteen);

pointIndexInString=stringMultiplySixteen.indexOf(".");

String secondValue=stringMultiplySixteen.substring(0,pointIndexInString);

if(firstValue.equalsIgnoreCase("10"))
{
firstValue="A";
}
if(firstValue.equalsIgnoreCase("11"))
{
firstValue="B";
}
if(firstValue.equalsIgnoreCase("12"))
{
firstValue="C";
}
if(firstValue.equalsIgnoreCase("13"))
{
firstValue="D";
}
if(firstValue.equalsIgnoreCase("14"))
{
firstValue="E";
}
if(firstValue.equalsIgnoreCase("15"))
{
firstValue="F";
}
if(secondValue.equalsIgnoreCase("10"))
{
secondValue="A";
}
if(secondValue.equalsIgnoreCase("11"))
{
secondValue="B";
}
if(secondValue.equalsIgnoreCase("12"))
{
secondValue="C";
}
if(secondValue.equalsIgnoreCase("13"))
{
secondValue="D";
}
if(secondValue.equalsIgnoreCase("14"))
{
secondValue="E";
}
if(secondValue.equalsIgnoreCase("15"))
{
secondValue="F";
}

hexValue=hexValue+firstValue+secondValue;
}


result.setText(hexValue);
r.setEditable(true);
g.setEditable(true);
b.setEditable(true);
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"Program error and it will terminate","ERROR",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}

public static void main(String[]args)
{
RGBtoHEX converter=new RGBtoHEX();
}
}


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

Java simple image magnifier tooltip


Sometime when we play with images...i mean here a lot of images, like we let user choose an image from 500 images in a JFrame and all images show at the same time without any scrollbar, it can give user a little problem if the image too small. So, how can user make a good choice. This give me some idea to built my own image tooltip. This is a simple tooltip. It will enlarge image when someone hover mouse cursor on it.Image below is a result from program below.


Before you compile and execute it, you should download all images below. After that place them at same location with this java source file.

Love_Music_by_jovincent.jpg

Leafs_1_by_NerghaL.jpg

Hancock-1611.jpg

matrix.jpg

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


import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import java.awt.GridLayout;
import java.awt.Graphics;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class ImageToolTip
{
public static void main(String[]args)
{
PanelImage imageA=new PanelImage("matrix.jpg");

PanelImage imageB=new PanelImage("Hancock-1611.jpg");

PanelImage imageC=new PanelImage("Leafs_1_by_NerghaL.jpg");

PanelImage imageD=new PanelImage("Love_Music_by_jovincent.jpg");

JFrame myMainWindow=new JFrame("Image Tool Tip");
myMainWindow.setResizable(false);
myMainWindow.getContentPane().setLayout(new GridLayout(2,2));
myMainWindow.getContentPane().add(imageA);
myMainWindow.getContentPane().add(imageB);
myMainWindow.getContentPane().add(imageC);
myMainWindow.getContentPane().add(imageD);

myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setSize(100,100);
myMainWindow.setLocationRelativeTo(null);
myMainWindow.setVisible(true);
}
}

class PanelImage extends JPanel implements MouseListener
{
ImageIcon temp;
ImageMagnifier im;

public PanelImage(String a)
{
addMouseListener(this);
temp=new ImageIcon(a);
}

public void paint(Graphics g)
{
super.paint(g);
g.drawImage(temp.getImage(),0,0,getSize().width,getSize().height,this);
}

public void mouseClicked(MouseEvent event)
{
}

public void mouseEntered(MouseEvent event)
{
im=new ImageMagnifier(temp,getSize().width,getSize().height,event.getXOnScreen(),event.getYOnScreen());
}

public void mouseExited(MouseEvent event)
{
im.dispose();
}

public void mousePressed(MouseEvent event)
{
}

public void mouseReleased(MouseEvent event)
{
}
}

class ImageMagnifier extends JFrame
{
ImageIcon temp;

public ImageMagnifier(ImageIcon imageFile,int width,int height,int x,int y)
{
setUndecorated(true);
temp=imageFile;
setLocation(x,y);
setSize(width*9,height*9);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.drawImage(temp.getImage(),0,0,getSize().width,getSize().height,this);
}
}


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

Java simple calendar


Complete source code below will show you, how to create simple calendar in java. This program will prompts the user to enter the year and first day of the year, and display the calendar table for the year on the console.

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


import java.util.Calendar;
import java.util.Scanner;

public class JavaSimpleCalendar
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);

System.out.print("YEAR : ");
int year=s.nextInt();

System.out.println();

System.out.println("FIRST DAY OF THE YEAR");
System.out.println("1 for MONDAY");
System.out.println("2 for TUESDAY");
System.out.println("3 for WEDNESDAY");
System.out.println("4 for THURSDAY");
System.out.println("5 for FRIDAY");
System.out.println("6 for SATURDAY");
System.out.println("7 for SUNDAY");
System.out.print("FIRST DAY : ");
int firstDay=s.nextInt();

System.out.println();

boolean leapYear=false;
if(year%4==0)
{
leapYear=true;
}

for(int i=1;i<=12;i++)
{
System.out.println("***********************");
System.out.println("MONTH : "+i);
System.out.println("***********************");

if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=31;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}

}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
else if(i==2)
{
if(leapYear==true)
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=29;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}

}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
else
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=28;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}

}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
}
else
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=30;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}

}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
}
}
}


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

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

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

Java simple counter

Source below will show you, how to create simple counter in java.

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


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Font;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JavaSimpleCounter extends JLabel implements ActionListener
{
JButton buttonReset=new JButton("RESET");
JButton buttonCount=new JButton("CLICK HERE");
int countValue=0;
JFrame frame;
Font fontForNumber=new Font("Tahoma",Font.BOLD,80);

public JavaSimpleCounter()
{
frame=new JFrame("JAVA SIMPLE COUNTER");
frame.setLayout(new BorderLayout());
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,2));
buttonCount.addActionListener(this);
buttonReset.addActionListener(this);
panel.add(buttonCount);
panel.add(buttonReset);
frame.add(this,BorderLayout.CENTER);
frame.add(panel,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
frame.setVisible(true);
}

public void paint(Graphics g)
{
g.setFont(fontForNumber);
g.setColor(Color.BLACK);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(countValue),10,80);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==buttonCount)
{
countValue++;
frame.repaint();
}

else if(event.getSource()==buttonReset)
{
Graphics panelGraphics=getGraphics();
panelGraphics.setFont(fontForNumber);
panelGraphics.setColor(Color.BLACK);
panelGraphics.fillRect(0,0,getSize().width,getSize().height);
panelGraphics.setColor(Color.RED);
panelGraphics.drawString(Integer.toString(0),10,80);
countValue=0;
frame.repaint();
}
}

public static void main(String[]args)
{
JavaSimpleCounter jsc=new JavaSimpleCounter();
}
}


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