# Thread: Analog Clock Problem

1. Member
Join Date
Feb 2012
Posts
2
Rep Power
0

## Analog Clock Problem

Can anyone explain to me why this clocks hands never move? Whenever the applet is launched the hands point to the right positions, but never move. Why, and how am I to fix it?
Thank you for you time.
Java Code:
```import java.awt.*;
import java.text.*;
import java.util.*;
import java.applet.*;

public class clock2 extends Applet implements Runnable{

private static final long serialVersionUID = 1787899701025425670L;
Thread t = null;
boolean threadSuspended;
String timeString = "";
int width, height;

Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
int hrHand, mnHand, secHand, radius = 95, hrLength, mnLength,
secLength, inLength, cx, cy, x1, y1, x2, y2;

public void init()
{
super.init();
width = getSize().width;
height = getSize().height;
resize(width, height);

hrLength = 6*radius/10;
mnLength = 8*radius/10;
secLength = 9*radius/10;
inLength = 8*radius/10;
cx = width/2;
cy = height/2;
}

public void start()
{
if(t == null)
{
t = new Thread(this);
t.setPriority(Thread.MIN_PRIORITY);
threadSuspended = false;
t.start();
}
else
{
if (threadSuspended)
{
threadSuspended = false;
synchronized(this)
{
notify();
}
}
}
}

public void stop()
{
threadSuspended = true;
}

public void run()
{
try {

while (true)
{
Calendar cal = Calendar.getInstance();
hrHand = cal.get(Calendar.HOUR_OF_DAY);
if (hrHand > 12) hrHand -= 12;
mnHand = cal.get(Calendar.MINUTE);
secHand = cal.get(Calendar.SECOND);

SimpleDateFormat formatter
= new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
Date date = cal.getTime();
timeString = formatter.format(date);

if (threadSuspended)
{
synchronized(this)
{
while (threadSuspended)
{
wait();
}
}
}
repaint();
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
}

public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;

calendar.setTime(trialTime);
hrHand = calendar.get(Calendar.HOUR_OF_DAY);
mnHand = calendar.get(Calendar.MINUTE);
secHand = calendar.get(Calendar.SECOND);

if(hrHand >= 12)
hrHand -= 12;

for (int i = 0; i < 60; i++)
{
g.setColor(Color.black);
x2 = (int)(cx + radius * Math.sin(i * 2 * Math.PI / 60));
y2 = (int)(cy - radius * Math.cos(i * 2 * Math.PI / 60));
if (i % 6 != 0)
{
x1 = (int)(cx + 0.9f * radius * Math.sin(i * 2 * Math.PI / 60));
y1 = (int)(cy - 0.9f * radius * Math.cos(i * 2 * Math.PI / 60));
}
else
{
x1 = (int)(cx + 0.8f * radius * Math.sin(i * 2 * Math.PI / 60));
y1 = (int)(cy - 0.8f * radius * Math.cos(i * 2 * Math.PI / 60));
}
g.drawLine(x1, y1, x2, y2);
}

g2d.setFont(getFont());
for(int hour = 1; hour <= 12; hour++)
{
double angle = (hour-3)*2*Math.PI/12;
int x = (int)(Math.cos(angle)*(width/3))+width/2-5;
int y = (int)(Math.sin(angle)*(width/3))+width/2+5;
g.setColor(Color.black);
g2d.drawString(""+hour, x, y);
}

x2 = (int)(cx + hrLength * Math.sin((hrHand + mnHand / 60 + secHand / 3600)
* 2 * Math.PI / 12));
y2 = (int)(cy - hrLength * Math.cos((hrHand + mnHand / 60 + secHand / 3600)
* 2 * Math.PI / 12));
g.setColor(Color.red);
g2.setStroke(new BasicStroke(2));
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
g2.setStroke(new BasicStroke(0));

g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
x2 = (int)(cx + mnLength * Math.sin((mnHand + secHand / 60) * 2 * Math.PI / 60));
y2 = (int)(cy - mnLength * Math.cos((mnHand + secHand / 60) * 2 * Math.PI / 60));
g.setColor(Color.green);
g2.setStroke(new BasicStroke(2));
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
g2.setStroke(new BasicStroke(0));

x2 = (int)(cx + secLength * Math.sin(secHand * 2 * Math.PI / 60));
y2 = (int)(cy - secLength * Math.cos(secHand * 2 * Math.PI / 60));
g.setColor(Color.blue);
g2.setStroke(new BasicStroke(0));
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);

g.setColor(Color.black);
g2.setStroke(new BasicStroke(2));
g.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2, 2 * radius,
2 * radius);
g2.setStroke(new BasicStroke(0));

g.setColor(Color.black);
g.drawString(timeString, 75, height-105);
}
}```

2. Member
Join Date
Feb 2012
Posts
2
Rep Power
0

## Re: Analog Clock Problem

My bad everyone I found the problem in paint this needs to come out
calendar.setTime(trialTime);
hrHand = calendar.get(Calendar.HOUR_OF_DAY);
mnHand = calendar.get(Calendar.MINUTE);
secHand = calendar.get(Calendar.SECOND);
Now the hands move, but the refresh is out of synch how do you get the clock from catching at spots then skip a couple of ticks at moments of the program?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•