|
Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++
Interview Questions
XML
Interview Questions
EJB
Interview Questions
JSP
Interview Questions
.........More
Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Java Source Codes
timer for various time zones
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TimerTest
{ public static void main(String[] args)
{ JFrame f = new TimerTestFrame();
f.show();
}
}
class TimerTestFrame extends JFrame
{ public TimerTestFrame()
{ setSize(450, 300);
setTitle("TimerTest");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container c = getContentPane();
c.setLayout(new GridLayout(2, 3));
c.add(new ClockCanvas("San Jose", "GMT-8"));
c.add(new ClockCanvas("Taipei", "GMT+8"));
c.add(new ClockCanvas("Berlin", "GMT+1"));
c.add(new ClockCanvas("New York", "GMT-5"));
c.add(new ClockCanvas("Cairo", "GMT+2"));
c.add(new ClockCanvas("Bombay", "GMT+5"));
}
}
interface TimerListener
{ void timeElapsed(Timer t);
}
class Timer extends Thread
{ public Timer(int i, TimerListener t)
{ target = t;
interval = i;
setDaemon(true);
}
public void run()
{ try
{ while (!interrupted())
{ sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
private TimerListener target;
private int interval;
}
class ClockCanvas extends JPanel
implements TimerListener
{ public ClockCanvas(String c, String tz)
{ city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(125, 125);
}
public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawOval(0, 0, 100, 100);
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;
g.drawLine(50, 50, 50 + (int)(30
* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(50, 50, 50 + (int)(40
* Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(50, 50, 50 + (int)(45
* Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));
g.drawString(city, 0, 115);
}
public void timeElapsed(Timer t)
{ calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();
}
private int seconds = 0;
private String city;
private int offset;
private GregorianCalendar calendar;
private final int LOCAL = 16;
}
<<<----- Return to
Java Source
Code Questions Page.
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
|