Results 1 to 13 of 13
Thread: Restart gif animation possible?
- 01-24-2011, 03:30 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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?
- 01-24-2011, 04:01 PM #2
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?
- 01-24-2011, 04:16 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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
- 01-24-2011, 04:20 PM #4
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
- 01-24-2011, 04:29 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Ill wait and see if someone got an answer, Otherwise Ill take that task on me :)
- 01-24-2011, 04:32 PM #6
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.
- 01-24-2011, 05:21 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-24-2011, 06:12 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
I looked that up actually.
Unfortunatly imageobserver does not contain any setter methods...
- 01-25-2011, 08:37 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I just had this inkling there was something you could do with it.
As I say, this is dredging up old memories.
- 01-25-2011, 05:45 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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
- 01-25-2011, 07:06 PM #11
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]; } } }
- 01-25-2011, 09:26 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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
- 01-25-2011, 09:29 PM #13
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
-
Level Restart Button
By Curtiz in forum AWT / SwingReplies: 4Last Post: 04-08-2010, 09:04 PM -
Restart main
By Syahrul in forum New To JavaReplies: 3Last Post: 11-22-2009, 08:34 AM -
Make application restart itself
By Singing Boyo in forum New To JavaReplies: 1Last Post: 03-22-2009, 02:49 AM -
Application restart in tomcat.....
By jithan in forum New To JavaReplies: 1Last Post: 09-10-2008, 03:31 PM -
Restart a For loop and increment an Int value
By dcnorman07 in forum New To JavaReplies: 1Last Post: 07-12-2008, 12:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks