Download presentation
Presentation is loading. Please wait.
Published byMarylou Carter Modified over 9 years ago
1
Chapter 22 The Smooth Motion: Case Study in Algorithmic Solving
2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-2
3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-3 Planning Smooth Motion Apply the Decomposition Principle –Divide a large task into smaller subtasks that can be solved separately and then combine their solutions to produce the overall solution Principle can be applied again to each subtask, if necessary
4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-4 Planning Smooth Motion (cont'd) Carol Schwartz: Can we insert the table that appears as part of the text on page 637? Carol Schwartz: Can we insert the table that appears as part of the text on page 637?
5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-5 Planning Smooth Motion (cont'd) Decide on a Problem-Solving Strategy –Strategy on how to solve each part –Order in which we'll solve parts –Build a basic web page first Don't spend time embellishing page until other tasks are done
6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-6 Planning Smooth Motion (cont'd) Solve Independent Tasks before Dependent Tasks –Some tasks rely on other tasks –Independent tasks should be done first Tasks that depend on the independent tasks are done next, and so on PERT Chart –Task dependency graph
7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-7
8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-8 Build the Basic Web Page GUI Full GUI has table with heading, grid, keys, controls, instructions Basic structural page: –Table –Heading –Instructions –Background color, font style and color –Centering of application on page
9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-9 Build the Basic Web Page GUI (cont'd) The Structural Page –Five-row, one-column table –Text for the Smooth Motion heading and Instructions in first and last rows The Structural Page Heading – heading for heading, paragraph tag for instructions (set font color for instructions text)
10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-10
11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-11 Animate the Grid Recall three basic steps of animation: –Define and place initial image –Prefetch frames for updating image –Set timer and build timer event handler to update image
12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-12 Animate the Grid (cont'd) Frames for the Columns of Blocks –Design and organize the column frames Indexing Columns from Left to Right –On each time step, given column is replaced by column to its right –If columns are indexed left to right, index in column i of grid at a given step is replaced on next time step by image in column i + 1 –The last column needs to have a new image assigned
13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-13 Animate the Grid (cont'd)
14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-14 Animate the Grid (cont'd) Second Analysis –Define and organize the eight columnar frames –Define and fetch the initial images (0-19) –Prefetch the eight frames for uploading the image –Set a timer with an event handler that shifts the images in columns 1 through 19 to columns 0 through 18, and assign new frame into column 19
15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-15 Animate the Grid (cont'd) Subtask: Define and Organize the Frames –Guidelines for creating frame images Ensure all images overwriting one another have the same dimensions in pixels Ensure all frames are saved in.gif or.jpeg format, and that they are used consistently (only overwrite.gifs with.gifs)
16
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-16 Animate the Grid (cont'd) Subtask: Define and Place Initial Images –Use tag to place initial image on page –Use a loop to complete image initialization of 20 images
17
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-17 Animate the Grid (cont'd) Subtask: Prefetch the Frame Images –Declare the array into which the images will be fetched –Initialize the array elements to be image objects –Assign the names of the files to the src fields of the image objects, causing browser to record the names and get the files
18
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-18 Animate the Grid (cont'd) Subtask: Set Timer and Build Timer Event Handler –Event handler animate() has three operations: To move all images but the first, one position to the left To assign a new frame to column 19 To schedule itself for sometime in the future
19
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-19
20
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-20
21
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-21 The Best Laid Plans… We find it cumbersome not to be able to start and stop animation on demand Build control task is scheduled for later Makes more sense to solve it now, to simplify our work
22
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-22 Build Controls What should happen when the control is clicked? –Go button click-event Start animation with setTimeout(), keeping track of the handle –Stop button click-event End animation by clearing the timer using the handle –Radio button click-event Set the timer interval by assigning to duration Place this code in the fourth row of the structural page table
23
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-23 Sense the Keys Ability to recognize when mouse hovers over a given key We need to learn about mouse motion detection –Browser with OS keeps track of where mouse is at any given time –MouseOver and MouseClick events recognized in JavaScript
24
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-24 Sense the Keys (cont'd) Subtask: Define and Organize the Frames Subtask: Place the Initial Images –Placing the images creates the keys –Place seven images in center of third row of the structural table using a loop
25
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-25 Sense the Keys (cont'd) Subtask: Prefetch the Frames –Analogous to earlier animations –Since there are only two frames to prefetch, it's not worth writing a loop
26
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-26 Sense the Keys (cont'd) Subtask: Build the Event Handlers –here() for MouseOver –gone() for MouseOut –What should happen when mouse moves over a key Key must change color to give user feedback Must tell Grid Animation event handler which new Stack image to draw in the last position in the grid
27
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-27 Sense the Keys (cont'd) Combine the Subtasks –Add the event handler specifications to the tags
28
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-28 Staircase Detection Subtask: Recognizing the Staircase –How do we recognize the seven consecutive frame values? –Keep predicting the next frame value
29
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-29 Staircase Detection (cont'd) Subtask: Recognizing Continuity –Modifiy the animate() function at the point where it is about to set the timer for the next tick If the staircase is found, there should be no next tick
30
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-30 Assemble Overall Design Build Controls task was performed out of order Parts of Assemble Overall Design task performed ahead of time There is not much left to complete the programming Make sure the whole application runs as planned
31
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-31 Primp the Design Make structural page more attractive –Table background color –Cell padding
32
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-32 Assessment and Retrospective Three primary topics from earlier chapters used in this design: –Loops –Parameterizing functions for reuse –Managing complexity with functions
33
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-33
34
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 22-34
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.