Results 1 to 10 of 10
- 06-17-2010, 06:30 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
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:
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 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); } }Java beginner.
- 06-17-2010, 08:04 PM #2
Although it would be a lot more sensible to use thread.sleep. How come you are not doing that?Java Code:// Not tested long CurrentTime = System.currentTimeMillis(); while (System.currentTimeMillis() < CurrentTime + 300) { // Do nothing }
- 06-17-2010, 08:32 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 06-17-2010, 09:28 PM #4
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.
- 06-17-2010, 10:27 PM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
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
- 06-17-2010, 10:44 PM #6
Heh, didnt say it was efficient. :P
-
- 06-18-2010, 04:20 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
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.
- 06-18-2010, 06:45 AM #9
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
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
- 06-18-2010, 07:39 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Stack Implemented Linked List HELP!!!!!!!!!
By dudedatroz in forum NetBeansReplies: 1Last Post: 02-19-2010, 10:56 PM -
Using Generics to execute implemented functions
By mleather in forum Advanced JavaReplies: 1Last Post: 08-07-2009, 05:55 AM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM -
Class Reflection: Finding implemented interfaces
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:09 PM -
A Map implemented with ArrayLists
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks