School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,132 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,935 people online right now. Registration is fast and FREE... Join Now!



clock applet shows wrong time!

clock applet shows wrong time! Rate Topic: -----

#1 xoxou  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 24-October 08


Dream Kudos: 0

Post icon  Posted 24 October 2008 - 07:38 AM

Hi,i have written an analog clock web applet, and it works fine. But the problem is that the hour hand and the minute hand have interchanged positions.So if the time is 5 'o' clock,it shows 12:25!!.i cant figure out where the problem is.please help.
here's the code for the clock:
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.applet.*;

public class Clock extends Applet implements Runnable {

	final static int midX = 180;
	final static int midY = 180;
	final static int rad = 90;
	Thread aclock = null;
	ClockHand seconds, minutes, hours;
	int sec, min, hr;
	Font fnt;
	boolean compulsoryRun = true;
	Calendar time;
	Calendar clocktime;
	String Timestart;
	String Timstart;
	Date dat;
	String da;
	String date;

	public void init() {
		setBackground(Color.green);
		setForeground(Color.gray);
		clocktime = Calendar.getInstance();
		time = Calendar.getInstance();
		fnt = new Font("Arial", Font.BOLD, 24);
	}

	public void start() {
		compulsoryRun = true;
		aclock = new Thread(this);
		seconds = new ClockHand(clocktime.get(Calendar.SECOND), rad - 15, 15, Color.black, 0);
		minutes = new ClockHand(clocktime.get(Calendar.MINUTE), rad * 5 / 7, Color.red, 9);
		hours = new ClockHand(clocktime.get(Calendar.HOUR_OF_DAY), rad - 15, 15, Color.blue, 14);
		sec = time.get(Calendar.SECOND);
		min = time.get(Calendar.MINUTE);
		hr = time.get(Calendar.HOUR_OF_DAY);
		hr = hr % 12;
		dat = new Date();
		da = new String();
		date = new String();
		da = dat.toString();
		date = da.substring(0, 10);
		aclock.start();
	}

	public void stop() 
{
		aclock = null;
		compulsoryRun = false;
		Timestart += "stopped";
		Timstart += "stopped";
	}

	public void run()
{
		repaint();
		while (compulsoryRun)
  {
			repaint(midX - rad - 5, midY - rad - 5, midX + rad + 5, midY + rad + 5);
			try 
	{
				Thread.sleep(1000);
	 } catch (InterruptedException ex) 
		   {
				System.out.println("Exception raised is: " + ex);
			}
			sec++;
			if (sec == 60)
				{
				sec = 0;
				min++;
			}
			if (min == 60) {
				min = 0;
				hr++;
			}
			if (hr == 13) {
				hr = 1;
				Timstart = hr + ":" + min + ":" + sec;
			}
			seconds.value++;
			if (seconds.value == 60) {
				seconds.value = 0;
				minutes.value++;
			}
			if (minutes.value == 60) {
				minutes.value = 0;
				hours.value++;
			}
			if (hours.value == 13) {
				hours.value = 1;
				Timestart = (int) hours.value + ":" + (int) minutes.value + ":" + (int) seconds.value;
			}
		}
	}

	private void assignAngles() {
		seconds.ang = (seconds.value) * Math.PI / 30;
		minutes.ang = (minutes.value) * Math.PI / 30 + (seconds.value) * Math.PI / 1800;
		hours.ang = (hours.value) * Math.PI / 6 + (minutes.value) * Math.PI / 360;
	}

	public void paint(Graphics g) {
		update(g);
	}

	public void update(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(Color.yellow);
		g.fillOval(midX - rad, midY - rad, 2 * rad, 2 * rad);
		g.setColor(Color.black);
		g.drawOval(midX - rad, midY - rad, 2 * rad, 2 * rad);
		g.setColor(Color.gray);
		g.drawOval(midX - rad - 2, midY - rad - 2, 2 * rad + 4, 2 * rad + 6);
		g.drawOval(midX - rad - 3, midY - rad - 3, 2 * rad + 6, 2 * rad + 6);
		g.setColor(Color.black);	
		g.fillRect(midX+rad/2-28,midY-7,68,14);
		g.setColor(Color.white);
		g.drawString(date,midX+rad/2-28,midY+4);
		for (int i = 0; i < 60; i++) {
			if (i % 5 == 0) {
				g.setColor(Color.black);
				g.drawLine(midX + (int) ((rad * 6 / 7) * Math.sin(Math.toRadians(i * 6))), midY - (int) ((rad * 6 / 7) * Math.cos(Math.toRadians(i * 6))), midX + (int) (rad * Math.sin(Math.toRadians(i * 6))),
						midY - (int) (rad * Math.cos(Math.toRadians(i * 6))));
			} else {
				g.setColor(Color.gray);
				g.drawLine(midX + (int) ((rad * 6.5 / 7) * Math.sin(Math.toRadians(i * 6))), midY - (int) ((rad *
						6.5 / 7) * Math.cos(Math.toRadians(i * 6))), midX + (int) (rad * Math.sin(Math.toRadians(i *
						6))), midY - (int) (rad * Math.cos(Math.toRadians(i * 6))));
			}
		}
		showStatus(Timestart);
		showStatus(Timstart);
		assignAngles();

		hours.disp(g);
		minutes.disp(g);
		seconds.disp(g);

		g.setColor(Color.red);
		g.fillOval(midX - 3, midY - 3, 6, 6);
		g.setColor(Color.green);
		g.fillOval(midX - 1, midY - 1, 2, 2);
		g.setColor(Color.black);
		g.setFont(fnt);
		showStatus("Current Time ---- " + hr + ":" + min / 10 + min % 10 + ":" + sec / 10 + sec % 10);
	}

	class ClockHand {

		private int leng;
		private int hlength;
		private Color handColour;
		private Shape hand;
		private double rect;
		double ang;
		double value;
		AffineTransform at;

		ClockHand(double val, Color colour, int ret) {
			leng = 100;
			value = val;
			hlength = 0;
			handColour = colour;
			rect = Math.PI * ret / 180;
			initial();

		}

		ClockHand(double val, int leng, Color colour, int ret) {
			this.leng = leng;
			this.value = val;
			hlength = 0;
			handColour = colour;
			rect = Math.PI * ret / 180;
			initial();
		}

		ClockHand(double val, int leng, int ha, Color colour, int ret) {
			this.leng = leng;
			this.value = val;
			hlength = ha;
			handColour = colour;
			rect = Math.PI * ret / 180;
			initial();
		}

		void initial() {
			int x[] = new int[4];
			int y[] = new int[4];
			x[0] = Clock.midX - hlength;
			y[0] = Clock.midY;
			x[1] = Clock.midX + (int) ((leng / 2) * Math.cos(rect));
			y[1] = Clock.midY - (int) ((leng / 2) * Math.sin(rect));
			x[2] = Clock.midX + leng;
			y[2] = Clock.midY;
			x[3] = Clock.midX + (int) ((leng / 2) * Math.cos(rect));
			y[3] = Clock.midY + (int) ((leng / 2) * Math.sin(rect));
			hand = new Polygon(x, y, 4);
		}

		void disp(Graphics g) {
			Graphics2D gd = (Graphics2D) g;
			g.setColor(handColour);

			at = AffineTransform.getRotateInstance(ang - Math.PI / 2, Clock.midX, Clock.midY);
			gd.fill(at.createTransformedShape(hand));
			gd.draw(at.createTransformedShape(hand));
		}
	}
}



And here's the html code for the applet:
<html>
<applet code="Clock" height = 350 width = 370 >
</applet>
</html>


Thanks.
Was This Post Helpful? 0
  • +
  • -


#2 g00se  Icon User is offline

  • D.I.C Lover
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,751
  • Joined: 19-September 08


Dream Kudos: 0

Posted 25 October 2008 - 01:47 AM

Your clock hand ctors should probably be simplified - they probably confused you. If you swap some parameters around to them, it should fix it:

				// 4 args
				minutes = new ClockHand(
						clocktime.get(Calendar.MINUTE),
						//(rad * 5) / 7,
						rad - 15,
						Color.red,
						9
						);
				// 5 args
				hours = new ClockHand(
						clocktime.get(Calendar.HOUR_OF_DAY),
						//rad - 15,
						(rad * 5) / 7,
						15,
						Color.blue,
						14
						);



This post has been edited by g00se: 25 October 2008 - 01:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 xoxou  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 24-October 08


Dream Kudos: 0

Posted 25 October 2008 - 08:51 AM

Thanks a lot :D , that worked. :^: . helped me a lot.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month