Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Drawing lines using angles in Java HELP

1. ## Drawing lines using angles in Java HELP

Hi All,

Im making a clock in java and i need help drawing the minutes and hours hand. I have done some maths and came up with the following formula

The formula i am using above is x = a + rCos(t)
y = b + rSin(t)
where (x,y) are the points on the circle i am trying to find and (a,b) is the centre of the circle. t is the angle between the x-axis and the line joining (a,b) and (x,y)

My clock is drawing perfectly, only it is 90degrees clockwise, so 12.00 looks like 15.00.
Ive tried to add/subtract degrees to the formula but its not working.

ive only shown the problematic code above, i can show the rest if needed.

Thanks you very much for looking at this,

2. ## Re: Drawing lines using angles in Java HELP

I'd suggest throwing together an SSCCE that we can play with. I'm not sure I quite understand all the magic numbers in your code. Also note that the Math class has handy methods for converting between radians and degrees that might clear up some confusion.

3. ## Re: Drawing lines using angles in Java HELP

Originally Posted by KevinWorkman
I'd suggest throwing together an SSCCE that we can play with. I'm not sure I quite understand all the magic numbers in your code. Also note that the Math class has handy methods for converting between radians and degrees that might clear up some confusion.
Sorry about the confusion. Il try to clear it up here:

The user gives me two int's hr and min, which are the hour and minutes to display on this clock. I then convert each hour into degrees, so if i want to show 6 o'clock, i would do 6 * 30 = 180degrees
Similarly for the minutes, 1 minute = 6 degrees (360degrees / 60 minutes)
So if the user wants to show "30 then the program does the math 30*6 = 180 degrees.
There are two hands: a minute hand and an hour hand which uses these values to show the time.

Then i convert degrees into radians by multiplying by PI/180

Assuming HR, MIN is the user inputted time,

Step 1. Convert into degrees:
30HR degrees (hour), 6MIN degrees (minutes)
30HR * PI/180, 6MIN * PI/180
= HR*PI/6 (hours), MIN * PI/180 (minutes)

Then i substitute this in the formula i described in my above post.

Ps. i am trying to edit this code and learning about using the Math class' RadiansConversion method. Pls bear with me whilst i do that.

4. ## Re: Drawing lines using angles in Java HELP

Depending on how you're doing it, 6:00 shouldn't be 180 degrees, it's 90 degrees. You're assuming that straight up (12:00) is 0 degrees, but I believe that 0 degrees is heading straight to the right, 90 degrees is going down, 180 is to the left, and 270 is up (12:00). I could be wrong, but it would explain your problem.

5. ## Re: Drawing lines using angles in Java HELP

Originally Posted by KevinWorkman
Depending on how you're doing it, 6:00 shouldn't be 180 degrees, it's 90 degrees. You're assuming that straight up (12:00) is 0 degrees, but I believe that 0 degrees is heading straight to the right, 90 degrees is going down, 180 is to the left, and 270 is up (12:00). I could be wrong, but it would explain your problem.
Thats one posibility i considered, i tried to add 90degrees to my values but it still doesnt point up
i.e.

But what this does is make 9:00 the new 12:00.
I try 90-h, h-90 etc no matter what values i put in it still does not point up.

6. ## Re: Drawing lines using angles in Java HELP

Well, you're adding 90 to the hours and minutes and not an angle, which doesn't really make sense to me.

8. ## Re: Drawing lines using angles in Java HELP

Originally Posted by KevinWorkman
Well, you're adding 90 to the hours and minutes and not an angle, which doesn't really make sense to me.
Oh. How would you suggest i do it then?

Btw i have updated the code, more viewer friendly now:

9. ## Re: Drawing lines using angles in Java HELP

hey ive solved it now. Yeah i changed it so i was subtracting once the hours was changed to degrees. Thanks for your help!