Asp. NET interactive bitmap form design (4)

Source: Internet
Author: User
Tags abstract count empty exception handling
Asp.net| interaction | How design drawings change
You'll notice that the Draw method is essentially the same as the base class--The main difference is that it calls the Fill method, because you need to populate it to finish drawing a fill object. Instead of rewriting the code for the drawing outline, we called the base class again: Mybase.draw (g) in Visual Basic. NET or base in C #. Draw (g);.

Because we are assigning pens to draw outlines, you need to use using or try/finally and Dispose to ensure that the Windows Pen object is released quickly. (Similarly, if you are very sure that the method being invoked does not throw an exception, you can skip exception handling and call Dispose only when the pen is finished processing.) But we have to call Dispose, either directly or through a using statement.

Implementing the Fill Method
The Fill method is simple: Assign a brush and then populate the screen with the object-and make sure the Dispose brush.

Note that in Visual Basic. NET, you must explicitly specify the method that implements an interface (...). Implements Ifillable.fill); In C #, the implementation of a method or property in an interface is determined by the signature of the method or property (because you wrote a method called Fill that does not return anything and accepts a Graphics, so it must be a Implementation of Ifillable.fill). Oddly enough, the Dr. GUI usually prefers a concise programming structure (if it is not possible to do simple writing), but actually prefers to use Visual Basic syntax because it is both clear and flexible (the method name in the Visual basic implementation class does not have to match the name in the interface). And a given method can usually implement multiple interface methods.

Implementation properties

The IFillable interface also contains a property from which you can set and get brush colors. (We use this property in the change fills to hot pink [add fill color to pink] button handler. )

To implement exposing properties, we need a private or protected field. Here we have chosen to protect the field so that it can be easily accessed from derived classes without allowing any classes.

With this field, we can easily write a very simple set and get method pair to implement the property.

Note again that in Visual Basic. NET, you must explicitly specify the properties that you implement.

Interface or abstract (MustInherit) base class?

One of the most common arguments in object-oriented programming is whether to use an abstract base class or an interface.

An interface can provide some extra flexibility, but it has to pay a price: all of them must be implemented for each class that implements the interface. We can use a helper class to help with this work (a related example will be provided later), but you still have to implement everything everywhere. and interfaces cannot contain data (though, unlike in systems in Brand J, they can contain attributes, so they can look like they contain data).

In this case, the Dr. GUI chose to use an abstract base class instead of an interface for DShape because he did not want to repeat the data as a property in each class. In addition, because all of the content derived from DShape is a shape, it can be populated because the object is still a shape.

Your selections may be different, but the Dr. GUI thinks he made the right choice here.

Container for drawing objects
Because you want to repeatedly draw our objects (images are drawn each time in the version of Windows forms, and the Web pages are reloaded each time in the ASP.net version), you need to put them in a container so that they can be accessed over and over again.

The Dr. GUI goes further and makes the container intelligent so that it knows how to draw the contained objects. The following is the C # code for this container class:


C#
public class DShapeList {
ArrayList wholelist = new ArrayList ();
ArrayList filledlist = new ArrayList ();

public void Add (DShape d) {
Wholelist.add (d);
if (d is ifillable)
Filledlist.add (d);
}

public void Drawlist (Graphics g) {
if (wholelist.count = 0)
{
Font f = new Font ("Arial", 10);
g.DrawString ("no content to draw; list is empty ...")
F, Brushes.gray, 50, 50);
}
Else
{
foreach (DShape d in Wholelist)
D.draw (g);
}
}

Public ifillable[] Getfilledlist () {
Return (ifillable[]) Filledlist.toarray (typeof (IFillable));
}
}


The following are similar Visual Basic. NET codes:


Visual Basic

. NET Public Class DShapeList
Dim wholelist as New ArrayList ()
Dim filledlist as New ArrayList ()
Public Sub Add (ByVal D as DShape)
Wholelist.add (d)
If TypeOf D is ifillable Then Filledlist.add (d)
End Sub

Public Sub drawlist (ByVal g as Graphics)
If wholelist.count = 0 Then
Dim F as New Font ("Arial", 10)
g.DrawString ("There is nothing to draw; the list is empty ...", _
F, Brushes.gray, 50, 50)
Else
Dim D as DShape
For each d in wholelist
D.draw (g)
Next
End If
End Sub

Public Function getfilledlist () as IFillable ()
Return Filledlist.toarray (GetType (ifillable))
End Function
End Class



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.