Results 1 to 10 of 10
  1. #1
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default How should an Animation class be implemented?

    I've tried to code an Animation class, which creates animations of different pictures following eachother.

    Heres what I've come up with:

    Java Code:
    package grxTest;
    
    import java.awt.*;
    import java.util.*;
    
    import javax.swing.ImageIcon;
    
    public class Animation {
    	private ArrayList images;
    	private int currFrame;
    	
    	public Animation() {
    		images = new ArrayList();
    		currFrame = -1;
    	}
    	
    	public void addFrame(Image i) {
    		images.add(i);
    	}
    	
    	public static Image loadImage(String s) {
    		return new ImageIcon(s).getImage();
    	}
    	
    	public Image getImage() {		
    		currFrame++;
    		
    		if(currFrame == images.size()-1) {
    			currFrame = 0;
    		}
    		
    		return (Image)images.get(currFrame);
    	}
    }
    The 3 images are swapping too fast. I know I can sleep the thread, but I dont wanna do that. What can I do to specify that the first image should show for 300 ms, and the nest two ones for 600 ms?
    Java beginner.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Java Code:
    // Not tested
    
    long CurrentTime = System.currentTimeMillis();
    while (System.currentTimeMillis() < CurrentTime + 300) {
        // Do nothing
    }
    Although it would be a lot more sensible to use thread.sleep. How come you are not doing that?

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Where exactly are you drawing your animation? This class appears to just hold the images that will be drawn are return the next one to draw.

    You could try putting the drawing of the animation in a Timer.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Go look at the Java tutorial. It has a section on using Swing timers with an example program: TumbleItem that does what you're trying to do.

    Sorry no URL, I have the tutorial on my computer.

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    long CurrentTime = System.currentTimeMillis();
    while (System.currentTimeMillis() < CurrentTime + 300) {
    // Do nothing
    }

    are you nuts :p you are making this thread taking over all the memory over everything else. if you want to do that place between the while
    Threading.getCurrentThread().yield(); or something like that

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Heh, didnt say it was efficient. :P

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

    Default

    Quote Originally Posted by berkeleybross View Post
    Heh, didnt say it was efficient. :P
    It's not a question of efficiency, but it will likely tie up the main Swing thread, the EDT, and put the whole application to sleep. I think the best solution is Norm's: use a Swing Timer as this is what they're built for. Yield won't help here either.

  8. #8
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    The reason I dont wanna use sleep is because I believe it is inefficient. Because if Im making a game, and have like 200 sprites/animations walking around, the game would sleep itself to hell.

    This is what I think, but feel free to correct me :)
    Last edited by kiregad; 06-18-2010 at 04:23 AM.
    Java beginner.

  9. #9
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    if you are using a game, your probably gonna have multiple threads.
    If one sleeps, everything else will stay reactive. The end user won't have a bad experience and sleep isn't ineffecient :p

  10. #10
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    But not all computers have the same hardware! So shouldnt we update animations based on real time?
    Java beginner.

Similar Threads

  1. Stack Implemented Linked List HELP!!!!!!!!!
    By dudedatroz in forum NetBeans
    Replies: 1
    Last Post: 02-19-2010, 10:56 PM
  2. Using Generics to execute implemented functions
    By mleather in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2009, 05:55 AM
  3. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM
  4. Class Reflection: Finding implemented interfaces
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:09 PM
  5. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 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
  •