习题12 参考解答
1.多机联合绘图系统中为何不使用Panel作为画板?
【解答】
如果使用Panel作为画板,在绘制图形图像时会出现闪烁现象。因此单独设计一个用户控件用作画板,通过设置该用户控件的【DoubleBuffered】属性值为true来启用双缓冲,从而避免出现闪烁现象。
2.读懂多机联合绘图系统的源程序,在此基础上进行功能扩展。
1)实现任意大小三角形和六边形的绘制。
2)实现任意形状的带箭头曲线、封闭曲线的绘制。
【解答】
1)在项目中添加一个新类DrawPloygon使其从TrackRectangle继承,重写TrackRectangle类中Draw方法实现椭圆的绘制。
class DrawPloygon: TrackRectangle
大班科学活动教案40篇{
public Rectangle objRectangle;//绘制的多边形外接矩形
public DrawPloygon () { }
public DrawPloygon (int x, int y, int width, int height, Color penColor,int id)
{
this.objRectangle = new Rectangle(x, y, width, height);
this.penColor = penColor;
this.ID = id;
}
public override void Draw(Graphics g) //重写基类的方法
{
using (Pen pen = new Pen(this.penColor))
{
int x = this.objRectangle.X;
int y = this.objRectangle.Y;
//三角形的三个顶点
Point[] points = new Point[3];
points[0] = new Point((2 * x + this.objRectangle.Width) / 2, y);
points[1] = new Point(x, y+this.objRectangle.Height);
points[2] = new Point(x+this.objRectangle.Width, y+this.objRectangle.Height);
g.DrawPolygon(pen,points);
期中考反思 }
}
}
若要实现六边形的绘制,只需要修改Draw方法即可。具体代码如下:
public override void Draw(Graphics g)
{
using (Pen pen = new Pen(this.penColor))
{
int x = this.objRectangle.X;
婚姻线图解大全
int y = this.objRectangle.Y;
//其余六个顶点
Point[] points = new Point[6];
points[0] = new Point((2 * x + this.objRectangle.Width) / 4, y);
points[1] = new Point(x, (2*y + this.objRectangle.Height)/2);
韩信的故事>淮阳菜 points[2] = new Point((2 * x + this.objRectangle.Width) / 4,y+this.objRectangle.Height);
points[3] = new Point((2 * x + this.objRectangle.Width) *3/ 4, y + this.objRectangle.Height);
points[4] = new Point(x+this.objRectangle.Width, (2*y+this.objRectangle.Height)/2);
points[5] = new Point((2 * x + this.objRectangle.Width) * 3 / 4, y);
g.DrawPolygon(pen, points);
}
}
2)实现任意形状的带箭头的曲线、封闭曲线的绘制。从TrackCurve类派生子类DrawArrowCurve、DrawClodCurve类,分别重写Draw方法。
对于带箭头的曲线DrawArrowCurve类的Draw方法的定义如下:
public override void Draw(Graphics g)
刘子楠{
Point[] pts = new Point[pointList.Count];
pointList.CopyTo(pts);
Pen pen = new Pen(penColor, penWidth);
g.SmoothingMode = SmoothingMode.AntiAlias;
化肥会挥发绕口令
AdjustableArrowCap myArrow = new AdjustableArrowCap(4, 4, true);
汉字字谜
pen.CustomEndCap = myArrow;
if (pts.Length < 3)
{
if (pts.Length > 1)
{
g.DrawLine(pen, pts[0], pts[1]);
}
}
el
{
g.DrawCurve(pen, pts);
}
pen.Dispo();
}
对于绘制封闭的曲线来说,设计类DrawClodCurve其中Draw方法定义如下:
public override void Draw(Graphics g)
{
Point[] pts = new Point[pointList.Count];
pointList.CopyTo(pts);
Pen pen = new Pen(penColor, penWidth);
g.SmoothingMode = SmoothingMode.AntiAlias;
AdjustableArrowCap myArrow = new AdjustableArrowCap(4, 4, true);
pen.CustomEndCap = myArrow;
if (pts.Length < 3)
{
if (pts.Length > 1)
{
g.DrawLine(pen, pts[0], pts[1]);
}
}
el
{
g.DrawClodCurve(pen, pts);
}
pen.Dispo();
}