Results 1 to 5 of 5
  1. #1
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Delaying Animation

    Hi! I was just wondering how to update an animation every second instead of almost instantly. Thanks!

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

    Default Re: Delaying Animation

    You could slow down the animation loop. How you do this will depend almost completely on how you are currently animating.

  3. #3
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Delaying Animation

    heres what I have so far but the delay method isnt really working
    Java Code:
    import java.awt.*;
    import java.applet.Applet;
    
    public class Liang extends Applet {
    	public void paint( Graphics screen ) {
    		setBackground( Color.green );
    		Image hare = getImage( getDocumentBase(), "hare.gif" );
    		Image tortoise = getImage( getDocumentBase(), "tortoise.gif" );
    		screen.drawImage( hare, 5, 5, this);
    		screen.drawImage(tortoise, 5, 120, this); 
    		screen.drawLine( 0, 115, 1300, 115);
    		
    		int disfromfinh = 50;
    		int disfromfint = 50;
    
    		int xh = 5;
    
    		int xt = 5;
    
    		while ( disfromfinh > 0 & disfromfint > 0 ) {
    			// For the hare
    
    			int moveh = (int)(Math.random() * 10) + 1;
    			if (moveh == 1 | moveh == 2) {
    				delay();delay();delay();
    				screen.clearRect(xh, 5, xh + 118, 107);
    				int a1 = xh + 180;
    				screen.drawImage( hare, a1 , 5, this);
    				xh = a1;
    
    				disfromfinh -= 9;
    			}
    			else if (moveh == 3 | moveh == 4 | moveh == 5) {
    				delay();delay();delay();
    				screen.clearRect(xh, 5, xh + 118, 107);
    				int a1 = xh + 20;
    				screen.drawImage( hare, a1 , 5, this);
    				xh = a1;
    
    				disfromfinh -= 1;
    			}
    			else if (moveh == 6) {
    				delay();delay();delay();
    				screen.clearRect(xh, 5, xh + 118, 107);
    				int a1 = xh - 240;
    				screen.drawImage( hare, a1 , 5, this);
    				xh = a1;
    
    				disfromfinh += 12;
    
    				if (disfromfinh > 50) {
    					screen.clearRect(xh, 5, xh + 118, 107);
    					screen.drawImage( hare, 5 , 5, this);
    					disfromfinh = 50;
    					xh = 5;
    				}
    			}
    			else if (moveh == 7 | moveh == 8) {
    				delay();delay();delay();
    				screen.clearRect(xh, 5, xh + 118, 107);
    				int a1 = xh - 40;
    				screen.drawImage( hare, a1 , 5, this);
    				xh = a1;
    
    				disfromfinh += 2;
    
    				if (disfromfinh > 50) {
    					screen.clearRect(xh, 5, xh + 118, 107);
    					screen.drawImage( hare, 5 , 5, this);
    					disfromfinh = 50;
    					xh = 5;
    				}
    			}
    			else
    				screen.clearRect(xh, 5, xh + 118, 107);
    				delay();delay();delay();
    				disfromfinh = disfromfinh;
    
    			//For the turtle
    			int movet = (int)(Math.random() * 10) + 1;
    			if (5 >= movet & movet >= 1) {
    				screen.clearRect(xt, 120, xt + 130, 198);
    				delay();delay();delay();
    				int a2 = xt + 60;
    				screen.drawImage( tortoise, a2 , 120, this);
    				xt = a2;
    
    				disfromfint -= 3;
    			}
    			else if (movet == 6 | movet == 7 | movet == 8) {
    				screen.clearRect(xt, 120, xt + 130, 208);
    				delay();delay();delay();
    				int a2 = xt + 20;
    				screen.drawImage( tortoise, a2 , 120, this);
    				xt = a2;
    
    				disfromfint -= 1;
    			}
    			else {
    				screen.clearRect(xt, 120, xt + 130, 208);
    				delay();delay();delay();
    				int a2 = xt - 120;
    				screen.drawImage( tortoise, a2 , 120, this);
    				xt = a2;
    
    				disfromfint += 6;
    
    				if (disfromfint > 50) {
    					screen.clearRect(xt, 120, xt + 130, 208);
    					screen.drawImage( tortoise, 5 , 120, this);
    					disfromfint = 50;		
    					xt = 5;
    				}
    			}
    		}
    	}
    
    
    	public void delay() {
    		for( int i = 0; i <= 900000000; i++ );
    	}
    }

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

    Default Re: Delaying Animation

    Step back a bit and first fix your non-animation drawing code as you're doing a lot incorrectly starting with setting a background color. This is not done in the paint method itself. Next you appear to have program logic in the paint method -- another thing that you should never do. Next you seem to have your animation itself in the paint method, another thing you should never do.

    I suggest that you go through some of the painting/drawing tutorials first to learn some of the rudiments of Java painting. Next consider using Swing rather than AWT. Then do your animation with a Swing Timer. Then you could delay the animation simply by increasing the Timer's delay property setting.

  5. #5
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  3. program not delaying
    By Brain_Child in forum New To Java
    Replies: 2
    Last Post: 11-29-2009, 02:10 PM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 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
  •