Asp. NET interactive bitmap form design (2)

Source: Internet
Author: User
Tags abstract integer
Asp.net| Interaction | Design Our Mission
The basic idea of this program is as follows: We have an abstract base class (in Microsoft Visual Basic?). is MustInherit), which contains common data (such as borders) and a set of virtual methods, most of which are abstract (MustOverride in Visual Basic), such as Draw. Note that Draw polymorphism is important because each object type that can be drawn (such as dots, lines, rectangles, circles, and so on) is drawn in completely different code.

Although the method can be polymorphic, the data cannot. Therefore, we only put data that is actually applied to all possible objects that can be drawn in the program--in this case, a border and a color (the line in which the object is drawn) is placed.

Data related to a particular type of drawing object, such as the center and radius of a circle, the coordinates of a rectangle's relative point, or the endpoint of a line, should be declared in a particular class that corresponds to a drawn object of that type, derived from the abstract base class. Note that you can use a two-time derivation to merge similar objects. For example, you can derive a circle from an ellipse, because all the circles are ellipses. Similarly, a square can be derived from a rectangle because all squares are rectangles (also quadrilateral, polygon). The selected derivation tree reflects the relationships between classes, as well as the usual expected usage patterns, so that the operations you perform frequently are quick and easy.

The following is our class derivation diagram:



Figure 3: Class derivation diagram

Because constructors (New in Visual Basic) exist primarily because they are used to initialize data, constructors are not (and cannot be) polymorphic. This means that the initial creation operation cannot be polymorphic because the data requirements vary depending on the type. However, a good design can be used to treat objects as polymorphic after the object is created, as we do here.

Let's take a look at what this class set contains, starting with the root abstract base class:

Abstract (MustInherit) base class
The following is the code for the abstract base class in C #. Click here to view all the source files in a new window.



C#

Public abstract class DShape {
public abstract void Draw (Graphics g);
protected Rectangle bounding;
protected Color PenColor; should also have attributes
You should also have methods such as moving, resizing, and so on.
}

The following are equivalent Visual Basic. NET code. Click here to view all the source files in a new window.

Visual Basic. NET

Public MustInherit Class DShape
Public MustOverride Sub Draw (ByVal g as Graphics)
Protected Bounding as Rectangle
Protected PenColor as Color ' should also have attributes
' It should also have the means of moving, resizing, and so on.
End Class

Although the syntax is different, it is obvious that this is the same class.

Note that the Draw method is implied as virtual (Overridable) because it is declared abstract (MustOverride). Also note that we do not provide an implementation in this class. Because we do not yet know what to execute in this class, it is not possible to write the drawing code.

What data is included?
Note also that there is not much data here--but we have declared all the data for such an abstract class.

Each drawing object, regardless of its shape, has a border-the smallest possible rectangle that can fully contain the object. Borders are used to draw points (as small rectangles), rectangles, and circles-and, for other shapes, as the first quick estimate for a hit or crash test.

There are not many other commonalities that apply to all objects; The center is useful for some objects, such as circles and rectangles, and does not make sense for other objects, such as triangles. And usually use a corner to represent the rectangle instead of using the center. But you can't use corners to specify a circle because a circle has no corners. The Dr. GUI is sure you've seen the difficulty of specifying additional data for a common, drawn object.

Each of the drawn objects also has a color associated with the line that drew it, which we also made a statement.

Some derived classes
As mentioned above, we cannot really create an object of an abstract base class type, although we can treat any object that derives from the abstract base class (or any base class) as a base class object.

So, to create a drawing object, we have to derive a new class from the abstract base class--and ensure that all abstract/mustoverride methods are overwritten.

In this case, we'll use the DHollowCircle class. The Dhollowrectangle class is very similar to the Dpoint class.

The following is the DHollowCircle in C #. Click here to view other classes in a new window.


C#
public class Dhollowcircle:dshape
{
Public DHollowCircle (point p, int radius, Color pencolor) {
P.offset (-radius,-radius); Need to convert to upper left corner
int diameter = radius * 2;
bounding = new Rectangle (p, new Size (diameter, diameter));
This.pencolor = PenColor;
}

public override void Draw (Graphics g) {
using (Pen p = new Pen (pencolor)) {
G.drawellipse (P, bounding);
}
}
}


The following are the equivalent Visual Basic. NET classes. Click here to view other classes in a new window.


Visual Basic. NET
Public Class DHollowCircle
Inherits DShape

Public Sub New (ByVal p as point, ByVal radius as Integer, _
ByVal PenColor as Color)
P.offset (-radius,-radius) ' needs to be converted to upper left corner
Dim diameter as Integer = radius * 2
bounding = new Rectangle (p, new Size (diameter, diameter))
Me.pencolor = PenColor
End Sub

Public Overrides Sub Draw (ByVal g as Graphics)
Dim p = New Pen (pencolor)
Try
G.drawellipse (p, bounding)
Finally
P.dispose ()
End Try
End Sub
End Class


Note that we do not declare additional data for this class-it gives a border and a pen enough. (This is true for points and rectangles, but not for triangles and other polygons.) Our application does not need to know the center or radius of the circle after the circle is set, so ignore it. (If the center and radius are needed, we can store the data or calculate it based on the border.) )

But we do need a border, because it's a parameter to the Graphics.drawellipse method used to draw the circle. So we compute the border based on the center point and radius passed in the constructor.

Here's a closer look at each method.


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.