/*
Author : RK
Aim : ANALOG CLOCK INSIDE A JFrame
*/
import java.awt.Toolkit;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
class AnalogClock extends JComponent implements Runnable
{
private static Stroke SEC_STROKE = new BasicStroke();
private static Stroke MIN_STROKE =new BasicStroke(4F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static Stroke HOUR_STROKE = MIN_STROKE;
private double sa = Math.PI / 2;
private double sda = Math.PI / 30;
private double mda = sda / 60;
private double hda = mda / 12;
int sx,sy,cenx,ceny;
String mday="";
int wday=0;
String swday="";
int month=0;
String year="";
String smonth="";
public AnalogClock()
{
(new Thread(this)).start();
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension dim=toolkit.getScreenSize();
JFrame f = new JFrame("Analog Clock");
f.setUndecorated(true);
f.setResizable(false);
f.getContentPane().add(this);
Color c=new Color(135,200,41);
f.getContentPane().setBackground(Color.BLACK);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
f.setVisible(true);
f.setBounds(dim.width-120, 4, 118, 200);
}
public void run()
{
try {
for(;;)
{
Thread.sleep(500);
repaint();
}
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
@Override
@SuppressWarnings("empty-statement")
public void paint(Graphics graphics)
{
Graphics2D g = (Graphics2D) graphics;
Calendar cal=Calendar.getInstance();
int s =cal.get(Calendar.SECOND) ;
int m =cal.get(Calendar.MINUTE) ;
int h =cal.get(Calendar.HOUR) ;
int ms = m * 60;
int hs = h * 60 * 60;
//Below line of code will draw a circle
Color c=new Color(18, 146, 235);
g.setColor(c);
g.setStroke(MIN_STROKE);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(3,2,111,111);
g.fillOval(3,2,111,111);
cenx=(116)/2;
ceny=(115)/2;
int sr=(110-7)/2;
//Below line of code will draw a second hand
g.setColor(Color.black);
int sxx = (int) ((Math.cos((s * sda) - sa) * sr) + cenx);
int syy = (int) ((Math.sin((s * sda) - sa) * sr) + ceny);
g.setStroke(SEC_STROKE);
g.drawLine(cenx, ceny, sxx, syy);
//Below line of code will draw a minute hand
int mr=sr-4;
c=new Color(255, 255, 255);
g.setColor(c);
int mx = (int) ((Math.cos(((ms + s) * mda) - sa) * mr) + cenx);
int my = (int) ((Math.sin(((ms + s) * mda) - sa) * mr) + ceny);
g.setStroke(MIN_STROKE);
g.drawLine(cenx, ceny, mx, my);
//Below line of code will draw a hour hand
int hr=sr-14;
g.setColor(Color.black);
int hx = (int) ((Math.cos(((hs + ms + s) * hda) - sa) * hr) + cenx);
int hy = (int) ((Math.sin(((hs + ms + s) * hda) - sa) * hr) + ceny);
g.setStroke(HOUR_STROKE);
g.drawLine(cenx, ceny, hx, hy);
//Below line of code will print the Date contents
mday=""+cal.get(Calendar.DAY_OF_MONTH);
wday=cal.get(Calendar.DAY_OF_WEEK);
month=cal.get(Calendar.MONTH);
year=""+cal.get(Calendar.YEAR);
month++;
if(wday==1)
{
swday="Sunday";
}else if(wday==2)
{
swday="Monday";
}else if(wday==3)
{
swday="Tuesday";
}else if(wday==4)
{
swday="Wednesday";
}else if(wday==5)
{
swday="Thursday";
}else if(wday==6)
{
swday="Friday";
}else if(wday==7)
{
swday="Saturday";
}
if(month==1)
{
smonth="Jan";
}else if(month==2)
{
smonth="Feb";
}else if(month==3)
{
smonth="March";
}else if(month==4)
{
smonth="April";
}else if(month==5)
{
smonth="May";
}else if(month==6)
{
smonth="June";
}else if(month==7)
{
smonth="July";
}else if(month==8)
{
smonth="Aug";
}else if(month==9)
{
smonth="Sep";
}else if(month==10)
{
smonth="Oct";
}else if(month==11)
{
smonth="Nov";
}else if(month==12)
{
smonth="Dec";
}
Font f=new Font("Verdana",Font.BOLD,15);
g.setFont(f);
g.setColor(Color.white);
int len_swday=swday.length();
int len_mday=mday.length();
int len_smonth=smonth.length();
int len_year=year.length();
int tot_char=10;
int ycord=140;
float xcord=(118/2)-((len_swday/2)*(118/tot_char));
g.drawString(swday,xcord,ycord);
xcord=(float)((118 / 2) - ((len_mday/ 2) * (118 / tot_char)));
ycord=ycord+15;
g.drawString(mday, xcord,ycord);
xcord=(118/2)-((len_smonth/2)*(118/tot_char));
ycord=ycord+15;
g.drawString(smonth,xcord,ycord);
xcord=(118/2)-((len_year/2)*(118/tot_char));
ycord=ycord+15;
g.drawString(year,xcord,ycord);
f=new Font("Verdana",Font.BOLD,9);
g.setFont(f);
String name="[RK]";
ycord=90;
xcord=(110/2)-((name.length()/3)*(110/tot_char));
g.drawString(name,xcord,ycord);
ycord=15;
}
public static void main(String rk[])
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
AnalogClock clock = new AnalogClock();
}
}
No comments:
Post a Comment