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

jerry Silverligth 2015年11月26日 收藏

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

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

  1. private void SharpsDemo2D()
  2. {
  3.  Color bg = Color.White;
  4.  Color fg = Color.Black;
  5.  Color red = Color.Red;
  6.  Color white = Color.White;
  7.  Pen pen = new Pen(fg, 1);
  8.  SolidBrush brush = new SolidBrush(red);
  9.  //Clear the canvas with white color.
  10.  graphics2D.Clear(bg);
  11.  Dimension d = new Dimension(screenWidth, screenHeight);
  12.  int gridWidth = d.Width / 2;
  13.  int gridHeight = d.Height / 6;
  14.  
  15.  int x = 5;
  16.  int y = 7;
  17.  int rectWidth = gridWidth - 2 * x;
  18.  int stringY = gridHeight - 3 - 2 - 16;
  19.  int rectHeight = stringY - y - 2;
  20.  graphics2D.Draw(pen, new Line(x, y + rectHeight - 1,
  21.   x + rectWidth, y));
  22.  x += gridWidth;
  23.  graphics2D.Draw(pen, new Rectangle(x, y, rectWidth,
  24.   rectHeight));
  25.  x += gridWidth;
  26.  x = 5;
  27.  y += gridHeight;
  28.  stringY += gridHeight;
  29.  graphics2D.Draw(pen, new RoundRectangle(x, y, rectWidth,
  30.    rectHeight, 10, 10));
  31.  x += gridWidth;
  32.  graphics2D.Draw(pen, new Arc(x, y, rectWidth, rectHeight, 90,
  33.    135, Arc.Open));
  34.  x = 5;
  35.  y += gridHeight;
  36.  stringY += gridHeight;
  37.  graphics2D.Draw(pen, new Ellipse(x, y, rectWidth, rectHeight));
  38.  x += gridWidth;
  39.  // draw GeneralPath (polygon)
  40.  int[] x1Points = { x, x + rectWidth, x, x + rectWidth };
  41.  int[] y1Points = { y, y + rectHeight, y + rectHeight, y };
  42.  Path polygon = new Path(Path.WindEvenOdd,
  43.    x1Points.Length);
  44.  polygon.MoveTo(x1Points[0], y1Points[0]);
  45.  for (int index = 1; index < x1Points.Length; index++)
  46.  {
  47.   polygon.LineTo(x1Points[index], y1Points[index]);
  48.  }
  49.  polygon.ClosePath();
  50.  graphics2D.Draw(pen, polygon);
  51.  x = 5;
  52.  y += gridHeight;
  53.  stringY += gridHeight;
  54.  int[] x2Points = { x, x + rectWidth, x, x + rectWidth };
  55.  int[] y2Points = { y, y + rectHeight, y + rectHeight, y };
  56.  Path polyline = new Path(Path.WindEvenOdd,
  57.    x2Points.Length);
  58.  polyline.MoveTo(x2Points[0], y2Points[0]);
  59.  for (int index = 1; index < x2Points.Length; index++)
  60.  {
  61.   polyline.LineTo(x2Points[index], y2Points[index]);
  62.  }
  63.  graphics2D.Draw(pen, polyline);
  64.  x += gridWidth;
  65.  graphics2D.SetPenAndBrush(pen, brush);
  66.  graphics2D.Fill(null,
  67.   new Rectangle(x, y, rectWidth, rectHeight));
  68.  graphics2D.Draw(null,
  69.   new Rectangle(x, y, rectWidth, rectHeight));
  70.  x = 5;
  71.  y += gridHeight;
  72.  stringY += gridHeight;
  73.  Color[] colors = new Color[] { red, white };
  74.  int[] fractions = new int[] { 0, 255 };
  75.  LinearGradientBrush redtowhite =
  76.    new LinearGradientBrush(x, y, x + rectWidth, y,
  77.    fractions, colors, Brush.NoCycle);
  78.  graphics2D.SetPenAndBrush(pen, redtowhite);
  79.  graphics2D.Fill(null, new RoundRectangle(x, y,
  80.   rectWidth,rectHeight, 10, 10));
  81.  graphics2D.Draw(null, new RoundRectangle(x, y,
  82.   rectWidth,rectHeight, 10, 10));
  83.  x += gridWidth;
  84.  graphics2D.SetPenAndBrush(pen, brush);
  85.  graphics2D.Fill(null, new Arc(x, y, rectWidth,
  86.   rectHeight, 90,135, Arc.Chord));
  87.  graphics2D.Draw(null, new Arc(x, y, rectWidth,
  88.   rectHeight, 90,135, Arc.Chord));
  89.  x = 5;
  90.  y += gridHeight;
  91.  stringY += gridHeight;
  92.  int[] x3Points = { x, x + rectWidth, x, x + rectWidth };
  93.  int[] y3Points = { y, y + rectHeight, y + rectHeight, y };
  94.  Path filledPolygon = new Path(Path.WindEvenOdd,
  95.    x3Points.Length);
  96.  filledPolygon.MoveTo(x3Points[0], y3Points[0]);
  97.  for (int index = 1; index < x3Points.Length; index++)
  98.  {
  99.   filledPolygon.LineTo(x3Points[index], y3Points[index]);
  100.  }
  101.  filledPolygon.ClosePath();
  102.  graphics2D.SetPenAndBrush(pen, brush);
  103.  graphics2D.Fill(null, filledPolygon);
  104.  graphics2D.Draw(null, filledPolygon);
  105.  
  106. }