Page 1 of 3 123 LastLast
Results 1 to 20 of 56

Thread: Clock Troubles

  1. #1
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Talking Clock Troubles

    I have been having troubles with this applet i have been making,
    the three problems i need solving are:
    1. the hands of the clock are moving to random places rather than clockwise
    2. i can not figure out how to refresh the graphics, the clock will not update unless you minimize and restore it
    3.this app was made from demonstrations so i have a lot of extra imports that i probable don't need, if you could tell me which i can delete that would be cool too.

    sadly I am not that great at java so please be as descriptive with your answers as possible


    P.S. if you have any input on how to improve the look of the clock or how to make it more efficient feel free to comment.
    Last edited by brycepower1; 05-10-2011 at 03:53 AM. Reason: removing code

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    To have it update every second, you'd need a thread that does that. Also, you could probably cut down a lot of code by using for-loops when drawing the clock.

  3. #3
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    what code would you use to update it though?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, if you post code please use code tags.
    [code]
    YOUR CODE HERE
    [/code]

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    In order to repaint an object, you can call the repaint() method. I'll leave the rest of the coding to you; you'll likely learn a whole lot more that way.

  6. #6
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    ha ha thanks

    by the way the applet is 500 by 500 with the clock in the center hence why the hands in the drawline methods start at 250,250 anyone have any ideas why the hands aren't moving as i want them too?
    Last edited by brycepower1; 05-09-2011 at 11:26 PM. Reason: more info

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Remember that Math.sin and Math.cos takes their argument in radians, not degrees. There's a nifty method in the Math class to fix that though! You can read the cods for it at Math (Java Platform SE 6)

  8. #8
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    are you perhaps talking about the toDegrees method?

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Other way around; you have degrees right now (seconds*6), you want radians.

  10. #10
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    cool thank you, i never would have figured that out on my own (been awhile since i have taken math)

    any ideas on which imports i don't need?

    and what did you mean by for loops?

  11. #11
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    In general, extra imports doesn't hurt (there are special cases where they do hurt though!). If you want to remove the ones you don't need, try commenting out all of them and see which classes can't be found, and add those imports back. Generally not really needed though.

  12. #12
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    hmmm i changed the seconds hand
    and now the second hand doesnt show up
    Last edited by brycepower1; 05-10-2011 at 03:53 AM.

  13. #13
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Here's why: Math.sin takes an angle in radian. Math.toRadians changes an angle in degrees to an angle in radians. Your angle in radians is seconds*6. Thus, you need to move the Math.toRadians inside the Math.sin and Math.cos method calls.

  14. #14
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Also, unless I'm confused (it's been years since I've dealt with angles, sin and cos) you shouldn't need to split it between four different calculations. One for X and one for Y should be enough.

  15. #15
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    that... was a bad mistake

    however it looks like the hands are moving properly now other than me needing to fix the negatives in the codes

  16. #16
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    I'm guessing all the hands stuck to the top-right part of the clock? :P

    EDIT: Hmm. Or bottom right perhaps? Now I got unsure.

  17. #17
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    unless im mistaken i don't think it works past 90 degrees i could be wrong though

    i'll experiment a little

  18. #18
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    they are stuck in bottom right actually, though i haven't given it enough time for the minutes and hour hands though lol

  19. #19
    brycepower1 is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    o lol i know why its acting up like this, the way i have it set up the angle should never exceed 90 but i didnt put a limit on it yet so it will just keep on bouncing till i do

  20. #20
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Actually, you're not that far off now. Just have to remember that sin and cos can return negative numbers...

Page 1 of 3 123 LastLast

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 10:10 AM
  2. Integer troubles
    By Tb0h in forum New To Java
    Replies: 11
    Last Post: 08-13-2009, 04:56 PM
  3. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-14-2009, 11:26 PM
  4. Image troubles
    By Theodoreb in forum New To Java
    Replies: 24
    Last Post: 07-14-2009, 12:41 AM
  5. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM

Tags for this Thread

Posting Permissions

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