Appendix A: A Description of Keeping Track

 

Project RollingBlock

This final project demonstrates some of the rudiments of animation. Rolling Block consists of an orange-colored, square block that moves along a flat surface from right to left. The timer that controls the movement has been placed in the method that starts the block movement, so its intBlockTimer1Interval (lines RB072 and RB082) is resettable by the user . This is the main window:

image from book
Figure 27-4: Rolling Block window

The block timer is initially set at 500, which means that the timer should tick every half-second. The complete source listing for Rolling Block is shown at the end of this section.

Pictorial Layout

The flat surface that the orange block rolls upon is a panel taken from the Toolbox. It is 450 pixels long and 2 pixels high. The Toolbox panel can be used to draw vertical lines or horizontal lines on a screen only. If the line is other than vertical or horizontal, it must be painted on.

Since the timer-tick rate must be changeable , the timer must be placed in the event handler of the Start the Block Rolling button, button1_Click(). Its initial intBlockTimer1Interval is declared in line RB013. This interval is modified in lines RB072 (button3_Click()) and RB082 (button4_Click()). Each time the interval is changed, the new number is posted in label2.Text at the top left of the window.

The positions of the block are dictated by the Angle array in line RB023. Each time the method timer1_Tick() commands the block to roll (lines RB090-RB109), the block rotates 15 degrees. After every six movements (0, 15, 30, 45, 60, 75 degrees) the start position for the block is moved to the left (along the flat surface) by a distance dBlockSize (line RB014).

Line RB015 declares an integer NumberOfSidesBFOTT (Number Of Sides Before Falling Off The Track). This number is set in line RB033 (in method Form1_Load()) so the block will not attempt to roll off the left edge of the window. Since this number is computed every time the program executes (in line RB033), the programmer has the option of increasing or decreasing the size of the block (line RB014).

All the painting action is controlled with the boolean variable RollTheBlock. If RollTheBlock is true, then lines RB126-RB143 are executed. If false, lines RB147-RB150 are executed. Lines RB147-RB150 create a static orange block on the right edge of the window.

The calculations to determine the position of ACorner, BCorner, CCorner, and DCorner (lines RB126-RB139) are based on sequential computation ” that is, the ACorner is set, then the BCorner is computed based on the ACorner location. The ACorner starts off as the left corner on the flat surface. The BCorner is on the right of flat surface. The CCorner is at the top right of the block, and the DCorner is at the top left of the block.

Upgrades to RollingBlock

Since the rolling rate of the orange block is resettable, about the only item in the project that is fixed is the size of the block. You can create a button to make the block larger and a separate button to make the block smaller. Or, you can create one button named Change Block Size and ask the user (via a MessageBox control) whether the block should be made larger or smaller. The variable that must be changed is dBlockSize; it is declared originally in line RB014. This number can be changed in an event handler that is created to support a button.

RollingBlock (RB) Listing

 Form1.cs: RB002:        using System; RB003:        using System.Collections.Generic; RB004:        using System.ComponentModel; RB005:        using System.Data; RB006:        using System.Drawing; RB007:        using System.Windows.Forms; RB009:        namespace RollingBlock RB010:        { RB011:          partial class Form1 : Form RB012:          {                   // Programmer-added variables.                   // Set the rolling rate. '1000' produces one tick each second.                   // The smaller the number, the faster the rate of roll. RB013:            public int intBlockTimer1Interval = 500;                   // Block size. RB014:            public double dBlockSize = 60; // Pixels.                   // Number of block sides before falling off the track. RB015:            public int iNumberOfSidesBFOTT; RB016:            public int iBP = 0; // Block Position, '0' through '5'. RB017:            public bool RollTheBlock = false; RB018:            public double dStartXAtStartup = 417.0; RB019:            public double dStartX; RB020:            public double dStartY = 133.0; RB021:            public int iHowManySidesRolled = 0; RB022:            public double dConvert; // Pi/180. RB023:            public double[] Angle = new double[6] { 0.0, 15.0, 30.0, 45.0, 60.0, 75.0 }; RB024:            public double ACornerX, ACornerY, BCornerX, BCornerY, CCornerX,                     CCornerY, DCornerX, DCornerY; RB025:            public Form1() RB026:            { RB027:              InitializeComponent();                     // Do not place the TIMER here! You cannot change its 'BlockTimerInterval'                     // easily if you place the timer here. RB028:            } //-----------------------------------------------------------------------------------------// RB030:            private void Form1_Load(object sender, EventArgs e) RB031:            { // Determine the coordinates of the four corners of the rolling block. RB032:              dConvert = Math.PI / 180.0; RB033:              iNumberOfSidesBFOTT = (450 - (int)dBlockSize) / (int)dBlockSize; RB034:            } //-----------------------------------------------------------------------------------------// RB040:            private void button1_Click(object sender, EventArgs e) RB041:            { // Start the block rolling.                     // Timer. RB042:              label2.Text = intBlockTimer1Interval.ToString(); RB043:              Timer timer1 = new Timer(); RB044:              timer1.Tick += new EventHandler(timer1_Tick); RB045:              timer1.Interval = intBlockTimer1Interval; RB046:              timer1.Start(); RB047:              dStartX = dStartXAtStartup; RB048:              iBP = 0; RB049:              RollTheBlock = true; RB050:            } //-----------------------------------------------------------------------------------------// RB060:            private void button2_Click(object sender, EventArgs e) RB061:            { // Quit. RB062:              Close(); RB063:            } //-----------------------------------------------------------------------------------------// RB070:            private void button3_Click(object sender, EventArgs e) RB071:            { // Increase rolling rate. RB072:              intBlockTimer1Interval = intBlockTimer1Interval - 50; RB073:              label2.Text = intBlockTimer1Interval.ToString(); RB074:            } //-----------------------------------------------------------------------------------------// RB080:            private void button4_Click(object sender, EventArgs e) RB081:            { // Decrease rolling rate. RB082:              intBlockTimer1Interval = intBlockTimer1Interval + 50; RB083:              label2.Text = intBlockTimer1Interval.ToString(); RB084:            } //-----------------------------------------------------------------------------------------// RB090:            private void timer1_Tick(object sender, EventArgs e) RB091:            { // Timer to roll the block. RB092:              if (RollTheBlock) RB093:              { RB094:                iBP++; RB095:                 if (iBP > 5) RB096:                 { RB097:                   iHowManySidesRolled++; RB098:                   if (iHowManySidesRolled > iNumberOfSidesBFOTT - 1) RB099:                   { RB100:                     timer1.Stop(); RB101:                     iHowManySidesRolled = 0; RB102:                     RollTheBlock = false; RB103:                   } RB104:                   iBP = 0; RB105:                   dStartX = dStartX - dBlockSize; RB106:                 } RB107:                 Invalidate(); RB108:               } RB109              } //-----------------------------------------------------------------------------------------// RB120:             private void Form1_Paint(object sender, PaintEventArgs e) RB121:             { RB122:               Graphics grfx = e.Graphics; RB123:               Pen orangePen = new Pen(Color.Orange, 3); // Color of the 'block'. RB124:               if (RollTheBlock) RB125:               {                        // Position the rolling block. RB126:                 ACornerX = dStartX; RB127:                 ACornerY = dStartY; RB128:                 double dXB = Math.Cos(Angle[iBP] * dConvert); RB129:                 double dYB = Math.Sin(Angle[iBP] * dConvert); RB130:                 BCornerX = dStartX + dXB * dBlockSize; RB131:                 BCornerY = dStartY - dYB * dBlockSize; RB132:                 double dXC = dXB + Math.Cos((Angle[iBP] + 90.0) * dConvert); RB133:                 double dYC = dYB + Math.Sin((Angle[iBP] + 90.0) * dConvert); RB134:                 CCornerX = dStartX + dXC * dBlockSize; RB135:                 CCornerY = dStartY - dYC * dBlockSize; RB136:                 double dXD = Math.Cos((Angle[iBP] + 180.0) * dConvert); RB137:                 double dYD = Math.Sin((Angle[iBP] + 180.0) * dConvert); RB138:                 DCornerX = dStartX + (dXC + dXD) * dBlockSize; RB139:                 DCornerY = dStartY - (dYC + dYD) * dBlockSize; RB140:                 grfx.DrawLine(orangePen, (int)ACornerX, (int)ACornerY,                          (int)BCornerX, (int)BCornerY); RB141:                 grfx.DrawLine(orangePen, (int)BCornerX, (int)BCornerY,                          (int)CCornerX, (int)CCornerY); RB142:                 grfx.DrawLine(orangePen, (int)CCornerX, (int)CCornerY,                          (int)DCornerX, (int)DCornerY); RB143:                 grfx.DrawLine(orangePen, (int)DCornerX, (int)DCornerY,                          (int)ACornerX, (int)ACornerY); RB144:               } RB145:               else // Print block at start-up. RB146:             { RB147:               grfx.DrawLine(orangePen, (int)dStartXAtStartup, (int)dStartY,                        (int)(dStartXAtStartup + dBlockSize), (int)dStartY); RB148:               grfx.DrawLine(orangePen, (int)(dStartXAtStartup + dBlockSize),                        (int)dStartY, (int)(dStartXAtStartup + dBlockSize),                        (int)(dStartY - dBlockSize)); RB149:               grfx.DrawLine(orangePen, (int)(dStartXAtStartup + dBlockSize),                        (int)(dStartY - dBlockSize), (int)dStartXAtStartup,                        (int)(dStartY - dBlockSize)); RB150:               grfx.DrawLine(orangePen, (int)dStartXAtStartup,                        (int)(dStartY - dBlockSize), (int)dStartXAtStartup, (int)dStartY); RB151:              } RB152:            } RB153:          } RB154:        } //========================================================================================// Form1.Designer.cs: RB200:        namespace RollingBlock RB201:        { RB202:          partial class Form1 RB203:          {                   // Required designer variable. RB204:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. RB205:            protected override void Dispose(bool disposing) RB206:            { RB207:              if (disposing && (components != null)) components.Dispose(); RB208:              base.Dispose(disposing); RB209:            } RB210:            #region Windows Form Designer generated code                   // Required method for Designer support. RB211:            private void InitializeComponent() RB212:            { RB213:              this.components = new System.ComponentModel.Container(); RB214:              this.button1 = new System.Windows.Forms.Button(); RB215:              this.button2 = new System.Windows.Forms.Button(); RB216:              this.panel1 = new System.Windows.Forms.Panel(); RB217:              this.timer1 = new System.Windows.Forms.Timer(this.components); RB218:              this.button3 = new System.Windows.Forms.Button(); RB219:              this.button4 = new System.Windows.Forms.Button(); RB220:              this.label1 = new System.Windows.Forms.Label(); RB221:              this.label2 = new System.Windows.Forms.Label(); RB222:              this.SuspendLayout();                     //                     // button1                     // RB223:              this.button1.Location = new System.Drawing.Point(325, 16); RB224:              this.button1.Name = "button1"; RB225:              this.button1.Size = new System.Drawing.Size(150, 25); RB226:              this.button1.TabIndex = 0; RB227:              this.button1.Text = "Start the Block Rolling"; RB228:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // RB229:              this.button2.Location = new System.Drawing.Point(16, 160); RB230:              this.button2.Name = "button2"; RB231:              this.button2.Size = new System.Drawing.Size(75, 25); RB232:              this.button2.TabIndex = 1; RB233:              this.button2.Text = "Quit"; RB234:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // panel1                     // RB235:              this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; RB236:              this.panel1.Location = new System.Drawing.Point(25, 136); RB237:              this.panel1.Name = "panel1"; RB238:              this.panel1.Size = new System.Drawing.Size(450, 2); RB239:              this.panel1.TabIndex = 2;                     //                     // timer1                     // RB240:              this.timer1.Tick += new System.EventHandler(this.timer1_Tick);                     //                     // button3                     // RB241:              this.button3.Location = new System.Drawing.Point(152, 160); RB242:              this.button3.Name = "button3"; RB243:              this.button3.Size = new System.Drawing.Size(150, 25); RB244:              this.button3.TabIndex = 3; RB245:              this.button3.Text = "Increase Rolling Rate"; RB246:              this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // button4                     // RB247:              this.button4.Location = new System.Drawing.Point(328, 160); RB248:              this.button4.Name = "button4"; RB249:              this.button4.Size = new System.Drawing.Size(150, 25); RB250:              this.button4.TabIndex = 4; RB251:              this.button4.Text = "Decrease Rolling Rate"; RB252:              this.button4.Click += new System.EventHandler(this.button4_Click);                     //                     // label1                     // RB253:              this.label1.AutoSize = true; RB254:              this.label1.Location = new System.Drawing.Point(32, 16); RB255:              this.label1.Name = "label1"; RB256:              this.label1.Size = new System.Drawing.Size(114, 14); RB257:              this.label1.TabIndex = 5; RB258:              this.label1.Text = "Block Timer Interval =";                     //                     // label2                     // RB259:              this.label2.AutoSize = true; RB260:              this.label2.Location = new System.Drawing.Point(140, 16); RB261:              this.label2.Name = "label2"; RB262:              this.label2.Size = new System.Drawing.Size(23, 14); RB263:              this.label2.TabIndex = 6; RB264:              this.label2.Text = "500";                     //                     // Form1                     // RB265:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); RB266:              this.ClientSize = new System.Drawing.Size(484, 200); RB267:              this.Controls.Add(this.label2); RB268:              this.Controls.Add(this.label1); RB269:              this.Controls.Add(this.button4); RB270:              this.Controls.Add(this.button3); RB271:              this.Controls.Add(this.panel1); RB272:              this.Controls.Add(this.button2); RB273:              this.Controls.Add(this.button1); RB274:              this.Name = "Form1"; RB275:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; RB276:              this.Text = "Rolling Block"; RB277:              this.Paint += new System.Windows.Forms.PaintEventHandler                       (this.Form1_Paint); RB278:              this.Load += new System.EventHandler(this.Form1_Load); RB279:              this.ResumeLayout(false); RB280:              this.PerformLayout(); RB281:            } RB282:            #endregion RB283:            private System.Windows.Forms.Button button1; RB284:            private System.Windows.Forms.Button button2; RB285:            private System.Windows.Forms.Panel panel1; RB286:            private System.Windows.Forms.Timer timer1; RB287:            private System.Windows.Forms.Button button3; RB288:            private System.Windows.Forms.Button button4; RB289:            private System.Windows.Forms.Label label1; RB290:            private System.Windows.Forms.Label label2; RB291:          } RB292:        } 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net