分享

笔记二:画形状

 百眼通 2014-10-31


Graphics用来绘图。

用draw来划线。vb.net没有画点的功能。

可以画直接、矩形、椭圆、Bezier、弧、饼(封闭的弧线)。

要注意的是:弧、饼用的是度数(不是弧度),且是顺时间转的角度。



  1. Imports System.Drawing  
  2. Imports System.Math  
  3. Public Class Form1  
  4.     Const pi As Double = 3.14159  
  5.   
  6.     '画点(用一个像素的矩形或两个像素的直接来模拟  
  7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
  8.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  9.         Dim j As Int32 = 50  
  10.         For i As Int32 = 10 To 150  
  11.             gr.DrawLine(Pens.Red, i, j, i + 1, j + 1)  
  12.             j += 1  
  13.         Next  
  14.     End Sub  
  15.   
  16.     '用直线构成图  
  17.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
  18.         PictureBox1.Refresh() '清除  
  19.   
  20.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  21.         Dim x1, y1, x2, y2, d As Short '起点终点、基准距离  
  22.         Dim b, c As Double '真实距离  
  23.   
  24.         d = 60  
  25.         For a = 0 To 2 * pi Step pi / 360  
  26.             b = d * (1 + 1 / 4 * Cos(12 * a))  
  27.             c = b * (1 + Sin(4 * a))  
  28.             x1 = 150 + Int(c * Cos(a))  '(150,180)中心点  
  29.             y1 = 180 + Int(c * Sin(a))  
  30.             x2 = 150 + Int(c * Cos(a + pi / 5))  
  31.             y2 = 180 + Int(c * Sin(a + pi / 5))  
  32.             gr.DrawLine(Pens.BlueViolet, x1, y1, x2, y2)  
  33.         Next  
  34.     End Sub  
  35.   
  36.     '椭圆或圆  
  37.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click  
  38.         PictureBox1.Refresh()  
  39.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  40.         gr.DrawEllipse(Pens.Red, New Rectangle(0, 0, 100, 80))  
  41.         gr.DrawEllipse(Pens.Aqua, New Rectangle(120, 120, 80, 80))  
  42.     End Sub  
  43.   
  44.     '椭圆弧  或圆弧 (是对上面椭圆的部分提取)  
  45.     '注意:开始和结束角度是以度为单位,且顺时针方向,下面是从30度到120度  
  46.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click  
  47.         PictureBox1.Refresh()  
  48.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  49.         gr.DrawArc(Pens.Red, New Rectangle(20, 20, 150, 200), 30, 120)  
  50.     End Sub  
  51.   
  52.     '封闭椭圆弧线,即饼图  
  53.     '参数与arc同,只是成图时是封闭的  
  54.     Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click  
  55.         PictureBox1.Refresh()  
  56.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  57.         gr.DrawPie(Pens.Red, New Rectangle(20, 20, 150, 200), 30, 120)  
  58.     End Sub  
  59.   
  60.     '贝赛尔曲线 Bezier  
  61.     '它由四个点:起点、两个中间点(控制)、终点 组成  
  62.     Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click  
  63.         PictureBox1.Refresh()  
  64.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  65.         gr.DrawBezier(Pens.Brown, 10, 20, 140, 40, 40, 240, 200, 270)  
  66.         gr.DrawRectangle(Pens.Red, New Rectangle(200, 40, 80, 170))  
  67.     End Sub  
  68.   
  69.     '多边形--不规则形状  
  70.     '由多个点来决定,多个点用Points()数组来表达  
  71.     Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click  
  72.         PictureBox1.Refresh()  
  73.         Dim gr As Graphics = PictureBox1.CreateGraphics  
  74.         Dim p(2) As System.Drawing.Point  
  75.   
  76.         p(0).X = 30 : p(0).Y = 30  
  77.         p(1).X = 100 : p(1).Y = 100  
  78.         p(2).X = 50 : p(2).Y = 250  
  79.         gr.DrawPolygon(Pens.DarkGreen, p)  
  80.     End Sub  
  81. End Class  








    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多