Results 1 to 10 of 10
  1. #1
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default How to make swing.Timer as a separate class

    I want to write a single class for swing.timer

    so i can create a static method in that class.

    everytime when I need to use the timer I can just call it to create a new timer.

    Can anyone show an example or give me some hints?
    many thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cross-posted in the sun forums: http://forums.sun.com/thread.jspa?th...35990&tstart=0 and ?elsewhere?

    To the original poster, welcome to the forum, and I hope your question gets answered, but I do request that if you cross-post a question you provide links to the other cross-posts. Nobody likes duplicating work that's already been done, and in fact many resent this. A simple solution is to again provide links or at least an indication of where you've cross-posted the question. Also, the person helping you in the other thread (me, in fact) remarked that it was difficult to help you come up with a specific solution as you did not reveal the specifics of your problem. Do you think that it would be wiser to have provided those specifics (which you haven't provided here either) rather than to have cross-posted? Thanks for your cooperation.

    Mod
    Last edited by Fubarable; 04-17-2010 at 12:00 AM.

  3. #3
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    I must apologize for cross positing.
    The problem I have encounter on my work is something like this.
    Java Code:
    if (!isJumping() && !isWalking() &&!isAttacking()){
                        if (!getFaceDirection()){
                        setBodyImage(new BodyImage("1idleL.gif"));
                    }else{
                        setBodyImage(new BodyImage("1idleR.gif"));
                    }}
    this is when the player is not walking/jumping/attacking. it will be painted in a idle picture.
    Java Code:
    if (!player.getFaceDirection()){
                                player.isAttacking()
                                player.shootLeft();
                                sound1.play();
                                player.setBodyImage(new BodyImage("1attackL.gif"));
                                player.stopAttacking()
                                
                        }else{
                            player.shootRight();
                            sound1.play();
                            player.setBodyImage(new BodyImage("1attackR.gif"));
                            
                       }}
    the code in the shoot method is just happenning too soon. the attack animation will never show.
    i tried use .wait, but its not workng..
    and i try create timer between them. i got errors like the picture attached
    Attached Thumbnails Attached Thumbnails How to make swing.Timer as a separate class-error.jpg  

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    So which thread is the active discussion thread, this one, or the other one in the Sun Swing Forum? Do you have cross-posts anywhere else? Once you've decided this, please post in all cross-posts which thread is the active one.

  5. #5
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    This one should be the active one,I only post this thread twice.
    I apologize again for cross posting.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by nethz13 View Post
    the code in the shoot method is just happenning too soon. the attack animation will never show.
    i tried use .wait, but its not workng..
    Yep, wait is a bad idea.

    and i try create timer between them. i got errors like the picture attached
    Your best bet is to solve why this error is occurring. You're trying to create an anonymous class that implements ActionListener, and the error is telling you that you must implement the method actionPerformed(...). The error suggests that you've not implemented it, and if you check your spelling of the actionPerformed method, you'll see why it's causing an error.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by nethz13 View Post
    This one should be the active one,I only post this thread twice.
    I apologize again for cross posting.
    OK, I have posted a notification in the other thread.

  8. #8
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yep, wait is a bad idea.


    Your best bet is to solve why this error is occurring. You're trying to create an anonymous class that implements ActionListener, and the error is telling you that you must implement the method actionPerformed(...). The error suggests that you've not implemented it, and if you check your spelling of the actionPerformed method, you'll see why it's causing an error.
    Thank you, I think I kinda solve it by firing a repeat timer,(thats what I am understand from looking at the code.)
    However if I want to make the timer fire only once,by invoking setRepeats(false).... can it be done?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by nethz13 View Post
    Thank you, I think I kinda solve it by firing a repeat timer,(thats what I am understand from looking at the code.)
    However if I want to make the timer fire only once,by invoking setRepeats(false).... can it be done?
    Yes, but the easiest way is to create a Timer variable, assign it your constructed Timer, set the repeats property, and only then start the Timer object (using the variable).

  10. #10
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes, but the easiest way is to create a Timer variable, assign it your constructed Timer, set the repeats property, and only then start the Timer object (using the variable).
    Thanks, I will try it now and see what I can get.

Similar Threads

  1. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  2. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  3. How do I make a timer run in the background?
    By caryr in forum Advanced Java
    Replies: 15
    Last Post: 09-04-2009, 02:34 AM
  4. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM

Posting Permissions

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