Asp.net| Interaction | Design Constructors
The constructor passes three parameters: the point that contains the center coordinates of the circle, the radius of the circle, and a System.Drawing.Color structure that contains the color used to draw the outline of the circle.
Then we calculate the border according to the center and radius, and set the pen color item to the color object we pass.
Drawing code
The Draw method overload is actually very simple: it creates a Pen object based on the color object we save in the constructor, and then uses the pen to invoke the Graphics.drawellipse method to draw the circle, passing the border we created earlier.
Graphics, pens, and brushes
Here we need to explain the Graphics, Pen, and Brush objects. (You'll see the Brush object when you start populating our stuffed objects.) )
The Graphics object represents a virtualized drawing space associated with a real drawing space. Virtualization means that by drawing on a Graphics object, we can use the same Graphics method to draw on the actual surface of any type associated with that object. For those who are accustomed to using MFC or Microsoft Windows? SDK programming users, the Graphics object is equivalent to a. NET version of Windows called a "device context" (or DC).
In this Windows forms application, the Graphics object passed to the Draw method is associated with a window on the screen-this is PictureBox. When this code is used in our Microsoft asp.net application, the Graphics object passed to the Draw method is associated with a bitmap image. It can also be associated with a printer or other device.
The advantage of this scheme is that we can draw on different surfaces using the same drawing code. In our drawing code, we don't need to know anything about the different--. NET Framework (and the underlying operating system) between screens, bitmaps, printers, and so on that can handle all the details for us.
Using the same markup, pens and brushes become virtualized drawing tools. The pen represents the line property--the color, width, style, or even the bitmap used to draw the line. A brush represents an attribute of a fill area--a color, a style, or even a bitmap to fill an area.
Purge (or at least Dispose of) after use using
Graphics, Pen, and Brush objects are associated with similar types of Windows objects. These Windows objects are allocated to the operating system's memory, which has not yet been managed by the. NET Runtime. Hosting these objects in memory for a long time can cause performance problems and, under Microsoft Windows 98, can cause a drawing problem when the graphics heap fills up. Therefore, we should release these Windows objects as soon as possible.
When the appropriate. NET Framework object completes the operation and reclaims memory, the. NET runtime automatically releases the Windows object. But it can take a long time to reclaim memory-if we don't release these objects quickly, all the unfortunate things (including filling up the Windows heap) can happen. In the ASP.net version of the application, this behavior is exacerbated by the fact that many users access the application on the same server.
Because these objects are associated with resources that are not managed, they implement the IDisposable interface. The interface has a method, Dispose, that separates the. NET Framework objects from the Windows object and frees the Windows objects so that the computer is in good condition.
You only need to do one task: Make sure that the Dispose method is invoked after the object is used.
The classic form of this content is shown in Visual Basic. NET code: First create the object, then use the object in a Try block, and finally clean the object in a finally block. Try/finally can ensure that objects are cleaned up even if an exception occurs. (In this case, the drawing method we call might not throw an exception, so the try/finally block might not be needed.) But mastering these techniques is useful, so the Dr GUI also wants to show you the right approach. )
This form is common, so C # provides it with a private statement: using. The using statements in C # code are equivalent to declarations and try/finally in Visual Basic. NET code-but are more concise, convenient, and reduce the likelihood of errors occurring. (Dr. The GUI is not sure why Visual Basic. NET does not contain some statements such as using. )
Interface
Some (but not all) of the drawn objects can be populated. Some objects, such as dots and lines, cannot be populated because they are not enclosed, and objects such as rectangles and circles can be hollow or filled.
On the other hand, as with all the objects that can be drawn as polymorphic, it is convenient to have all the populated objects as polymorphic. For example, as we put all the objects that can be drawn into a collection, by traversing the collection and invoking Draw on each object to draw the object, we can put all the populated objects into a collection without regard to the actual type of the populated objects. So, we use some kind of mechanism (such as inheritance) to get real polymorphism.
Because not all of the draft objects can be populated, the declaration of the Fill method cannot be placed in an abstract base class. The. NET Framework does not allow multiple inheritance of classes, so it cannot be placed in another abstract base class. And if we do not derive a populated object from the other base classes of the non-refillable class, then all the objects that can be drawn cannot be treated as polymorphic.
But the. NET Framework supports interfaces--and provides a class that implements any number of interfaces. The interface does not have any implementation--no code, no data. Therefore, the class that implements the interface must provide all the content.
The interface can contain only declarations. The following is our interface ifillable in C #. Click here to view all the source files in a new window.
C#
Public interface IFillable {
void Fill (Graphics g);
Color FillBrushColor {get; set;}
}
The following are equivalent Visual Basic. NET code. Click here to view all the source files in a new window.
Visual Basic. NET
Public Interface IFillable
Sub Fill (ByVal g as Graphics)
Property FillBrushColor () as Color
End Interface
We do not need to declare a method or virtual/overridable or Abstract/mustoverride attribute, as well as any other item, because all methods and properties in the interface are automatically set to public and abstract/mustoverride.
Use a property: cannot include data in an interface
Note that although we cannot declare a field in an interface, we can declare a property because the property is actually implemented as a method.
But doing so will put a burden on the implementation of the interface, as you can see below. The implementing person must implement the get and set methods and any data necessary to implement the property. If the implementation is very complex, you can write a helper class to encapsulate some parts. Later in this article, we'll show you how to use the helper class for a slightly different context environment.
Implementing interfaces
We have defined the interface and now we can implement it in the class. Note that we must provide a complete implementation of the implemented interface: You cannot select only a portion from it.
Let's look at the code for the Dfilledrectangle class in C #.
C#
public class Dfilledcircle:dhollowcircle, ifillable
{
Public DFilledCircle (Point center, int radius, Color pencolor,
Color brushcolor): Base (center, RADIUS, pencolor) {
This.brushcolor = BrushColor;
}
public void Fill (Graphics g) {
using (Brush B = new SolidBrush (BrushColor)) {
G.fillellipse (b, bounding);
}
}
protected Color BrushColor;
Public Color FillBrushColor {
get {
return brushcolor;
}
set {
BrushColor = value;
}
}
public override void Draw (Graphics g) {
Fill (g);
Base. Draw (g);
}
}
The following is code for the Dfilledrectangle class in Visual Basic. NET.
Visual Basic. NET
Public Class Dfilledrectangle
Inherits Dhollowrectangle
Implements ifillable
Public Sub New (ByVal rect as Rectangle, _
ByVal pencolor as color, ByVal brushcolor as color)
MyBase.New (rect, pencolor)
Me.brushcolor = BrushColor
End Sub
Public Sub Fill (ByVal g as Graphics) Implements Ifillable.fill
Dim B = New SolidBrush (fillbrushcolor)
Try
G.fillrectangle (b, bounding)
Finally
B.dispose ()
End Try
End Sub
Protected BrushColor as Color
Public Property FillBrushColor () as Color _
Implements Ifillable.fillbrushcolor
Get
Return BrushColor
End Get
Set (ByVal Value as Color)
BrushColor = Value
End Set
End Property
Public Overrides Sub Draw (ByVal g as Graphics)
Dim p = New Pen (pencolor)
Try
Fill (g)
Mybase.draw (g)
Finally
P.dispose ()
End Try
End Sub
End Class
The following are considerations for these classes.
Deriving from Hollowrectangle
We derive the fill class from the hollow version of this class. Most of the content in this class has changed: Both the Draw method and the constructor are new (but both call the base class version), and the implementation is added for the IFillable interface's Fill method and the FillBrushColor property.
The reason for the new constructor is that we have included in this class other data that needs to be initialized, that is, a fill brush. (You can review the brushes we discussed earlier.) Note how this constructor calls the base class constructor: In C #, the call is built into the declaration (: Base (center, RADIUS, pencolor)), and in Visual Basic. NET, we place it explicitly in the New method (that is, the constructor) Row (MyBase.New (rect, pencolor)).
Since we have passed two of the three parameters to the base class constructor, we can now just initialize the last field.
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.