visual c++ programming: concepts and projects chapter 6b methods (tutorial)
TRANSCRIPT
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS
Chapter 6BMethods (Tutorial)
Tutorial: Planetary Motion
Visual C++ Programming
2
Problem Analysis Create a program with a planet that orbits
around a central sun Planets orbit in an elliptical path The program will draw the planet at a new
location at regular intervals To calculate the next position of the planet you
need to know the x coordinate of the center of the ellipse The length of the semimajor axis of the ellipse The angle of the planet from the semimajor axis
Visual C++ Programming
3
Visual C++ Programming
4
Design
Visual C++ Programming
5
Interface sketch Controls
PictureBox to draw on Timer control to execute commands at regular
intervals Constants
Center of the ellipse Length of the semimajor and semiminor axes
Instance variables Graphics and Drawing objects
Visual C++ Programming
6
Visual C++ Programming
7
Visual C++ Programming
8
Visual C++ Programming
9
Visual C++ Programming
10
Design (continued)
Visual C++ Programming
11
Algorithm Algorithm for Timer1_Tick()
Refresh the PictureBox Draw the sun Draw the planet Increase the angle
Visual C++ Programming
12
Visual C++ Programming
13
Development
Visual C++ Programming
14
Create the interface Assign an image file to the PictureBox to
appear in the background Important: to make the image appear
Download the image Place it in your Project folder (within your
Solution folder of the same name) Project6 (Solution folder)
Project6 (Project folder) Use the Properties window to assign the image
to the PictureBox
Visual C++ Programming
15
Visual C++ Programming
16
Visual C++ Programming
17
Visual C++ Programming
18
Visual C++ Programming
19
Visual C++ Programming
20
Development (continued)
Visual C++ Programming
21
Coding Declare and assign static constants Declare handles for Graphics object and
Drawing objects Form1_Load()
Construct graphics and drawing objects btnStart_Click()
Start the Timer control btnStop_Click()
Stop the Timer control
Visual C++ Programming
22
Visual C++ Programming
23
Visual C++ Programming
24
Visual C++ Programming
25
Development (continued)
Visual C++ Programming
26
Coding Timer1_Tick() Declare local variables Refresh the pictureBox Draw the sun Draw the planet
Convert angle from degrees to radians Calculate planet x and y coordinates Draw the planet
Increase angle by 5 degrees
Visual C++ Programming
27
Visual C++ Programming
28
On Your Own
Visual C++ Programming
29
Instance methods with return values (Planets version 2) ConvertToRadians()
Instance methods with value parameters (Planets version 3) DrawPlanets()
Instance methods with reference parameters (Planets version 4) SetXY()