swing - Java Animation: Rotating Image -
i have simple animation task java. need create basic "wheel of fortune applet". displayed wheel , button. when button pressed want select random # of degrees (say in range of 720-3600) , spin wheel many degrees. use logic convert degree number money value. problem in animation, how image spin @ constant pace x number of degrees? there swing function that? appreciated, don't need know else java animation right besides that.
i'm going assume understand how rotate image once. if don't, can find quick google search.
what need background process rotates you. works this:
/** * warning - class unsynchronized! */ public class rotatableimage { image image; float currentdegrees; public rotateableimage(image image) { this.image = image; this.currentdegrees = 0.0f; this.remainingdegrees = 0.0f; } public void painton(graphics g) { //put code rotate image once in here, using current degrees rotation } public void spin(float additionaldegrees) { setspin(currentdegrees + additionaldegrees); } public void setspin(float newdegrees) { currentdegrees += additionaldegrees; while(currentdegrees < 0f) currentdegrees += 360f; while(currentdegrees >= 360f) currentdegrees -= 360f; } } public class imagespinner implements runnable { rotateableimage image; final float totaldegrees; float degrees; float speed; // in degrees per second public imagespinner(rotatableimage image, float degrees, float speed) { this.image = image; this.degrees = degrees; this.totaldegrees = degrees; this.speed = speed; } public void run() { // assume 40 frames per second, , doesn't matter if isn't exact int fps = 40; while(math.abs(degrees) > math.abs(speed / fps)) { // how close degrees 0? float degreestorotate = speed / fps; image.spin(degreestorotate); degrees -= degreestorotate; /* sleep wait @ least 1000 / fps before recalcing have no guarantee won't take forever! if absolutely require better timing, isn't solution */ try { thread.sleep(1000 / fps); } catch(interruptedexception e) { /* swallow */ } } image.setspin(totaldegrees); // might need 360 - totaldegrees, not sure } }
Comments
Post a Comment