Results 1 to 17 of 17
Thread: Make an animation smoother
- 12-12-2012, 10:23 PM #1
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
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?
there are two more classes but I don't think they're useful to make the animation smoother...otherwise tell me :DJava 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); } }Last edited by Chri; 12-12-2012 at 10:50 PM.
- 12-13-2012, 08:01 PM #2
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Make an animation smoother
bump...any tips to make an animation faster in general?
- 12-13-2012, 08:16 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Make an animation smoother
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)
- 12-13-2012, 08:42 PM #4
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Make an animation smoother
the method getGraphics is defined in another of my classes, here it is
I used this to use bufferStrategy, which I though could improve the animation...Java Code:public Graphics2D getGraphics(){ Window w = vc.getFullScreenWindow(); if(w!=null){ BufferStrategy s = w.getBufferStrategy(); return (Graphics2D)s.getDrawGraphics(); }else{ return null; } }
- 12-13-2012, 09:07 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-14-2012, 07:21 PM #6
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Make an animation smoother
could you please make an example of how to do it without bufferstrategy?
- 12-14-2012, 09:25 PM #7
Re: Make an animation smoother
Why make an example when there's a whole tutorial?
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-15-2012, 11:53 AM #8
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
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 -.-
- 12-15-2012, 02:13 PM #9
Re: Make an animation smoother
Too much trouble to go through the tutorial, is it?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-15-2012, 02:17 PM #10
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
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
- 12-15-2012, 04:08 PM #11
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-15-2012, 10:10 PM #12
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
- 12-15-2012, 10:33 PM #13
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
- 12-16-2012, 02:15 AM #14
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
- 12-16-2012, 11:15 AM #15
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Make an animation smoother
sprite.update() defines the physics
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.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; }
- 12-18-2012, 01:12 AM #16
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
- 12-18-2012, 10:50 AM #17
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
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
-
can i make a program to make keyboard and mouse idle or not responding for 10 second
By 3ammary in forum Advanced JavaReplies: 4Last Post: 07-23-2011, 08:08 PM -
Animation
By ryainad in forum Advanced JavaReplies: 1Last Post: 04-04-2011, 05:52 PM -
Make animation pause
By mneskovic in forum New To JavaReplies: 11Last Post: 06-08-2010, 05:16 PM -
need help about animation ?
By h9h in forum Java 2DReplies: 1Last Post: 10-30-2009, 11:41 AM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks