Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 56

Thread: Clock Troubles

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

    Default

    ok i did in fact get it to run off of 1 if statement but its off my 30 seconds and i have absolutely no idea why

    if ((second >= 00)&&(second <= 59)){
    xsecond = (int)(250+(-100*(Math.sin(Math.toRadians(second*6)))));
    ysecond = (int)(250+(100*(Math.cos(Math.toRadians(second*6)) )));

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

    Default

    ha never mind i got it! now i just need to figure out that refresh thing you were talking about, do you know where i could find an article on it?

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

    Default

    Hmm... If I'm correct, it should be really easy to fix. Remember that 0, 0 is at the top left corner of the window, so in order to move upwards you need to reduce Y, and to move rightwards you need to increase X. Hopefully that's enough of a hint; otherwise I'll just spell it out (even though I don't like doing that).

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

    Default

    Heh. Unfortunately none come to mind right away, but there's likely one among the various tutorials on the official website. I'll go digging a bit, but perhaps someone else knows?

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

    Default

    thank you you have been a great help =)

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

    Default

    Ah, found one! Hopefully this'll help you; otherwise I can't help much, as I'm heading out now. Good luck!

    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

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

    Default

    thanks again

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

    Default

    im still having trouble with refresh codes anyone else know anything about them?

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

    Default

    i did figure out repaint() however its buffering is terrible anyone know about that?

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

    Default

    The flickering comes from how Java repaints the objects. Here's roughly the steps that Java takes when a call to repaint() comes in (this could differ a little bit depending on the type of object repainted, but anything that inherits Container somewhere down the line, including Applet, does this):

    First, it fetches the graphics to be used for the object.
    Next, it calls update(Graphics g)
    The update-method then clears the component.
    It then calls paint(Graphics g)

    And, just for fun of it, this is the update method from Container:
    Java Code:
        public void update(Graphics g) {
            if (isShowing()) {
                if (! (peer instanceof LightweightPeer)) {
                    g.clearRect(0, 0, width, height);
                }
                paint(g);
            }
        }
    Can you figure out what to do from that?

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

    Default

    alas i can not, were in the code do i put it? or do i put the code into it?

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

    Default

    Basically, it's the update method that's causing the flicker at the moment. What you need to do is override it by creating another update-method in your own class. Just copy-and-paste the one I showed and remove the clearRect-line and the if-statement surrounding it. This might cause other problems though, but let's take it one step at a time.

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

    Default

    alright so i copy and pasted it, hopefully like you said and it said illegal start of type on the "if (! (peer instanceof LightweightPeer)) {" line of code

    Java Code:
        class DoubleBufferedCanvas extends Canvas {
            if (! (peer instanceof LightweightPeer)) {
                g.clearRect(0, 0, width, height);
            }
            paint(g);

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

    Default

    Don't make a new class; just put the method update(Graphics g) without the if-statement with the Lightweight and without the clearRect-statement. That way you override the original update-method which caused the flickering. Like this:
    Java Code:
        public void update(Graphics g) {
            if (isShowing()) {
                paint(g);
            }
        }
    This can, as I said, cause other issues but we'll take care of those if they appear.

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

    Default

    my mistake, the code is now located
    Java Code:
        public void paint(Graphics g){
            if (! (peer instanceof LightweightPeer)) {
                g.clearRect(0, 0, width, height);
            }
            paint(g);
    however i still get an error but now it said
    peer is not public in java.awt.component; cannot be accessed from outside package

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

    Default

    i rewrote the code how you have it however the screen comes out blank, does that line of code need to be placed somewhere special?

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

    Default

    ... Uh... what? Why did you make paint call itself? Keep paint as it was, and just add the update-code.

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

    Default

    so like this?
    Java Code:
        public void update(Graphics g){       
            if (isShowing()) {
                paint(g);
    
            }
    sorry im a little confused, but the screen is still coming out blank

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

    Default

    Hmm. Strange; it shouldn't do that. Try adding the clearRect-line again in the update-method before the call to paint to see if that makes it work again.

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

    Default

    well.... i found out that if i move the update class you made to the end like:
    Java Code:
                        xhour = (int)(250+(70*(Math.sin(Math.toRadians(hour*0.5)))));
                        yhour = (int)(250+(-70*(Math.cos(Math.toRadians(hour*0.5)))));
                    }
                    g.drawLine(250,250,xhour,yhour);
                    g.drawLine(250,250,xminute,yminute);
                    g.drawLine(250,250,xsecond,ysecond);
                }
            }
            if (isShowing()) {         
                paint(g);
            }
        }
    }
    then it does refresh however the numbers stack up on top of each other in the time and the clock flickers, untill it eventualy fails at about 20 seconds

    what i cant figure out is why their isn't an after image of the hands like their is of the time

Page 2 of 3 FirstFirst 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
  •