Results 1 to 17 of 17
  1. #1
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Make an animation smoother

    here's a little animation I made, it's a simple sprite moving in the screen by following parameters as acceleration, velocity and physics bla bla bla...but I can get only like a frame each 70 milliseconds, or 14 fps...how can I improve this?
    Java Code:
    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JSlider;
    	
    public class Princ extends JFrame {
    
    	long bounceTime =0;
    	long startingTime = System.currentTimeMillis();
    	long cumTime = startingTime;
    	long totTime = 0;
    	long timePassed;
    	private Graphics2D g;
    
    		public static void main(String[] args){
    			DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    			Princ p = new Princ();
    			p.run();
    		
    		}
    		
    		private Screen s;
    		private Image bg;
    		private Animation a;
    		private Sprite sprite;
    		private static final DisplayMode modes1[] = {
    			new DisplayMode(800,600,32,0),
    			new DisplayMode(800,600,24,0),
    			new DisplayMode(800,600,16,0),
    			new DisplayMode(640,480,32,0),
    			new DisplayMode(640,480,24,0),
    			new DisplayMode(640,480,16,0)
    		};
    		
    		public void loadImages(){
    			bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    			Image face1 = new ImageIcon(getClass().getResource("scena1.png")).getImage();
    			Image face2 = new ImageIcon(getClass().getResource("scena2.png")).getImage();
    			
    			a= new Animation();
    			
    		a.addScene(face1, 45);
    		a.addScene(face2, 45);
    			
    		sprite = new Sprite(a);
    		sprite.setVelocityX(0.6f);
    		sprite.setVelocityY(0f);
    		sprite.setAccelerationX(0f);
    		sprite.setAccelerationY(0.000001f);
    		}
    		
    		public void run(){
    			s = new Screen();
    			try{
    				DisplayMode dm = s.findFirstCompatibleMode(modes1);
    				s.setFullScreen(dm);
    				loadImages();
    				movieLoop();
    			}finally{
    				s.restoreScreen();
    			}
    				
    		}
    		
    		public void movieLoop(){
    			
    			while(1==1){
    				timePassed = System.currentTimeMillis() - cumTime;
    				cumTime += timePassed;
    				totTime +=timePassed;	
    				g = s.getGraphics();
    				draw(g);
    				update(timePassed, totTime);
    				g.dispose();
    				s.update();
    				System.out.println(timePassed);
    			
    				
    			}
    			
    		}
    		
    		public void draw(Graphics2D g){
    		g.drawImage(bg, 0,0,null);	
    		g.drawImage(sprite.getImage(), Math.round(sprite.getX()), Math.round(sprite.getY()), null);	
    		}
    		
    		public void update(long timePassed, long totTime){
    	
    			
    		
    			
    			if(sprite.getX()<=0){
    				sprite.setVelocityX(Math.abs(sprite.getVelocityX()));
    			}else if(sprite.getX() + sprite.getWidth()>= s.getWidth()){
    				sprite.setVelocityX(-Math.abs(sprite.getVelocityX()));
    			}
    			
    			if(sprite.getY()<=0){
    				sprite.setVelocityY(Math.abs(sprite.getVelocityY()));
    				
    			}else if(sprite.getY() + sprite.getHeight()>= s.getHeight()){
    				
    				
    				if(sprite.getVelocityY()*1000<5.75){
    			
    				
    						sprite.setAccelerationY(0);
    						sprite.setVelocityY(0);
    						sprite.setAccelerationX(-0.0000001f);
    						
    					
    				}else{
    					
    				sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
    				
    				
    				}
    			}
    		
    		
    			sprite.update(timePassed, totTime);
    			
    		}
    }
    there are two more classes but I don't think they're useful to make the animation smoother...otherwise tell me :D
    Last edited by Chri; 12-12-2012 at 11:50 PM.

  2. #2
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    bump...any tips to make an animation faster in general?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Make an animation smoother

    Quote Originally Posted by Chri View Post
    bump...any tips to make an animation faster in general?
    Yes...don't call getGraphics unless you truly know what you are doing. Override paintComponent and use the Graphics object passed to this method to do you drawing. If you wish to repaint, call the method by that name. If you wish to animate, use a SwingTimer. See
    Trail: 2D Graphics (The Java™ Tutorials)
    Solving Common Painting Problems (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting)

  4. #4
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    the method getGraphics is defined in another of my classes, here it is
    Java Code:
    public Graphics2D getGraphics(){
    	Window w = vc.getFullScreenWindow();
    	
    	if(w!=null){
    		BufferStrategy s = w.getBufferStrategy();
    		return (Graphics2D)s.getDrawGraphics();
    	}else{
    		return null;
    	}
    }
    I used this to use bufferStrategy, which I though could improve the animation...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default Re: Make an animation smoother

    All Swing components are double buffered by default. Using BufferStrategy is an old AWT trick and I wouldn't expect to see any significant performance boost with Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    could you please make an example of how to do it without bufferstrategy?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default Re: Make an animation smoother

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    please i just can't figure it out, any time I try any way to not use that stupid getGraphics method it just give me exceptions in pretty much every method -.-

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default Re: Make an animation smoother

    Too much trouble to go through the tutorial, is it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    hey, I've gone through it, but there I cant find the solution for my problem, I'm not looking for ready easy code, this isn't a homework or something and if you don't want to help me don't even bother answering

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default Re: Make an animation smoother

    The tutorial is very clear. Did you find any code sample in that tutorial that uses getGraphics() of a Component?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Make an animation smoother

    Is this issue just smooth animation? If so, decrease the wait time between frames. (70 millisec to 50) That would speed up the animation, making it look smoother. If the symbols, objects, characters etc move to fast, change some of the physics variables. Also, follow the tutorials that everyone else has talked about. Do not worry about doubleBuffering if you are (and you should be) using swing, just call repaint (unless you are creating something really complex and customized).

    If you are looking for performance, then that is another topic. Putting them together can be tricky at times and finding that optimal point can be easy to look past.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  13. #13
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    Quote Originally Posted by kammce View Post
    Is this issue just smooth animation? If so, decrease the wait time between frames. (70 millisec to 50)
    this is the point, the movieloop takes 70 millisec to execute itself, I don't get how could I decrease that time to 50 :S

  14. #14
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Make an animation smoother

    Ok, so what you are telling me is that you have no idea how the algorithm you have implemented works. Could you include the code for this function sprite.update(timePassed, totTime);. I have no idea what is going on when you call it.

    Lastly, why not remove that entirely and try using Thread.sleep(time in millisecond). See how that works. Using Thread.sleep() is not the best way of going about this, but you will have better control over the amount of time it takes before you call repaint.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  15. #15
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    sprite.update() defines the physics
    Java Code:
    public void update(long timePassed, long totTime){
    		
    		if(ax!=0){
    			
    	vx+=ax*totTime;
    	x+=vx*totTime; 
    	}
    	else{
    		x+=vx*timePassed; 
    
    		
    	}
    		
    	if(ay!=0){
    		vy+=ay*totTime;
    		y+=vy*totTime; 
    
    		}
    		else{
    			y+=vy*timePassed; 
    
    		
    		}
    using sleep will do nothing but increase the time between the frames, doing the opposite of what I want to do, I know my code and I didn't set a time for the animation, all the time variables are for calculating velocity by time and acceleration by velocity.

  16. #16
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Make an animation smoother

    What is inside of the Animation class?

    Sorry, I cannot figure out what is lagging your animation.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  17. #17
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Make an animation smoother

    originally that sprite was supposed to be two images (two balls of different colors) alternating fast, to create some kind of "static animation" but I disabled it cause it obviously slowed down the moving animation more, so I modified the method getImage, that before returned a different image each 250 ms, now it return a single image

Similar Threads

  1. Replies: 4
    Last Post: 07-23-2011, 09:08 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 06:52 PM
  3. Make animation pause
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 06-08-2010, 06:16 PM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 12:41 PM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04: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
  •