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

分享到:
相关推荐
Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例Silverlight示例
WebCast系列silverlight视频:绘制与着色
【Silverlight在线几何绘图】是一种基于Web的图形绘制技术,它允许用户在浏览器中创建、编辑和展示复杂的几何图形。这项技术由微软公司开发,主要用于增强网页的交互性和多媒体体验,尤其在富互联网应用程序(RIA)...
**Silverlight 示例详解** Silverlight,由微软公司开发,是一种基于.NET Framework的浏览器插件,旨在增强Web应用程序的媒体体验和交互性。它允许开发者创建丰富的、动态的、交互式的用户界面,支持多媒体播放、2D...
在本文中,我们将深入探讨如何使用Silverlight技术来实现一个二维旋转、平面渐变和动画效果,以此模拟雷达扫描的视觉效果。Silverlight是微软推出的一款强大的富互联网应用程序(RIA)开发平台,它允许开发者创建...
Canvas对象是Silverlight用户界面(UI)构建的基础,它是一个二维绘图区域,支持绝对定位。这意味着你可以精确地控制每个子元素在Canvas上的位置,通过设置其`Top`和`Left`属性。这两个属性分别定义了元素距离Canvas...
《Silverlight微软一站式示例代码库:中文版源码解析》 Silverlight,作为微软推出的一款基于.NET Framework的浏览器插件,曾广泛应用于富互联网应用(RIA)开发,尤其是在多媒体内容展示、交互式用户界面设计等...
- **坐标系统**:在计算机图形学中,理解如何定义和使用二维坐标系统来定位和绘制图形元素。 - **颜色和填充**:了解如何使用RGB、ARGB或其他颜色模型表示颜色,以及如何设置填充样式和描边样式。 - **数据绑定**:...
【银光+N维拼图游戏:Silverlight与WCF技术的完美融合】 本文将深入探讨一个基于Silverlight和WCF技术构建的n维拼图游戏的实现细节。Silverlight是微软推出的一种富互联网应用程序(RIA)平台,它允许开发者创建...
【Silverlight探秘系列课程(13):网络通信与开发示例】 本课程主要探讨的是Silverlight在实现网络通信及开发示例方面的知识。Silverlight是微软推出的一款跨浏览器、跨平台的插件,主要用于增强Web应用的交互性和...
**Silverlight 3 完整示例解析** Silverlight,微软推出的一种富互联网应用程序(RIA)平台,旨在提供丰富的媒体体验和交互式用户界面。Silverlight 3是该技术的一个重要版本,它在前两版的基础上增加了许多新特性...
2. **图形和媒体支持**:Silverlight内置了强大的图形渲染引擎,支持矢量图形、动画以及高清视频播放,使得开发富媒体应用变得轻松。 3. **跨浏览器兼容**:Silverlight插件可以在多种浏览器上运行,包括Internet ...
【标题】:“Silverlight示例1”揭示了Silverlight技术在Web应用开发中的实际运用,这是一个专注于用户体验提升和多媒体互动的平台。Silverlight是由微软公司推出的插件技术,旨在为互联网提供丰富的媒体体验和交互...
Silverlight是一种由微软开发的富互联网应用程序(RIA)平台,它允许开发者创建具有丰富图形、交互性和动画效果的Web应用程序。这个"Silverlight项目界面示例"提供了宝贵的资源,可以帮助你理解和模仿,以便设计出...
Silverlight计算机图形学2二维坐标和显示基础.pdf
Silverlight 2系列(35):升级Silverlight 2 Beta 1应用程序到Beta 2 Silverlight 2系列(34):使用Silverlight Streaming托管Silverlight应用程序 Silverlight 2系列(33):Silverlight 2应用Web Service两例 ...
Silverlight 2系列(35):升级Silverlight 2 Beta 1应用程序到Beta 2 Silverlight 2系列(34):使用Silverlight Streaming托管Silverlight应用程序 Silverlight 2系列(33):Silverlight 2应用Web Service两例 ...
5. **Silverlight 特性**:在WinCE环境下,虽然Silverlight的功能可能受到限制,但它仍然支持动画、图形绘制、数据绑定、样式和模板等功能,使得开发出的界面既美观又动态。 6. **兼容性和性能**:由于WinCE的资源...
这个“silverlight 示例”压缩包很可能是为了展示Silverlight技术的应用,通过SilverlightAirlines这个示例项目来帮助开发者理解和学习Silverlight的编程。 Silverlight在2007年首次发布,作为Adobe Flash的竞争...