Silverlight 引路蜂二维图形库示例:绘制各种几何图形

本文介绍如何使用Graphics2D对象绘制点、线段、二次及三次曲线等几何图形。还展示了如何定义不同类型的矩形、椭圆和圆弧,并通过实例代码演示了这些图形的绘制过程。

这个例子说明如何使用Graphics2D对象来绘制各种几何图形。引路蜂二维图形库中定义里多种基本几何图形,如,点,线段,曲线和矩形等。接口PathIterator定义了从Path中获取路径元素的方法。接口IShape定义了描述几何图形公用的方法。

类Point定义了二维空间位置在(x,y)一个点。Point point = new Point (x, y); 创建一个点对象。此外Point类也提供了计算两点之间距离的方法等。

线段

类Line定义了平面上一条线段。下面代码绘制一条线段。

// draw Line2D.Double
graphics2D.draw (pen, new Line(x1, y1, x2, y2));

图形库中类Pen可以用来定义绘制线段的线型,线宽等参数。

二次曲线

类QuadCurve实现了IShape接口,它定义了平面上一条二次曲线。二次曲线可以通过曲线的两个端点和一个控制点来定义。

// create new QuadCurve
QuadCurve q = new QuadCurve();
// draw QuadCurve2D with set coordinates
graphics2D.draw(pen,q.setCurve(x1, y1, ctrlx, ctrly, x2,y2));

三次曲线

类CubicCurve同样实现了IShape接口,它定义了平面上一条三次曲线。和二次曲线类似,三次曲线可以通过曲线的两个端点和两个控制点来定义。

// create new CubicCurve
CubicCurve c = new CubicCurve();
// draw CubicCurve with set coordinates
graphics2D.draw(pen, c.setCurve(x1, y1, ctrlx1,ctrly1, ctrlx2, ctrly2, x2, y2));

矩形

类Rectangle是RectangleShape的子类。RectangleShape同样实现了IShape接口。 Rectangle可以通过一个点(x,y)和大小(Dimsension)来定义。

// draw Rectangle
graphics2D.draw(pen,new Rectangle(x, y,rectwidth,rectheight));

类RoundRectangle定义了带圆角的矩形。圆角矩形可以由下列参数来定义:位置,宽度,长度,圆角宽度,圆角长度。

// draw RoundRectangle
graphics2D.draw(pen,new RoundRectangle(x, y,rectwidth,rectheight,10, 10));

椭圆

类Ellipse通过椭圆的外接矩形来定义其大小。

// draw Ellipse
graphics2D.draw(pen, new Ellipse(x, y,rectwidth,rectheight));

圆弧

类Arc通过外接矩形,起始角度,角度,封闭类型来定义。

// draw Arc
graphics2D.draw(pen,new Arc(x, y,rectwidth,rectheight,90, 135,Arc.OPEN));

下面代码显示了多种几何图形。

private void SharpsDemo2D()
{
 Color bg = Color.White;
 Color fg = Color.Black;
 Color red = Color.Red;
 Color white = Color.White;
 Pen pen = new Pen(fg, 1);
 SolidBrush brush = new SolidBrush(red);
 //Clear the canvas with white color.
 graphics2D.Clear(bg);
 Dimension d = new Dimension(screenWidth, screenHeight);
 int gridWidth = d.Width / 2;
 int gridHeight = d.Height / 6;
 
 int x = 5;
 int y = 7;
 int rectWidth = gridWidth - 2 * x;
 int stringY = gridHeight - 3 - 2 - 16;
 int rectHeight = stringY - y - 2;
 graphics2D.Draw(pen, new Line(x, y + rectHeight - 1,
  x + rectWidth, y));
 x += gridWidth;
 graphics2D.Draw(pen, new Rectangle(x, y, rectWidth,
  rectHeight));
 x += gridWidth;
 x = 5;
 y += gridHeight;
 stringY += gridHeight;
 graphics2D.Draw(pen, new RoundRectangle(x, y, rectWidth,
   rectHeight, 10, 10));
 x += gridWidth;
 graphics2D.Draw(pen, new Arc(x, y, rectWidth, rectHeight, 90,
   135, Arc.Open));
 x = 5;
 y += gridHeight;
 stringY += gridHeight;
 graphics2D.Draw(pen, new Ellipse(x, y, rectWidth, rectHeight));
 x += gridWidth;
 // draw GeneralPath (polygon)
 int[] x1Points = { x, x + rectWidth, x, x + rectWidth };
 int[] y1Points = { y, y + rectHeight, y + rectHeight, y };
 Path polygon = new Path(Path.WindEvenOdd,
   x1Points.Length);
 polygon.MoveTo(x1Points[0], y1Points[0]);
 for (int index = 1; index < x1Points.Length; index++)
 {
  polygon.LineTo(x1Points[index], y1Points[index]);
 }
 polygon.ClosePath();
 graphics2D.Draw(pen, polygon);
 x = 5;
 y += gridHeight;
 stringY += gridHeight;
 int[] x2Points = { x, x + rectWidth, x, x + rectWidth };
 int[] y2Points = { y, y + rectHeight, y + rectHeight, y };
 Path polyline = new Path(Path.WindEvenOdd,
   x2Points.Length);
 polyline.MoveTo(x2Points[0], y2Points[0]);
 for (int index = 1; index < x2Points.Length; index++)
 {
  polyline.LineTo(x2Points[index], y2Points[index]);
 }
 graphics2D.Draw(pen, polyline);
 x += gridWidth;
 graphics2D.SetPenAndBrush(pen, brush);
 graphics2D.Fill(null,
  new Rectangle(x, y, rectWidth, rectHeight));
 graphics2D.Draw(null,
  new Rectangle(x, y, rectWidth, rectHeight));
 x = 5;
 y += gridHeight;
 stringY += gridHeight;
 Color[] colors = new Color[] { red, white };
 int[] fractions = new int[] { 0, 255 };
 LinearGradientBrush redtowhite =
   new LinearGradientBrush(x, y, x + rectWidth, y,
   fractions, colors, Brush.NoCycle);
 graphics2D.SetPenAndBrush(pen, redtowhite);
 graphics2D.Fill(null, new RoundRectangle(x, y,
  rectWidth,rectHeight, 10, 10));
 graphics2D.Draw(null, new RoundRectangle(x, y,
  rectWidth,rectHeight, 10, 10));
 x += gridWidth;
 graphics2D.SetPenAndBrush(pen, brush);
 graphics2D.Fill(null, new Arc(x, y, rectWidth,
  rectHeight, 90,135, Arc.Chord));
 graphics2D.Draw(null, new Arc(x, y, rectWidth,
  rectHeight, 90,135, Arc.Chord));
 x = 5;
 y += gridHeight;
 stringY += gridHeight;
 int[] x3Points = { x, x + rectWidth, x, x + rectWidth };
 int[] y3Points = { y, y + rectHeight, y + rectHeight, y };
 Path filledPolygon = new Path(Path.WindEvenOdd,
   x3Points.Length);
 filledPolygon.MoveTo(x3Points[0], y3Points[0]);
 for (int index = 1; index < x3Points.Length; index++)
 {
  filledPolygon.LineTo(x3Points[index], y3Points[index]);
 }
 filledPolygon.ClosePath();
 graphics2D.SetPenAndBrush(pen, brush);
 graphics2D.Fill(null, filledPolygon);
 graphics2D.Draw(null, filledPolygon);
 
}


201

 

项目描述: 该项目是一款计算机平面几何学的绘图工具。 内置了一些交互的样式,供在线浏览。 项目使用Silverlight 2.0(可以使用Microsoft Expression v3 beta 转换成 silverlight v3.0) 以及 C#3.0进行开发(在silverlight 和 wpf 上各自实现了一个版本)。核心的绘图引擎具备了良好的灵活性和扩展性,方便开发人员添加新的图形和功能。 我们将面临的工作类型: 将已有的之前无法顺利迁移到web上的桌面应用程序(庞大交互复杂,有一定安全要求或者比较华丽总之就是Ajax无法胜任)使用Silverlight 3.0 技术迁移到web上。 对于一些良好的Flash应用的迁移。(这属于站坑拉屎,谁先占住坑的谁就是大爷) 对于现有AJAX应用进行更加丰富的扩展。 我们将面临的挑战: 开发WinForm程序的大爷们终于可以灵活的涉足于Web界而不需要搞令人沮丧到要无限次重构直到自己写http协议的Asp.net框架。他们来了。 欢迎Flex阵营的朋友投身的到Silverlight3.0 的圈子中,思路一致(不好听的说法叫抄袭)很好迁移,你们也来吧。 之所以能带来更加丰富的用户体验,是因为那纠葛交错的WinForm消息循环体系,任何的动作都可以轻易牵动应用全身,这远远区别于Web应用开发(因为很多时候我们选择--我刷我刷我刷刷刷)。我们的优势: XAML = html,XAML(资源文件) = CSS,C#(Ruby or Python even JS) = JS。 可见,你依然可以像开发web一样开发Silverlight应用只要大脑思路一转。 Silverlight 最终还是要放置在网站之上(我们首先不考虑Out of browser功能),所以熟悉HTML熟悉JS我们可以方便的让Silverlight与 HTML交互。 赘述到此,我想大家应该已经明白, 这款Silverlight 在线本版的平面几何图形画图板便是经典的WinForm 到 Web的迁移。当你解读Silverlight代码和WPF代码的时候,你将会发现这并没有什么重大的不同之处(所以现在出现了Silverlight 和 WPF的兼容性类,甚至出现了Silverlight 和 WPF的转换程序)。或者我们不妨悲剧的理解这本就是一个WebForm应用。我们带着强烈的愤怒怀疑这个微软的结构师本就是开发了10+年的WinForm(顺便仰慕一下)。 Main/SilverlightClient下为综合演示Demo,用VS2008打开后运行(或右键:在浏览器中打开)即可看到效果 自带了一些在线Demo,其他文件夹中还有些其他几何模型,有兴趣的可以自行研究
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值