Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: lagging animation

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default lagging animation

    Hi everyone

    I have this animation, that consists in a ball moving around the screen following physical parameters as acceleration etc... everything works fine, but I only get a very bad frame rate, a frame each 70 millisecond, or 14 fps, how can I make it smoother? :S
    here's the main class

    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
     
     
    public class mainclass {
     
    	long startingTime = System.currentTimeMillis();
    	long cumTime = startingTime;
    	long totTime = 0;
    	long timePassed;
    	private Graphics2D g;
    	private Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();
    	private Image bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    	int i =0;
    	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)};
     
    	Physics ph = new Physics();
     
     
     
     
    	public static void main(String[] args) throws InterruptedException{
     
    	DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    	mainclass p = new mainclass();
    	p.run();
     
    		}
     
     
    	public void setPhysics(){
    		ph.setVelocityX(0f);
    		ph.setVelocityY(0f);
    		ph.setAccelerationX(0f);
    		ph.setAccelerationY(0.0000045f);
     
    		}
     
    	public void movieLoop(){
    		Screen s = new Screen();
    		while(1==1){
    			timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			totTime +=timePassed;
    			g = s.getGraphics();
    			update(timePassed, totTime);
    			draw(g);
    			g.dispose();
    			s.update();
    			}
    		}
     
    	public void run() throws InterruptedException{
    		Screen s = new Screen();
    		try{
    			DisplayMode dm = s.findFirstCompatibleMode(modes1);
    			s.setFullScreen(dm);
    			setPhysics();
    			movieLoop();
     
    		}finally{
    			s.restoreScreen();
    			}
     
    		}	
     
     
    	public void draw(Graphics2D g){
    		g.drawImage(bg, 0,0, null);
    		g.drawImage(ball, Math.round(ph.getX()), Math.round(ph.getY()), null);
    		}
     
    	public void update(long timePassed, long totTime){
    		Screen s = new Screen();
     
    		if(ph.getX()<=0){
    			ph.setVelocityX(Math.abs(ph.getVelocityX()));
    		}else if(ph.getX() + ph.getWidth()>= s.getWidth()){
    			ph.setVelocityX(-Math.abs(ph.getVelocityX()));
    			}
     
    		if(ph.getY()<=0){
    			ph.setVelocityY(Math.abs(ph.getVelocityY()));
     
    		}else if(ph.getY() + ph.getHeight()>= s.getHeight()){
    			System.out.println(ph.getVelocityY()*1000);
     
    			if(ph.getVelocityY()*1000 == 0){
     
    				ph.setAccelerationY(0);
    				ph.setVelocityY(0);
    				ph.setVelocityX(0);
    				ph.setAccelerationX(0f);
     
    				}
    			else{
     
    			ph.setVelocityY(-Math.abs(ph.getVelocityY()));
    				}
    			}
     
    			ph.update(timePassed, totTime);
    		}
    }

    The screen class

    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Window;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
     
    public class Screen {
     
     
    	private GraphicsDevice vc;
    	private Window w;
    	mainclass p = new mainclass();
     
     
    	public Screen(){
    	GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	vc = env.getDefaultScreenDevice();
    	}
     
    	public DisplayMode[] getCompatibleDisplayMode(){
    		return vc.getDisplayModes();
    		}
     
    	public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    		DisplayMode goodModes[] = vc.getDisplayModes();
    		for (int x =0; x<modes.length;x++){
    			for(int y = 0; y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];			
    						}
    				}	
    			}
    		return null;
    		}	
     
    	public DisplayMode getCurrentDisplayMode(){
    		return vc.getDisplayMode();
    		}
     
     
    	public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
    			return false;
    		}
     
    		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() !=  DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		}
     
    		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
    			return false;
    		}
    		return true;
    	}
     
    	public void setFullScreen(DisplayMode dm){
    		JFrame f = new JFrame();
    		f.setUndecorated(true);
    		f.setResizable(false);
    		f.setIgnoreRepaint(true);
    		f.setVisible(true);
    		vc.setFullScreenWindow(f);
     
    		if(dm != null && vc.isDisplayChangeSupported()){
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception e){}
    		}
     
    		f.createBufferStrategy(2);
    	}
     
     
    	public Graphics2D getGraphics(){
    		w = vc.getFullScreenWindow();
     
    		if(w!=null){
     
     
    			BufferStrategy s = w.getBufferStrategy();
    			return (Graphics2D)s.getDrawGraphics();
    		}else{
    			return null;
    		}
    	}
     
    	public void update(){
    		w = vc.getFullScreenWindow();
    		if(w!=null){
    			BufferStrategy s = w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
     
     
    	public Window getFullScreenWindow(){
    		return vc.getFullScreenWindow();
    	}
     
    	public int getWidth(){
    		 w = vc.getFullScreenWindow();
    		if(w != null){
    		return w.getWidth();
    		}else{
    			return 0;
    		}
     
    	}
    	public int getHeight(){
    		 w = vc.getFullScreenWindow();
    		if(w!=null){
    			return w.getHeight();
    		}else{
    			return 0;
    		}
     
    	}
     
    	public void restoreScreen(){
    		 w = vc.getFullScreenWindow();
    		if(w!=null){
    			w.dispose();
     
    		}
    	vc.setFullScreenWindow(null);
    	}
     
    	public BufferedImage createCompatibleImage(int w, int h, int t){
    		Window win = vc.getFullScreenWindow();
    		if(win!=null){
    			GraphicsConfiguration gc = win.getGraphicsConfiguration();
    			return gc.createCompatibleImage(w,h,t);
    		}
    		return null;
    	}
     
    }

    and the Physics class

    import java.awt.Image;
     
    import javax.swing.ImageIcon;
     
     
    public class Physics {
     
    	private Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();
    	private Image bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    	private float x, y, vx, vy, ax, ay;
     
    	public void Physics(){
     
     
    	}
    	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; 
     
     
    		}	
     
    	}
     
    	public float getX(){
    		return x;
    	}
     
    	public float getY(){
    		return y;
    	}
     
    	public void setX(float x){
    		this.x=x;
    	}
     
    	public void setY(float y){
    		this.y=y;
    	}
     
    	public int getWidth(){
    		return ball.getWidth(null);
    	}
    	public int getHeight(){
    		return ball.getHeight(null);
    	}
     
    	public float getVelocityX(){
    		return vx;
    	}
    	public float getVelocityY(){
    		return vy;
    	}
     
    	public void setVelocityX(float vx){
    		this.vx = vx;	
    	}
     
    	public void setVelocityY(float vy){
    		this.vy = vy;
    	}
     
    	public float getAccelerationX(){
    		return ax;
    	}
     
    	public float getAccelerationY(){
    		return ay;
    	}
     
    	public void setAccelerationX(float ax){
    		this.ax=ax;
    	}
     
    	public void setAccelerationY(float ay){
    		this.ay=ay;
    	}
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    One comment so far: The formatting of the code is poor. The {}s aren't in the right columns.

    Next comment: There are too many new instances of classes created instead of passing a reference to an existing instance. Also there are new objects created that are never used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    Quote Originally Posted by Norm View Post
    One comment so far: The formatting of the code is poor. The {}s aren't in the right columns.
    sorry, is it better now?

    Quote Originally Posted by Norm View Post
    Next comment: There are too many new instances of classes created instead of passing a reference to an existing instance.
    I don't understand o_O could you explain it better please?
    Quote Originally Posted by Norm View Post
    Also there are new objects created that are never used.
    I removed them

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: lagging animation

    For example, look at how many times you call new mainclass() (which actually should be named MainClass). You should be creating the mainclass once and only once. If you need a reference to it, then you should pass a reference to the object that needs it via a method or constructor parameter.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    Quote Originally Posted by curmudgeon View Post
    For example, look at how many times you call new mainclass() (which actually should be named MainClass). You should be creating the mainclass once and only once. If you need a reference to it, then you should pass a reference to the object that needs it via a method or constructor parameter.
    I removed the call of mainclass() from screen cause it was useless, I have no clue of why I created it , but I don't get it, if I need a method from mainclass how can I not create a new object of it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    Look at all the places where "= new" is coded. If you see more than one for any class, think about removing all but one of them and using that one in all the places where new ones are created.

    Why are the images loaded in two places? Load in one and pass its reference to the other place.

    very bad frame rate, a frame each 70 millisecond, or 14 fps
    How do you compute the frame rate for the posted code?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    Quote Originally Posted by Norm View Post
    Look at all the places where "= new" is coded. If you see more than one for any class, think about removing all but one of them and using that one in all the places where new ones are created.

    Why are the images loaded in two places? Load in one and pass its reference to the other place.


    How do you compute the frame rate for the posted code?
    ok I tried what you said, now I have no objects in the screen class, I load the images only in the Physics class and then I get them through a method but I still have 3 objects in mainclass and I don't know how to get rid of them
    is the effective number of objects that lags the animation or the distribution? cause I could have an object in physics and two in mainclass...
    anyway I didn't notice any difference in the frame rate...

    I get the frame rate by System.out.println(1000/timePassed), timePassed is the time that movieLoop takes to execute itself, and I do 1000/timePassed to have the frames per second...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    Can you post some of the output for when the code computed the frame rate to show its values?

    The values I get for timePassed when counted and saved in an array:
     //[5, 0, 0, 0, 3, 169, 154, 48, 80, 98, 27, 9, 9, 8, 3, 2, 1, 4, 1, 1, 0, 0, 3, 0, 0, 0, 2, 0, 1, 1]
     // 0  1  2  3  4   5    6    7  8    9  10

    Most of the timePassed values were: 5 and 6 ms. A few less were: 7,8,9 ms
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    Fr rate	FPS
    449	2
    118	8
    79	12
    65	15
    68	14
    65	15
    66	15
    61	16
    67	14
    68	14
    67	14
    65	15
    67	14
    68	14
    67	14
    66	15
    66	15
    68	14
    74	13
    65	15
    62	16
    66	15
    67	14
    67	14
    67	14
    66	15
    67	14
    67	14
    68	14
    69	14
    62	16
    69	14
    68	14
    67	14
    63	15
    69	14
    68	14
    66	15
    67	14
    67	14

    here it is

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    Interesting that your timePassed values are 10 times mine. See post#8
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    O_O does that mean that the problem isn't my code but my computer or something? I play 3d games finely on my computer I don't think an animation like this could create so many problems xD

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    Can you post the current code?

    Have you removed all the extra object creating?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    main
    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
     
     
    public class mainclass {
     
    	long startingTime = System.currentTimeMillis();
    	long cumTime = startingTime;
    	long totTime = 0;
    	long timePassed;
    	int i=0;
    	private Graphics2D g;
    	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)};
     
    	Physics ph = new Physics();
    	Screen s = new Screen();
     
     
     
    	public static void main(String[] args) throws InterruptedException{
    	mainclass p = new mainclass();
    	p.run();
     
    		}
     
     
    	public void setPhysics(){
    		ph.setVelocityX(1f);
    		ph.setVelocityY(0f);
    		ph.setAccelerationX(0f);
    		ph.setAccelerationY(0.000005f);
     
    		}
     
    	public void movieLoop(){
    		while(totTime < 10000){
    			timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			totTime +=timePassed;
    			g = s.getGraphics();
    			update(timePassed, totTime);
    			draw(g);
    			g.dispose();
    			s.update();
     
     
    			System.out.println(timePassed+"\t"+ 1000/timePassed);
    			i++;
    			}
    		}
     
    	public void run() throws InterruptedException{
     
    		try{
    			DisplayMode dm = s.findFirstCompatibleMode(modes1);
    			s.setFullScreen(dm);
    			setPhysics();
    			System.out.println("Fr rate\tFPS");
    			movieLoop();
     
     
    		}finally{
    			s.restoreScreen();
    			}
     
    		}	
     
     
    	public void draw(Graphics2D g){
    		g.drawImage(ph.getBg(), 0,0, null);
    		g.drawImage(ph.getBall(), Math.round(ph.getX()), Math.round(ph.getY()), null);
    		}
     
    	public void update(long timePassed, long totTime){
    		if(ph.getX()<=0){
    			ph.setVelocityX(Math.abs(ph.getVelocityX()));
    		}else if(ph.getX() + ph.getWidth()>= s.getWidth()){
    			ph.setVelocityX(-Math.abs(ph.getVelocityX()));
    			}
     
    		if(ph.getY()<=0){
    			ph.setVelocityY(Math.abs(ph.getVelocityY()));
     
    		}else if(ph.getY() + ph.getHeight()>= s.getHeight()){
     
    			if(ph.getVelocityY()*1000< 5.75){
     
    				ph.setAccelerationY(0);
    				ph.setVelocityY(0);
    				ph.setVelocityX(0);
    				ph.setAccelerationX(0f);
     
    				}
    			else{
     
    			ph.setVelocityY(-Math.abs(ph.getVelocityY()));
    				}
    			}
     
    			ph.update(timePassed, totTime);
    		}
    }

    screen

    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Window;
    import java.awt.image.BufferStrategy;
     
    import javax.swing.JFrame;
     
    public class Screen {
     
     
    	private GraphicsDevice vc;
    	private Window w;
     
     
    	public Screen(){
    	GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	vc = env.getDefaultScreenDevice();
    	}
     
    	public DisplayMode[] getCompatibleDisplayMode(){
    		return vc.getDisplayModes();
    		}
     
    	public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    		DisplayMode goodModes[] = vc.getDisplayModes();
    		for (int x =0; x<modes.length;x++){
    			for(int y = 0; y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];			
    						}
    				}	
    			}
    		return null;
    		}	
     
    	public DisplayMode getCurrentDisplayMode(){
    		return vc.getDisplayMode();
    		}
     
     
    	public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
    			return false;
    		}
     
    		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() !=  DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		}
     
    		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
    			return false;
    		}
    		return true;
    	}
     
    	public void setFullScreen(DisplayMode dm){
    		JFrame f = new JFrame();
    		f.setUndecorated(true);
    		f.setResizable(false);
    		f.setIgnoreRepaint(true);
    		f.setVisible(true);
    		vc.setFullScreenWindow(f);
     
    		if(dm != null && vc.isDisplayChangeSupported()){
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception e){}
    		}
     
    		f.createBufferStrategy(2);
    	}
     
     
    	public Graphics2D getGraphics(){
    		w = vc.getFullScreenWindow();
     
    		if(w!=null){
     
     
    			BufferStrategy s = w.getBufferStrategy();
    			return (Graphics2D)s.getDrawGraphics();
    		}else{
    			return null;
    		}
    	}
     
    	public void update(){
    		w = vc.getFullScreenWindow();
    		if(w!=null){
    			BufferStrategy s = w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
     
     
    	public Window getFullScreenWindow(){
    		return vc.getFullScreenWindow();
    	}
     
    	public int getWidth(){
    		 w = vc.getFullScreenWindow();
    		if(w != null){
    		return w.getWidth();
    		}else{
    			return 0;
    		}
     
    	}
    	public int getHeight(){
    		 w = vc.getFullScreenWindow();
    		if(w!=null){
    			return w.getHeight();
    		}else{
    			return 0;
    		}
     
    	}
     
    	public void restoreScreen(){
     
    		if(w!=null){
    			w.dispose();
     
    		}
    	vc.setFullScreenWindow(null);
    	}
     
     
     
    }
    physics
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
     
     
    public class Physics {
    	private Image bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    	private Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();
    	private float x, y, vx, vy, ax, ay;
     
     
    	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; 
     
     
    		}	
     
    	}
     
    	public float getX(){
    		return x;
    	}
     
    	public float getY(){
    		return y;
    	}
     
    	public void setX(float x){
    		this.x=x;
    	}
     
    	public void setY(float y){
    		this.y=y;
    	}
     
    	public int getWidth(){
    		return ball.getWidth(null);
    	}
    	public int getHeight(){
    		return ball.getHeight(null);
    	}
     
    	public float getVelocityX(){
    		return vx;
    	}
    	public float getVelocityY(){
    		return vy;
    	}
     
    	public void setVelocityX(float vx){
    		this.vx = vx;	
    	}
     
    	public void setVelocityY(float vy){
    		this.vy = vy;
    	}
     
    	public float getAccelerationX(){
    		return ax;
    	}
     
    	public float getAccelerationY(){
    		return ay;
    	}
     
    	public void setAccelerationX(float ax){
    		this.ax=ax;
    	}
     
    	public void setAccelerationY(float ay){
    		this.ay=ay;
    	}
    	public Image getBall(){
    		return ball;
    	}
    	public Image getBg(){
    		return bg;
    	}
     
     
     
     
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    The ball's motion has changed to a straight down on the left side to off the screen movement. It used to start in upper left, move to the right and down and bounce around some before moving to the left at the bottom of the screen and off the screen.
    The timePassed counts:
    [3, 0, 0, 0, 2, 21, 12, 5, 9, 4, 2, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]

    5,6 ms still highest count with 7,8,9 next
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    yeah I changed some physics parameter and that screwed the calculation of when the ball should stop, which wasn't already precise, to make it move to the right change the parameters in setPhysics for example setVelocityX(1f);
    and I changed line 92 of mainclass to do some test, it should be
    if(ph.getVelocityY()*1000< 5.75){

    still not get why on your computer it works well and on mine not...

    Update: I updated the mainclass code in post #13, you can take it from there if you want it...

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    The new code draws at these coordinates which are off my screen:
    draw ball @ x=2068.0 y=21.38312
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    for me it works...maybe it's cause of the background that is 2560*1440 (my screen's resolution) but the question is why I get this bad frame rate!

    edit: how did you get the images?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    Code should not depend on a fixed screen size to work. It should use the existing screen size to set the drawing bounds.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    maybe the velocity is too high and if you have a smaller screen than the mine it get immediately off the screen...

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    This will fix the problem:
        public void movieLoop(){
       	long cumTime = System.currentTimeMillis()+1;    //<<<<<<<< THIS keeps x in bounds
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    should I add
    long cumTime = System.currentTimeMillis()+1;
    after the start of movieloop?
    this screw up the entire time thing

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    The code works when I use what I posted in post#20. It keeps x inbounds.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    but what for the frame rate? The entire thing won't work at 15 fps cause the ball won't detect the edge of the screen correctly, do you have any clue of what is lagging the animation on my computer?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: lagging animation

    I have no idea why the timing is different on your PC.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: lagging animation

    oh dear...should I try to reinstall java or something?

Page 1 of 2 12 LastLast

Similar Threads

  1. Animation in JApplet
    By Aurin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 10:18 AM
  2. My program keeps lagging when dealing with large arrays.
    By chrynelson in forum Java Theory & Questions
    Replies: 4
    Last Post: October 21st, 2011, 04:57 PM
  3. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  4. Project with animation.
    By ryainad in forum Paid Java Projects
    Replies: 3
    Last Post: April 10th, 2011, 02:59 PM
  5. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM