bufferedimage - Animating Frames -
which class should use animate set of frames?i did this.it worked well.that animate picture.which class best?
public jlabel frame; public image[] frames; public bird() { frames = new image[5]; frames[0] = gettoolkit().getimage("resources\\bird_swing_up2.png"); frames[1] = gettoolkit().getimage("resources\\bird_swing_up.png"); frames[2] = gettoolkit().getimage("resources\\bird_swing_down.png"); frames[3] = gettoolkit().getimage("resources\\bird_swing_up.png"); frames[4] = gettoolkit().getimage("resources\\bird_swing_up2.png"); }
i animate in class this?is ok?is method better way do?
private image city; private image ground; private int x = 0; private timer speedcontroller; private bird bird; private timer timer; int = 0; public board() { super(); ground = gettoolkit().getimage("resources\\ground.png"); city = gettoolkit().getimage("resources\\city.png"); speedcontroller = new timer(5, new groundspeed()); speedcontroller.start(); bird = new bird(); timer = new timer(50, new fly()); timer.start(); } public void paintcomponent(graphics g){ super.paintcomponent(g); g.drawimage(city, 0, 0, 1082, 450, this); g.drawimage(ground, x, 372, this); g.drawimage(ground, x + 350, 372, this); g.drawimage(bird.frames[i], 100, 100, this); if(x == -350){ x = 0; } } public class groundspeed implements actionlistener{ @override public void actionperformed(actionevent e) { x--; repaint(); } } public class fly implements actionlistener { @override public void actionperformed(actionevent e) { //frame = new jlabel(frames[i]); i++; if(i == 4){ = 0; } repaint(); } }
hmm.. unfortunately, there no magical class in java that, meaning have write own animation mechanism.
there 2 main techniques creating animations:
- frame-based animation
- time-based animation
timee-based animation preferable! 1 of best stuff there subject available here. shares jsfiddle examples demonstrating every technique explained in post.
not long ago i translated code c++/qt:
Comments
Post a Comment