306
LESSON 25 Fine-Tuning Classes
In the form’s
Load event handler, create some ShapeRectangles and ShapeEllipses and
add them to the list.
Give the form a
CheckBox labeled Colored. In its CheckedChanged event handler, invalidate
the form to force a redraw.
In the form’s
Paint event handler, redraw the form by looping through the Shapes in the
list. If the
CheckBox is checked, invoke the Shape objects’ Draw method, passing it the pen
and brush
Pens.Blue and Brushes.LightBlue. If the CheckBox is not checked, invoke the
Draw method with no parameters.
Hints
If gr is the Graphics object, then you can use these methods to draw:
gr.Clear(this.BackColor)
Clears the object with the form’s background color.
gr.FillRectangle(
brush, rect) Fills a rectangle defined by the Rectangle rect with
brush.
gr.DrawRectangle(
pen, rect) Outlines a rectangle defined by the Rectangle rect
with
pen.
gr.FillEllipse(
brush, rect) Fills an ellipse defined by the Rectangle rect with brush.
gr.DrawEllipse(
pen, rect) Outlines an ellipse defined by the Rectangle rect with pen.
In the
Paint event handler, draw on the Graphics object provided by the event handler’s
e.Graphics parameter.
Step-by-Step
Start a new program and create the
Shape class.
Give the class a
using System.Drawing directive.
1. Simply place the using directive at the top of the file.
Give the class a property named
Bounds of type Rectangle. A Rectangle has prop-
erties
X, Y, Width, and Height.
1. Use code similar to the following:
public Rectangle Bounds { get; set; }
Make a constructor that takes
x, y, width, and height parameters and uses them to
initialize the
Bounds property.
1. Use code similar to the following:
public Shape(int x, int y, int width, int height)
{
Bounds = new Rectangle(x, y, width, height);
}
596906c25.indd 306 4/7/10 12:33:59 PM
Try It
307
Make a virtual
Draw method that takes three parameters of type Graphics, Pen, and
Brush. Make this method draw a box with an X in it on the Graphics object using
the
Pen and Brush.
1. Use code similar to the following:
// Draw a box with an X in it using a pen and brush.
public virtual void Draw(Graphics gr, Pen pen, Brush brush)
{
gr.FillRectangle(brush, Bounds);
gr.DrawRectangle(pen, Bounds);
gr.DrawLine(pen, Bounds.Left, Bounds.Top,
Bounds.Right, Bounds.Bottom);
gr.DrawLine(pen, Bounds.Right, Bounds.Top,
Bounds.Left, Bounds.Bottom);
}
Make a virtual overloaded
Draw method that takes only a Graphics object as a
parameter. Make it invoke the other
Draw method, passing it the pen and brush
Pens.Red and Brushes.Transparent.
1. Use code similar to the following:
// Draw a box with an X in it.
public virtual void Draw(Graphics gr)
{
Draw(gr, Pens.Red, Brushes.Transparent);
}
Derive the
ShapeRectangle class from the Shape class.
Make a constructor that takes
x, y, width, and height parameters and invokes the
base class’s constructor.
1. Use code similar to the following:
public ShapeRectangle(int x, int y, int width, int height)
: base(x, y, width, height)
{
}
Override the
Shape class’s version of the Draw method that takes four parameters so
it draws a rectangle.
1. Use code similar to the following:
public override void Draw(Graphics gr, Pen pen, Brush brush)
{
gr.FillRectangle(brush, Bounds);
gr.DrawRectangle(pen, Bounds);
}
Override the second
Draw method so it calls the first, passing it the pen and brush
Pens.Black and Brushes.Transparent.
1. Use code similar to the following:
596906c25.indd 307 4/7/10 12:33:59 PM
308
LESSON 25 Fine-Tuning Classes
public override void Draw(Graphics gr)
{
Draw(gr, Pens.Black, Brushes.Transparent);
}
Repeat the previous steps for the
ShapeEllipse class.
1. The following code shows the ShapeEllipse class’s code:
class ShapeEllipse : Shape
{
public ShapeEllipse(int x, int y, int width, int height)
: base(x, y, width, height)
{
}
public override void Draw(Graphics gr, Pen pen, Brush brush)
{
gr.FillEllipse(brush, Bounds);
gr.DrawEllipse(pen, Bounds);
}
public override void Draw(Graphics gr)
{
Draw(gr, Pens.Black, Brushes.Transparent);
}
}
In the main form, create a
List<Shape>.
1. Use code similar to the following:
// Our list of shapes to draw.
List<Shape> Shapes = new List<Shape>();
In the form’s
Load event handler, create some ShapeRectangles and ShapeEllipses and
add them to the list.
1. You can use code similar to the following, or you can create any ShapeEllipse and
ShapeRectangle objects that you like:
// Make some shapes.
private void Form1_Load(object sender, EventArgs e)
{
Shapes.Add(new ShapeEllipse(50, 50, 200, 200));
Shapes.Add(new ShapeEllipse(100, 80, 40, 60));
Shapes.Add(new ShapeEllipse(100, 80, 40, 60));
Shapes.Add(new ShapeEllipse(120, 100, 20, 30));
Shapes.Add(new ShapeEllipse(160, 80, 40, 60));
Shapes.Add(new ShapeEllipse(180, 100, 20, 30));
Shapes.Add(new ShapeEllipse(135, 130, 30, 50));
Shapes.Add(new ShapeRectangle(120, 190, 60, 5));
Shapes.Add(new ShapeRectangle(75, 25, 150, 50));
Shapes.Add(new ShapeRectangle(50, 75, 200, 10));
}
596906c25.indd 308 4/7/10 12:33:59 PM
Try It
309
Give the form a
CheckBox. In its CheckedChanged event handler, invalidate the form to force
a redraw.
1. The following code shows this event handler:
// Force a redraw.
private void coloredCheckBox_CheckedChanged(object sender, EventArgs e)
{
this.Invalidate();
}
In the form’s
Paint event handler, redraw the form by looping through the Shapes in the
list. If the
CheckBox is checked, invoke the Shape objects’ Draw method, passing it the pen
and brush
Pens.Blue and Brushes.LightBlue. If the CheckBox is not checked, invoke the
Draw method with no parameters.
1. Use code similar to the following:
// Redraw the objects.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor);
// Loop through all of the shapes.
foreach (Shape shape in Shapes)
{
if (coloredCheckBox.Checked)
{
// Draw with a pen and brush.
shape.Draw(e.Graphics, Pens.Blue, Brushes.LightBlue);
}
else
{
// Draw with no pen or brush.
shape.Draw(e.Graphics);
}
}
}
Figure 25-2 shows the result for the objects I created. A more useful drawing program would save
edge and fill colors in the objects so they could each have different colors.
FIGURE 252
596906c25.indd 309 4/7/10 12:34:00 PM
310
LESSON 25 Fine-Tuning Classes
Please select Lesson 25 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Copy the complex number program you built in Lesson 24, Exercise 2 (or download Lesson 24’s
version from the book’s web site). Override the class’s
ToString method so it returns the num-
ber in a form similar to “2 + 3i.” Overload the
ComplexNumber class’s AddTo, MultiplyBy, and
SubtractFrom methods so you can pass them a single double parameter representing a real
number with no imaginary part. Modify the form so you can test the new methods.
2. Copy the bank account program you built in Lesson 24, Exercise 3 (or download Lesson 24’s
version from the book’s web site). Derive a new
OverdraftAccount class from the Account
class. Give it a constructor that simply invokes the base class’s constructor. Override the
Debit method to allow the account to have a negative balance and charge a $50 fee if any
debit leaves the account with a negative balance. Change the main program so the
Account
variable is still declared to be of type
Account but initialize it as an OverdraftAccount.
(Hint: Don’t forget to make the
Account class’s version of Debit virtual.)
3. Copy Lesson 23’s Turtle program. The Turtle class has a Move method that moves the turtle
a specified distance in the object’s current direction. Overload this method by making a second
version that takes as parameters the X and Y coordinates where the turtle should move. Be sure
to raise the
OutOfBounds event if the point is not on the canvas. (Hint: Can you reuse code
somehow between the two
Move methods?)
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson25 folder.
596906c25.indd 310 4/7/10 12:34:00 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.52.124