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; } }