博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight 引路蜂二维图形库示例:绘制各种几何图形
阅读量:6854 次
发布时间:2019-06-26

本文共 4631 字,大约阅读时间需要 15 分钟。

这个例子说明如何使用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

 

转载地址:http://tlyyl.baihongyu.com/

你可能感兴趣的文章
物联网技术将深度改变你我生活
查看>>
IDC:2017年全球安全技术支出预计突破817亿美元
查看>>
中国工程院院士:物联网市场须走出碎片化
查看>>
云计算和大数据行业:了解其中真实的谎言
查看>>
香港以大数据打造智慧城市
查看>>
中国电信完成物联网eSIM卡平台建设:力争明年实现eSIM商用
查看>>
OA系统选型分析:华天动力OA与金和OA
查看>>
如果你没被WannaCry感染就一定要小心Adylkuzz
查看>>
HR:2017/2018年数据中心驱动400Gbps部署
查看>>
单元测试覆盖工具coverlipse
查看>>
Jmeter分布式部署文档
查看>>
微软打算用DNA存储数据 但成本和速度仍是个大问题
查看>>
使用Java向properties存数据
查看>>
产能过剩的光伏电池,是否还是未来的朝阳产业?
查看>>
如何在SaaS企业及服务市场上站稳脚跟
查看>>
中心商务区建智慧城市 将现 “芝加哥夜景”
查看>>
移动端App测试实用指南(下)
查看>>
为什么没有一个软件质量保证的RUP工作流程
查看>>
海尔王淼:智能家居互联互通先解决用户需求
查看>>
商务部:中国将采取一切措施维护光伏企业合法权益
查看>>