1.AnalogClock .java
2.AnalogClockView .java
// -->>>>> First AnalogClock .javaThen if you have 2 class.You can run AnalogClockView.java
package utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class AnalogClock extends JPanel {
ImageIcon img;
private GregorianCalendar m_calendar;
private int[] x = new int[2];
private int[] y = new int[2];
private java.util.Timer clocktimer = new java.util.Timer();
/**
* You could set the TimeZone for the clock here. I used the Dfault time
* zone from the user so that every time the program runs on different
* computers the correct time is displayed
*/
private TimeZone clockTimeZone = TimeZone.getDefault();
// Constructor
public AnalogClock() {
this.setPreferredSize(new Dimension(210, 210));
this.setMinimumSize(new Dimension(210, 210));
// schedules the clocktimer task to scan for every 1000ms=1sec
clocktimer.schedule(new TickTimerTask(), 0, 1000);
}
// The Clock Face instance method
public void paint(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
drawCardinals((Graphics2D) g);
drawHands((Graphics2D) g);
}
// Endpoints of the Clock Hand
void clockMinutes(int startRadius, int endRadius, double theta) {
theta -= Math.PI / 2;
x[0] = (int) (getWidth() / 2 + startRadius * Math.cos(theta));
y[0] = (int) (getHeight() / 2 + startRadius * Math.sin(theta));
x[1] = (int) (getWidth() / 2 + endRadius * Math.cos(theta));
y[1] = (int) (getHeight() / 2 + endRadius * Math.sin(theta));
}
// The Hours/Cardinals of the clock
/** Set Stroke sets the thickness of the cardinals and hands */
void drawCardinals(Graphics2D g) {
g.setStroke(new BasicStroke(9));
g.setColor(Color.black);
for (double theta = 0; theta < h =" 2" m =" 2" s =" 2" m_calendar =" (GregorianCalendar)">>>>>. Second AnalogClockView .java
package utils;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class AnalogClockView extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("My Java Project Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Clock.gif"));
AnalogClock m_AnalogClock = new AnalogClock();
frame.add(m_AnalogClock);
frame.setVisible(true);
}
}
Output as below...
Analog Clock
Tag: study code program java
nice idie to make an analog clock, it will be a usefull program...
ReplyDeletethanks for sharing..
tyang @ codeprogram.co.cc