Teen Programmers Unite  
 

 

Return to forum top

2D animation with Java

Posted by arashfarmand [send private reply] at October 30, 2002, 10:17:44 PM

I am creating a 2D video game using Java 1.4.0: My drawing surface is a Component (ie: public class Screen extends Component) I have a tree on the screen and my character as well as the grass onwhich they are found. when I walk around, say I walk NE, I make the tree and the grass move SW (to give the impression of walking) All graphics are Sprites which is a class I created that holds: an image, x, y coordinated, width, height and a draw(Graphics g) method. Every time I move around on the screen, I move all Sprites in the opposite direction and this is invoked every 50 or so milliseconds as I am walking but When I move, the tree Sprite for example bounces or vibrates a little so the animation is not so smooth. Here is some more specs:
- I draw all Sprites to an offscreen buffer (BufferedImage) and then I draw the buffer all at once
- I have tried using the VolatileImage class but this makes most frames simply disappear
altogether even when I reconstruct the VolatileImage if its contents is lost.
- Here is the component Hierarchy: I use a JWindow, inside of which is found the Monitor class which is a Container, inside of which is found the Screen class. Should I use an Applet instead?
- I use framerate syncronization to make sure the timing is constant ie:

protected void syncronize(){
long frame_Start = 0;
long elapsed_Time = 0;

Thread t = Thread.currentThread();
while(t == thread){
frame_Start = System.currentTimeMillis();
// here is where I draw this sprite to some graphics context...bla bla
elapsed_Time = System.currentTimeMillis() - frame_Start;
try{
if(elapsed_Time < 40)
thread.sleep(40 - elapsed_Time);
}catch(InterruptedException e) {break;}
}
}

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.