I am trying to get this clock to tick twice as fast when the "2x Speed" button is clicked. For some reason the clock will jump to another location, when the button is clicked, as if the clock were entirely multiplied by 2, and then the second hand will tick twice as fast. I want to know if anyone can explain why this is so, and how to fix this? I have asked others and they have said that because the button affects the clock as it does is that x2 speed calculation just takes the seconds and doubles them so the second hand is always going to jump if the current seconds value is anything other than a very small number.

Keang, I believe, said I need to keep a note of the time the x2 was turned on and double the difference between the current time and the start time.

But even though the button is pushed it will jump first to another location and then start ticking twice as fast. What am I supposed to do?

The seconds are affected I believe in the paint() function, and the seconds are actually multiplied by 2 in the run()function.

Thanks for your help.

Code:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.applet.*;

public class AnalogClock extends Applet implements ActionListener, Runnable{
	
	private static final long serialVersionUID = 1787899701025425670L;
	Thread t = null;
	boolean threadSuspended;
	String timeString = "";
	
	
	Calendar cal = Calendar.getInstance();
	String regTimeFormat = "hh:mm:ss a";
	String milTimeFormat = "HH:mm:ss a";
	SimpleDateFormat formatter = null;
	
	int width, height, hrHand, mnHand, secHand, radius = 85, hrLength, mnLength,
		secLength, cx, cy, x1, y1, x2, y2;
	
	private Button buttonMilt;
	private Button b2xSpeed;
	private boolean buttonMiltClicked = false;
	private boolean b2xSpeedClicked = false;
	
	public void init()
	{
		buttonMilt = new Button("Military Time");
		add(buttonMilt);
		buttonMilt.addActionListener(this);
		
		b2xSpeed = new Button("2x Speed");
		add(b2xSpeed);
		b2xSpeed.addActionListener(this);

		
		width = getSize().width;	//get width of applet
		height = getSize().height;	//get height of applet
		
		hrLength = 6*radius/10;		//sets hours
		mnLength = 8*radius/10; 	//sets minutes
		secLength = 9*radius/10; 	//sets seconds
		cx = width/2; 				//initialize position cx
		cy = height/2;				//initialize position cy
		
		Calendar cal = Calendar.getInstance();
		formatter = new SimpleDateFormat(regTimeFormat);
		timeString = formatter.format(cal.getTime());
		this.setSize(400, 200);
		
		
		 
	}
		
	public void start() //initialize clock
	{
		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() //stop clock
	{
		threadSuspended = true;
	}
	
	public void run() //run clock
	{
		try {
			
			while(true) 
			{
				Calendar cal = Calendar.getInstance();						   //*gets time from the system
					hrHand = cal.get(Calendar.HOUR_OF_DAY);					   //*gets hour
				if (hrHand > 12) hrHand -= 12;								   //*if hour exceeds 12 decrease
					mnHand = cal.get(Calendar.MINUTE);						   //*get minutes
					//secHand = cal.get(Calendar.SECOND);						   //*get seconds	
				
					if (b2xSpeedClicked)
	        		{
	        			secHand =  (2*cal.get(Calendar.SECOND));					//*get seconds	
	        		}
	        		else
	        		{
	        			secHand = cal.get(Calendar.SECOND);						   //*get seconds	
	        		}
	        		repaint();
					
				if (threadSuspended) 
				{
					synchronized(this) 
					{
						while (threadSuspended) 
						{
							wait();
						}
					}
				}
				repaint();
				Thread.sleep(1000);
			}
		}
		catch (InterruptedException e) 
		{
		}
	}
	
	
	public void paint(Graphics g) //paint clock
	{
		Graphics2D g2d = (Graphics2D) g; //graphics g2d
		Graphics2D g2 = (Graphics2D) g; //graphics g2
		
		buttonMilt.setLocation(200,25);
		b2xSpeed.setLocation(200,50);
		
		if(hrHand >= 12) //decrease if hour exceeds 12
			hrHand -= 12;
		
		for (int i = 0; i < 60; i++) //tickmarks for loop
		{
			g.setColor(Color.black); //color of tickmarks
			x2 = (int)(cx + radius * Math.sin(i * 2 * Math.PI / 60)); //tickmarks endpoint1
			y2 = (int)(cy - radius * Math.cos(i * 2 * Math.PI / 60)); //tickmarks endpoint2
			if (i % 5 != 0)
			{
				x1 = (int)(cx + 0.9f * radius * Math.sin(i * 2 * Math.PI / 60)); //length bigticks endpoint1
				y1 = (int)(cy - 0.9f * radius * Math.cos(i * 2 * Math.PI / 60)); //length bigticks endpoint2
			}
			else
			{
				x1 = (int)(cx + 0.8f * radius * Math.sin(i * 2 * Math.PI / 60)); //length littleticks endpoint1
				y1 = (int)(cy - 0.8f * radius * Math.cos(i * 2 * Math.PI / 60)); //length littleticks endpoint2
			}
			g.drawLine(x1, y1, x2, y2); //tickmarks
		}
		
		g2d.setFont(getFont()); //sets font of numbers
		for(int hour = 1; hour <= 12; hour++) //Analog numbers for loop
		{
			double angle = (hour-3)*2*Math.PI/12; //angle of numbers
			int x = (int)(Math.cos(angle)*(width/3))+width/2-5; //endpoint1 of numbers
			int y = (int)(Math.sin(angle)*(width/3))+width/2+5; //endpoint2 of numbers
			g.setColor(Color.black); //color of analog numbers
			g2d.drawString(""+hour, x, y); //Analog numbers
		}
		
		x2 = (int)(cx + hrLength * Math.sin((hrHand + mnHand / 60 + secHand / 3600)
				* 2 * Math.PI / 12)); //endpoint1 of hour hand
		y2 = (int)(cy - hrLength * Math.cos((hrHand + mnHand / 60 + secHand / 3600)
				* 2 * Math.PI / 12)); //endpoint2 of hour hand
		g.setColor(Color.red); //color of hour hand
		g2.setStroke(new BasicStroke(2)); //width of hour hand
		g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //hour hand
		g2.setStroke(new BasicStroke(0)); //reset width
		
		x2 = (int)(cx + mnLength * Math.sin((mnHand + secHand / 60) * 2 * Math.PI / 60)); //endpoint1 of minute hand
		y2 = (int)(cy - mnLength * Math.cos((mnHand + secHand / 60) * 2 * Math.PI / 60)); //endpoint2 of minute hand
		g.setColor(Color.green); //color of minute hand
		g2.setStroke(new BasicStroke(2)); //width of minute hand
		g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //minute hand
		g2.setStroke(new BasicStroke(0)); // reset width of minute hand
		
		x2 = (int)(cx + secLength * Math.sin(secHand * 2 * Math.PI / 60)); //endpoint1 of second hand
		y2 = (int)(cy - secLength * Math.cos(secHand * 2 * Math.PI / 60)); //endpoint2 of second hand
		g.setColor(Color.blue); //color of circle
		g2.setStroke(new BasicStroke(0)); //width of circle
		g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //second hand
		
		g.setColor(Color.black); //color of circle
		g2.setStroke(new BasicStroke(2)); //width of circle
		g.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2, 2 * radius,
				2 * radius); //circle
		g2.setStroke(new BasicStroke(0)); //reset width
		
		g.setColor(Color.black); //color of timer
        Calendar cal = Calendar.getInstance();
        timeString = formatter.format(cal.getTime());

		g.drawString(timeString, 75, height-105); //timer
		
	}
	
	public void actionPerformed(ActionEvent e) {

		        String formatStr = null;
		        
		
		        if (e.getSource() == buttonMilt) {
		            buttonMiltClicked = !buttonMiltClicked;
		            if(buttonMiltClicked)
		            {
		                formatStr = milTimeFormat;
		            }
		            else 
		            {
		                formatStr = regTimeFormat;
		            }  
		            formatter = new SimpleDateFormat(formatStr);
		            repaint();
		            }
		        
	

		        if (e.getSource()==b2xSpeed){
		        	b2xSpeedClicked = !b2xSpeedClicked;
		        		
		        }
	}
}