Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan.

Similar presentations


Presentation on theme: "C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan."— Presentation transcript:

1

2 C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan

3 2 P REREQUISITE Loops Advanced

4 3 S HUTTLE A NIMATION

5 4 B ASIC A RCHITECTURE APlotted Shuttle APlotted Shuttle AShuttleAnimator main setShuttleX(Y)() Main Class animateFromOrigin()

6 5 A NIMATE F ROM O RIGIN S EMANTICS Basic Idea: Should animate from origin to current location Should move to origin Should animate first in Y direction to current Y Next should animate in X direction to current X

7 6 public void animateFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime) { int originalX = shuttle.getShuttleX(); int originalY = shuttle.getShuttleY(); int curX = 0; int curY = 0; shuttle.setShuttleX(curX); shuttle.setShuttleY(curY); animateYFromOrigin(shuttle, animationStep, animationPauseTime, curY, originalY); animateXFromOrigin(shuttle, animationStep, animationPauseTime, curX, originalX); } A NIMATE FROM O RIGIN

8 7 A NIMATING TO A D ESTINATION L OCATION 1. Should move some distance (< total_distance) in straight line to destination 2. Go back to 1 if location not reached

9 8 A NIMATING TO A D ESTINATION L OCATION 1. Should move a constant distance (< total_distance) in straight line to destination 2. Should pause for some time3. Go back to 1 if location not reached

10 9 P AUSING P ROGRAM * ASSIGNMENT_TIME different on different computers! Unnecessarily using the CPU (Busy Waiting) Need the OS to suspend program for pause time void sleep ( int pauseTime) { int numberOfAssignments = pauseTime; //ASSIGNMENT_TIME; for ( int i = 0; i < numberOfAssignments; i++) { int dummy = 0; // nonsense assignment }

11 10 P AUSING P ROGRAM : T HREAD S UPPORT.S LEEP () public void sleep( int pauseTime) { try { // OS suspends program for pauseTime Thread.sleep(pauseTime); } catch (Exception e) { // program may be forcibly interrupted while sleeping e.printStackTrace(); }; }

12 11 A NIMATION IN Y D IRECTION protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); } // move to destination Y position shuttle.setShuttleY(endY); }

13 12 A NIMATION IN X D IRECTION protected void animateXFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startX, int endX) { while (startX < endX) { ThreadSupport.sleep(animationPauseTime); startX += animationStep; shuttle.setShuttleX(startX); } shuttle.setShuttleX(endX); }

14 13 A NIMATE F ROM O RIGIN public void animateFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime) { int originalX = shuttle.getShuttleX(); int originalY = shuttle.getShuttleY(); int curX = 0; int curY = 0; shuttle.setShuttleX(curX); shuttle.setShuttleY(curY); animateYFromOrigin(shuttle, animationStep, animationPauseTime, curY, originalY); animateXFromOrigin(shuttle, animationStep, animationPauseTime, curX, originalX); }

15 14 M AIN M ETHOD public static void main(String[] args) { PlottedShuttle shuttle = new APlottedShuttle(50, 100); OEFrame oeFrame = ObjectEditor.edit(shuttle); oeFrame.hideMainPanel(); oeFrame.setSize (450, 450); ShuttleAnimator shuttleAnimator = new AShuttleAnimator(); shuttleAnimator.animateFromOrigin(aShuttle, 5, 100); } No animation, nothing happens on the screen Using non observable version of plotted shuttle

16 15 protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); } // move to destination Y position shuttle.setShuttleY(endY); } N O R EFRESH Shuttle being displayed by some GUI code (e.g. ObjectEditor) GUI needs to be told when shuttle coordinates change Even if the method was called by it and it does automatic refresh Automatic refresh done when the method returns, not at indeterminate intermediate points

17 16 protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY, OEFrame frame) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); frame.refresh(); } // move to destination Y position shuttle.setShuttleY(endY); } M ANUAL F ULL R EFRESH

18 17 M ANUAL F ULL R EFRESH public class ShuttleAnimationDriver { public static void demoShuttleAnimation( ShuttleAnimator aShuttleAnimator, PlottedShuttle aShuttle, OEFrame anOEFrame) { aShuttleAnimator.animateFromOrigin(aShuttle, 5, 100); aShuttleAnimator.animateFromOrigin(aShuttle, 5, 100, anOEFrame); } public static void main(String[] args) { PlottedShuttle shuttle = new APlottedShuttle(50, 100); OEFrame oeFrame = ObjectEditor.edit(shuttle); oeFrame.hideMainPanel(); oeFrame.setSize (450, 450); ShuttleAnimator shuttleAnimator = new AShuttleAnimator(); demoShuttleAnimation(shuttleAnimator, shuttle, oeFrame); } Both animateFromOrigins take the same amount of time The refreshing one changes the display, the other one does not

19 18 R EFRESHING A RCHITECTURE APlotted Shuttle APlotted Shuttle AShuttleAnimator main setShuttleX(Y)() Main Class Component repaint() paint() animateFromOrigin() animateFromOrigin ObjectEditor refresh()

20 19 A NIMATION S TEPS Animation consists of one or more animation steps An animation step updates one or more animating graphical properties such as size, location, and icon of one or more graphical objects and then pauses execution

21 20 P AUSING E XECUTION Execution can be paused using busy waiting or a sleep call provided by the operating system Busy waiting has the problem that it is platform-specific and does not allow other activity to proceed while the animation is paused Therefore using the sleep call is preferable

22 21 I NCREMENTAL U PDATES After each animation step, all displays of the animation must be updated The refresh operation can be used to do ensure these updates are made.


Download ppt "C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan."

Similar presentations


Ads by Google