The goal of Lab 4 is to get used to working with object-level variables and methods.
Part 1
Continue working with a copy of your project from last week.
1.
Create a copy of your Lab3B folder and rename it to distinguish Lab4 from Lab 3B. In Visual Studio, select Open Project and open the solution (filename.sln) from the Lab4 folder you just created. It will look identical to project 3 so you should rename the Solution and Project using the properties window .
2.
Create a new label to display the distance between mouse clicks in the currently selected units.
3.
In the frmMain class , create four variables to record the starting and ending points of a line. public partial class frmMain : Form
{ int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0;
.
.
.
}
4.
Create a “MouseDown” handler for the PictureBox by finding the “MouseDown” event in the
Property/Event window (lightning bolt) on the lower right and double-clicking on it. In this method you can save the starting mouse position provided by e.X and e.Y in the x1 and y1 variables. private void pbxImage_MouseDown( object sender, MouseEventArgs e)
{
x1 = e.X;
y1 = e.Y;
}
5.
To display the line length, write a function that implements the basic line length calculation: public double LineLength( int pX1, int pY1, int pX2, int pY2)
{ return Math .Sqrt( Math .Pow(pX2 - pX1, 2) + Math .Pow(pY2 - pY1, 2));
}
It should take in the 4 variables of the 2 end points of a line and return a double value.
6.
Create a “MouseUp” handler for the PictureBox by finding the “MouseUp” event in the
Property/Event window (lightning bolt) on the lower right and double-clicking on it. In this event handler, you will save the current mouse position in x2 and y2 and display the length returned from
your “LineLength” function. Use the conversion function from Lab 3 to convert the length to the currently-selected unit of measure. Since you need the window to redraw after each MouseUp event, include the Refresh() method: private void pbxImage_MouseUp( object sender, MouseEventArgs e)
{
x2 = e.X;
y2 = e.Y;
lblLength.Text = "Distance is: " +
ConvertFromPixels(( int )LineLength(x1,y1,x2,y2)).
ToString( "##,##0.00" );
pbxImage.Refresh();
}
7.
Create a “Paint” event handler for the PictureBox by finding the “Paint” event in the Property/Event window (lightning bolt) on the lower right and double-clicking on it. You can draw a line using the
“e.Graphics.Drawline” statement in this sample code: private void pbxImage_Paint( object sender, PaintEventArgs e)
{
e.Graphics.DrawLine( new Pen ( Color .Red, 3), x1, y1, x2, y2);
}
The paint handler is invoked whenever Refresh() is called as in the “MouseUp” handler. You can use different colors and line widths by experimenting with different parameters to new Pen(…).
“Click and Drag” your mouse on the image. When you lift up the Mouse button the line and length should appear. The result should look something like the image below, with the line starting where the mouse is depressed and ending where the mouse is released:
8.
Zip your current Lab 4 folder, save it as Part 1 and e-mail it to me.
Part 2
Part 2 of this lab is to create a “line class” using the results from Part 1 and to get used to working with multiple code files.
1.
Add a new file called Geometry.cs to the project: Select Project, Add New Item, select C# Class, and fill in the name (Geometry.cs) at the bottom of the form. Alternatively, you can right click on the project name in the solution explorer window to get to the same menu items.
2.
Note that Visual Studio will default the class name to Geometry (taken from the Geometry.cs file name). This is because there is usually one class per file. In our case we’ll be creating multiple small classes in this file.
Add a class in the new file called “Line”: namespace YourProjectNamespace
{ class Geometry
{
} class Line
{
}
}
3.
Copy the variables related to the line from your form class or main file to the new file. The Line class should have member variables for the starting and ending coordinates, such as x1, y1, x2 and y2.
Copy the LineLength function from your frmMain class into your line class and remove the parameters list. Rename the X,Y variables in the function to match those defined in the Line class.
The line class should look something like this: class Line
{ public int x1 = 0; public int y1 = 0; public int x2 = 0; public int y2 = 0; public double LineLength()
{ return Math .Sqrt( Math .Pow(x2 - x1, 2) + Math .Pow(y2 - y1, 2));
}
}
Note the use of the access modifier “public” on the member variables and method. This makes the items available to outside modules. Since the LineLength function is now part of the Line class, parameters are no longer used since the beginning and ending coordinate variables are part of the class as well. You now have a Line class that you can use in your main code file.
4.
In your frmMain class, replace the x1, y1, x2, y2 variable declarations with the Line object declaration:
Line myLine = new Line ();
Any references to x1, y1, x2, y2 and the LineLength function will now generate error messages.
Replace them with their “myLine” equivelants myLine.x1
, myLine.y1
, myLine.LineLength() etc. to make your program work as it did in Lab 3.
5.
By default, the geometry file used the namespace of your project. This makes its contents directly accessible from our main form file. To make your Geometry class more generic and portable for use in other programs, you can put the new geometry code in its own namespace. This requires changing the namespace name at the top of the new Geometry file: namespace Geometry
To access this new namespace within in your main Form1 program, you will need to add a “using statement” at the top of the file: using Geometry;
6.
Zip your current Lab 4 folder, save it as Part 2 and e-mail it to me.