Buttons on Clock
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Buttons on Clock

  1. #1
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Exclamation Buttons on Clock

    Whenever the Military Time button is pushed the Military time is displayed, but only for one second. It then reverts back to standard 12 hour digital time. What is going on, and can anyone help me to fix this?
    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;
    	TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int hrHand, mnHand, secHand, radius = 85, hrLength, mnLength,
    		secLength, cx, cy, x1, y1, x2, y2;
    	private Button buttonMilt;
    	private boolean buttonMiltClicked = false;
    	
    	public void init()
    	{
    		buttonMilt = new Button("Military Time");
    		add(buttonMilt);
    		buttonMilt.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
    	}
    		
    	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	
    			
    				SimpleDateFormat formatter									   //********
    					= new SimpleDateFormat("hh:mm:ss a", Locale.getDefault()); //*Gets time and 
    				Date date = cal.getTime();									   //*puts it into a digital timer
    				timeString = formatter.format(date);						   //********
    			
    				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);
    		
    		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
    		g.drawString(timeString, 75, height-105); //timer
    		
    	}
    	
    	public void actionPerformed(ActionEvent e)  
        {  
    		Calendar cal = Calendar.getInstance();						   //*gets time from the system
    		
    		if(e.getSource()==buttonMilt){
    			
    			if(buttonMiltClicked == true)
    			{
    			SimpleDateFormat formatter									   //********
    			= new SimpleDateFormat("HH:mm:ss a", Locale.getDefault()); //*Gets time and 
    			Date date = cal.getTime();									   //*puts it into a digital timer
    			timeString = formatter.format(date);						   //********
    			}
    			buttonMiltClicked = true;
    			repaint();
    		}
    			
        }
    	
    }
    Program created in Eclipse.
    The action performed is at the bottom of the code, and the time is in the run() not the init().
    You will probably need to push the button a couple of time to actually see the digital time go into Military time.
    Also the applet window will need to be extended to actually see the button.
    Please excuse some of my code and the way it is formatted. Thank you for you time and help.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Buttons on Clock

    Every time your the while loop in the run method executes it sets the clock back to standard time.

    BTW variables which can be modified by different threads should be marked as volatile.

    Why create a new SimpleDateFormat object every second, why not create it once and reuse it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    Ok, I fixed it, but in a "rough draft" sort of way. I just went and put the military button into run(). It works, but it seems to be extremely messy. I plan on adding more buttons that affect the time of the clock such as run the clock in reverse speed, 120xspeed, and things of the sort. If I continue to add buttons this way it would look like a "code tumor" has developed within run(). Is there another way to clean it up instead of this "quick fix."
    I left parts of the code in that would need to come out according to the fix.
    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;
    	TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int hrHand, mnHand, secHand, radius = 85, hrLength, mnLength,
    		secLength, cx, cy, x1, y1, x2, y2;
    	private Button buttonMilt;
    	private boolean buttonMiltClicked = false;
    	
    	public void init()
    	{
    		buttonMilt = new Button("Military Time");
    		add(buttonMilt);
    		buttonMilt.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
    	}
    		
    	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(buttonMiltClicked == false)
    				{
    				SimpleDateFormat formatter									   //********
    					= new SimpleDateFormat("hh:mm:ss a", Locale.getDefault()); //*Gets time and 
    				Date date = cal.getTime();									   //*puts it into a digital timer
    				timeString = formatter.format(date);						   //********
    				}
    				else{					
    					if(buttonMiltClicked == true)
    					{
    					SimpleDateFormat formatter									   //********
    						= new SimpleDateFormat("HH:mm:ss a", Locale.getDefault()); //*Gets time and 
    					Date date = cal.getTime();									   //*puts it into a digital timer
    					timeString = formatter.format(date);						   //********
    					}
    						buttonMiltClicked = true;
    						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);
    		
    		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
    		g.drawString(timeString, 75, height-105); //timer
    		
    	}
    	
    	public void actionPerformed(ActionEvent e)  
        {  
    		Calendar cal = Calendar.getInstance();						   //*gets time from the system
    		
    		if(e.getSource()==buttonMilt){
    			
    			if(buttonMiltClicked == true)
    			{
    			SimpleDateFormat formatter									   //********
    			= new SimpleDateFormat("HH:mm:ss a", Locale.getDefault()); //*Gets time and 
    			Date date = cal.getTime();									   //*puts it into a digital timer
    			timeString = formatter.format(date);						   //********
    			}
    			buttonMiltClicked = true;
    			repaint();
    		}
    			
        }
    	
    }
    Thanks to all.

  4. #4
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Red face Re: Buttons on Clock

    Ok got more help here is the code a bit more cleaned up at the Military button. I know that the paint() should be seperated into more smaller methods, but I will fix it after I get the core of the program done. I'll ask for more help later thanks everyone!
    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;
    	TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int 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;
    	
    	String regTimeFormat = "hh:mm:ss a";
    	String milTimeFormat = "HH:mm:ss a";
    	SimpleDateFormat formatter = null;
    	Calendar cal = Calendar.getInstance();
    	
    	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
    		
    		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 (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
    		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);
    		
    		            timeString = formatter.format(cal.getTime());
    		
    		            repaint();
    		
    		        }
    		
    	}
    }

  5. #5
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    I am having a similar problem with having the seconds increasing the speed of the clock by 2. What can I do without having to do if else statements within run()? Also I've tried to do something sort of like the Military button.
    The part I am talking about is at the bottom of the program. Thanks.

    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;
    	TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int 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;
    	
    	String regTimeFormat = "hh:mm:ss a";
    	String milTimeFormat = "HH:mm:ss a";
    	SimpleDateFormat formatter = null;
    	Calendar cal = Calendar.getInstance();
    	
    	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
    		
    		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 (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
    		g.drawString(timeString, 75, height-105); //timer
    		
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    
    		        String formatStr = null;
    		        Calendar cal = Calendar.getInstance();
    		
    		        if (e.getSource() == buttonMilt) {
    		            buttonMiltClicked = !buttonMiltClicked;
    		            if(buttonMiltClicked)
    		            {
    		                formatStr = milTimeFormat;
    		            }
    		            else 
    		            {
    		                formatStr = regTimeFormat;
    		            }  
    		            formatter = new SimpleDateFormat(formatStr);
    		            timeString = formatter.format(cal.getTime());
    		            repaint();
    		        }
    		        if (e.getSource()==b2xSpeed){
    		        	b2xSpeedClicked = !b2xSpeedClicked;
    		        		if (b2xSpeedClicked)
    		        		{
    		        			secHand = 2*cal.get(Calendar.SECOND);					//*get seconds	
    		        		}
    		        		else
    		        		{
    		        			secHand = cal.get(Calendar.SECOND);						   //*get seconds	
    		        		}
    		        		repaint();
    		        }
    		
    	}
    }

  6. #6
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    Ok gotta take a step back on the previous post. Clock seconds still isn't fixed to where they tick appropriately on the digital ticker. What is wrong? Can anyone give a pointer.
    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;
    	TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int hrHand, mnHand, secHand, radius = 85, hrLength, mnLength,
    		secLength, cx, cy, x1, y1, x2, y2;
    	private Button buttonMilt;
    	private boolean buttonMiltClicked = false;
    	
    	String regTimeFormat = "hh:mm:ss a";
    	    String milTimeFormat = "HH:mm:ss a";
    	
    	    SimpleDateFormat formatter = null;
    	
    	    Calendar cal = Calendar.getInstance();
    
    	
    	public void init()
    	{
    		buttonMilt = new Button("Military Time");
    		add(buttonMilt);
    		buttonMilt.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
    		
    		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 (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);
    		
    		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
    		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);
    			
    			            timeString = formatter.format(cal.getTime());
    			
    			            repaint();
    			
    			        }
    			
    			    }
    
    			
        }
    Thanks

  7. #7
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    Second question, Why does the 2x button not reset when clicked for a second time. The original time starts where it left off, but it seems that 2x the speed keeps running in the background. Whenever the 2x button is pushed again it should speed up at the original time not where it left off? Does anyone know what is going on here? Problem is in run().
    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;
    	//TextField text = new TextField(20);
    	Thread t = null;
    	boolean threadSuspended;
    	String timeString = "";
    	int width, height;
    	
    	Calendar calendar = Calendar.getInstance();
    	int 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;
    	
    	Calendar cal = Calendar.getInstance();
    	String regTimeFormat = "hh:mm:ss a";
    	String milTimeFormat = "HH:mm:ss a";
    	SimpleDateFormat formatter = null;
    	
    	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(500);
    			}
    		}
    		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
    		g.drawString(timeString, 75, height-105); //timer
    		
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    
    		        String formatStr = null;
    		        Calendar cal = Calendar.getInstance();
    		
    		        if (e.getSource() == buttonMilt) {
    		            buttonMiltClicked = !buttonMiltClicked;
    		            if(buttonMiltClicked)
    		            {
    		                formatStr = milTimeFormat;
    		            }
    		            else 
    		            {
    		                formatStr = regTimeFormat;
    		            }  
    		            formatter = new SimpleDateFormat(formatStr);
    		            timeString = formatter.format(cal.getTime());
    		            repaint();
    		            }
    		        
    	
    
    		        if (e.getSource()==b2xSpeed){
    		        	b2xSpeedClicked = !b2xSpeedClicked;
    		        		
    		        }
    	}
    }
    Thanks to all.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Buttons on Clock

    I'm going to ignore all the questions prior to your last post as I've no idea as to whether or not they still apply.

    Whenever the 2x button is pushed again it should speed up at the original time not where it left off?
    Your 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.

    You 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. ie

    Code:
    x2TimeMillis = (2 x (currentTimeMillis - startTimeMillis)) + startTimeMillis;
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    Yeah I was still trying to figure out why the seconds never tick in the digital clock part of the program. Does it have anything to do with it not being in the while loop within run()?

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Buttons on Clock

    You only set timeString when the milt button is pressed and so it won't change until the button is next pressed.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    Yes, got digital time to work properlly.
    Calendar cal = Calendar.getInstance();
    timeString = formatter.format(cal.getTime());
    These need to be put into the paint function, above where timestring is drawn.
    Thanks.
    Still trying to get the 2x speed to work properly.
    How do you actually get the milliseconds to propley count the start time, and then subtract it from the current time? I mean actually getting the millisecond time from the sytem then doing the computation.
    Thanks

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Buttons on Clock

    Be careful what you put in the paint method it can get called very often (for example when moving windows etc) and so should execute quickly.

    Current time is
    Code:
    time = System.currentTimeMillis();
    and Date objects have a getTime() method that returns the millis.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Buttons on Clock

    What is wrong here?
    Code:
    if (b2xSpeedClicked)
    	        		{
    						long start = System.currentTimeMillis();
    						secHand = cal.get(Calendar.SECOND);
    						long end = System.currentTimeMillis(); 
    						millsec = (int) (((end-start)*2)+ start);
    						secHand = millsec/500;
    	        		}
    	        		else
    	        		{
    	        			secHand = cal.get(Calendar.SECOND);						   //*get seconds	
    	        		}
    First it gets the start time in line1
    now is it supposed to multiply in the seconds by 2, in line 2?
    After it computes the end or current time is saved in end?
    Then millsec is computed.
    divide milliseconds by 500 and return to secHand.
    It still jumps?

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Buttons on Clock

    How will start and end ever be more than about 1 millisecond different.

    You need to set the start time when the x2 button is selected.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center