visual c++ programming: concepts and projects chapter 6b methods (tutorial)

29
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Upload: egbert-phillips

Post on 17-Jan-2016

225 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 6BMethods (Tutorial)

Page 2: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (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

Page 3: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

3

Page 4: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

4

Page 5: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

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

Page 6: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

6

Page 7: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

7

Page 8: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

8

Page 9: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

9

Page 10: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

10

Page 11: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Design (continued)

Visual C++ Programming

11

Algorithm Algorithm for Timer1_Tick()

Refresh the PictureBox Draw the sun Draw the planet Increase the angle

Page 12: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

12

Page 13: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

13

Page 14: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

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

Page 15: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

15

Page 16: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

16

Page 17: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

17

Page 18: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

18

Page 19: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

19

Page 20: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

20

Page 21: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

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

Page 22: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

22

Page 23: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

23

Page 24: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

24

Page 25: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

25

Page 26: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

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

Page 27: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

27

Page 28: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

Visual C++ Programming

28

Page 29: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)

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()