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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Drawing lines using angles in Java HELP

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation 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,

    Shady
    Last edited by shadysback; October 8th, 2012 at 02:12 PM. Reason: Code explanation


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Drawing lines using angles in Java HELP

    Quote Originally Posted by KevinWorkman View Post
    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)
    Step 2. Convert into radians,
    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. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Drawing lines using angles in Java HELP

    Quote Originally Posted by KevinWorkman View Post
    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.
    Last edited by shadysback; October 8th, 2012 at 02:11 PM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    shadysback (October 8th, 2012)

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Drawing lines using angles in Java HELP

    Quote Originally Posted by KevinWorkman View Post
    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:
    Last edited by shadysback; October 8th, 2012 at 02:11 PM.

  9. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Algorithms to smoothen lines using java and eclipse
    By bczm8703 in forum Algorithms & Recursion
    Replies: 7
    Last Post: March 18th, 2012, 11:55 PM
  2. Drawing images with different angles
    By gargamel7 in forum AWT / Java Swing
    Replies: 24
    Last Post: September 13th, 2011, 06:04 AM
  3. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 07:58 AM
  4. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  5. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM

Tags for this Thread