Results 1 to 13 of 13
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Restart gif animation possible?

    Hello!
    I need help because I got this image.
    A gif animation.
    I CAN put it to loop, but that will cause problem


    See, Its an animation of a guy swinging a sword.
    Now each time I click enter, that animation will be rendered.
    Problem is that either I choose to make the gif loop.
    If so, then I dont know if the image will start at the end of the swing, in the begining or in the middle.

    If I have non looping, then after clicking enter once, the gif will not replay when I render it a second time.

    Can I restart a gif animation or set the current frame?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I do my animation with arrays of PNGs and handle all my own looping. Are you drawing this on a Swing component like a Canvas?

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    On a jpanel.
    I was gona do like you do, but I found these gifs and creating a animationhandler seems like another pain the in as :P

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well, the animation handler is handy in the sense that you have complete control over speed, direction, starting/stopping, etc... Also, it allows you to use 24bit PNG which looks amazing compared to gif, and supports better than 1 bit alpha channels. Sorry, I don't have a specific answer for you, only advice that there are many benefits to doing it manually. :D

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Ill wait and see if someone got an answer, Otherwise Ill take that task on me :)

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Alright, post if you need suggestions with doing it the [hard] way - though, it really can be quite simple with animation listeners, or simple timers.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Isn't this one of those things you use the ImageObserver for?
    Damned if I can remember exactly, but I'm sure I did something similar...years ago though, and not for work.

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I looked that up actually.
    Unfortunatly imageobserver does not contain any setter methods...

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    I just had this inkling there was something you could do with it.
    As I say, this is dredging up old memories.

  10. #10
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I guess I could make it reload the gif each time it needs to play again.
    Tho Only doing this on the few animations that really needs to start from the start would ease the pain on the cpu... Guess theres no other way..

    64, If you got tips on animation handling I suggest u give em now :D

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Alright. There are several techniques but one of the simplest is this:
    Load each frame as a png or the like into BufferedImages:
    Java Code:
    //This is a really quick and dirty example off of the top of my head, but I think it should at least give you an idea:
    public class Misc {
        BufferedImage[] crazyDuckFrames;
    
        public static void main(String[] args) {
            new Misc();
        }
    
        public Misc() {
            loadSprites();
            Animation a = new Animation(crazyDuckFrames);
            javax.swing.Timer t = new javax.swing.Timer(250, a);
            t.start();
        }
    
        private void loadSprites(){
            crazyDuckFrames = new BufferedImage[5];
            for (int i = 0; i < 5; i++) {
                try {
                    ImageIO.read(new File("sprites/" + i + "duck.png"));
                } catch (IOException ex) {
                    Logger.getLogger(Misc.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //Do this for each sprite type
        }
    
        private class Animation implements ActionListener{
            BufferedImage[] frames;
            int index = 0;
    
            public Animation(BufferedImage[] frames){
                this.frames = frames;
            }
    
            public void actionPerformed(ActionEvent e) {
                if(index < frames.length-1){
                    index++;
                }else{
                    index = 0;
                    // or you could reverse direction and cycle back and forth to
                    // save frames.
                }
            }
    
            public BufferedImage getFrame(){
                return frames[index];
            }
    
        }
    }

  12. #12
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I was thinking of something like that, thanks for taking time :D
    Appriciate it, tho I can't copy anything as no parts of my game is to be taken from the internet.
    Design ideas is fortunatly not included in that agreement :P

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah, I didn't invent this concept, it exists more or less in this form all over the place. The basic idea is that the timer fires every n milliseconds, and makes an event. The event can do anything - either increment a frame directly, or simply trigger a screen redraw. In the latter case, elapsed time since last update can be sent to every animation object, and a determination can be made in the object on whether to advance to the next frame or not. Thats a little bit more work, but gives smoother animation, esp if the sprites will travel around the screen! Good luck :D

Similar Threads

  1. Level Restart Button
    By Curtiz in forum AWT / Swing
    Replies: 4
    Last Post: 04-08-2010, 09:04 PM
  2. Restart main
    By Syahrul in forum New To Java
    Replies: 3
    Last Post: 11-22-2009, 08:34 AM
  3. Make application restart itself
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 03-22-2009, 02:49 AM
  4. Application restart in tomcat.....
    By jithan in forum New To Java
    Replies: 1
    Last Post: 09-10-2008, 03:31 PM
  5. Restart a For loop and increment an Int value
    By dcnorman07 in forum New To Java
    Replies: 1
    Last Post: 07-12-2008, 12:24 AM

Posting Permissions

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