跳转至内容

Gambas/Graphics

来自维基教科书,开放的书籍,用于开放的世界

返回 Gambas

如果你想在 Gambas 中生成一些图形,你应该熟悉 drawingarea 控件。

画一条线

[编辑 | 编辑源代码]

这个小程序将画一条线。你需要创建一个新的图形项目。你需要一个 drawingarea 和一个 commandbutton 才能开始。你需要在表单上放置一个 drawingarea 和一个 commandbutton。

 PUBLIC SUB Button1_Click()
 Draw.Begin(DrawingArea1)
 Draw.Line(1, 130, 500, 400)
 Draw.End
 END

如果你想绘制更多线条,请尝试以下示例

 PUBLIC SUB Form_Open()
 DIM B AS Integer 
 Draw.Begin(DrawingArea1)
 FOR B = 1 TO 200 STEP 10 
  Draw.Line(1, B, 500, B)
 NEXT 
 Draw.End
 END

这里绘制了一些其他线条

 PUBLIC SUB Button1_Click()
 DIM B AS Integer 
 Draw.Begin(DrawingArea1)
 ' Draws a line horizontally across the centre of the form
 Draw.Line (0, ME.Height / 2, ME.Width, ME.Height / 2)
 ' Draws a line vertically down the centre of the form
 Draw.Line (ME.Width / 2, 0,ME.Width / 2, ME.Height)
 ' Draws a line from the top left, to the bottom right
 Draw.Line (0, 0,ME.Width, ME.Height)
 ' Draws a line from the top right, to the bottom left
 Draw.Line (ME.Width, 0,0, ME.Height)
 Draw.End
 END

如果你想操作线条的宽度,请尝试以下示例

 PUBLIC SUB Form_Open()
 DIM B AS Integer 
 Draw.Begin(DrawingArea1)
 Draw.Line(10,100, 20, 100)
      FOR B = 1 TO 100 STEP 10 
       Draw.LineWidth=B 
       Draw.Line(10+B,100, 20+B, 100)
      NEXT 
 Draw.End
 END

如果你想将线条的颜色更改为白色,请尝试以下示例

 Draw.Begin(DrawingArea1)
 Draw.ForeColor = &HFFFFFF
 Draw.Line(1, 130, 500, 400)
 Draw.End

下面的示例将绘制一个方框并用白色填充它。

 PUBLIC SUB Button1_Click()
 Draw.Begin(DrawingArea1)
  Draw.FillColor = &HFFFFFF
  draw.FillStyle = 1
  'draw.ForeColor = &HFFFFFF the outline will be white also
  Draw.Rect (100, 100,200,200)
 Draw.End
 END

螺旋程序

[编辑 | 编辑源代码]

这个程序向你展示一个漂亮的螺旋。Gambas 中没有比例尺命令(至少目前还没有)。因此,程序中转换了 x 和 y 坐标。坐标系范围从 xmin = - 2 到 xmax = 2,ymin = - 2 到 ymax = 2。

你需要一个 Drawingarea 和一个 Commandbutton 才能开始运行该示例。

它看起来如何?请参见 [1]

代码

 PUBLIC SUB Button1_Click()
 DIM dymax AS Integer
 DIM dymin AS Integer 
 DIM ymax AS Integer
 DIM ymin AS Integer
 DIM y AS Float
 DIM dy AS Float
 DIM dyi AS Integer
 DIM dxmax AS Integer
 DIM dxmin AS Integer 
 DIM xmax AS Integer
 DIM xmin AS Integer
 DIM x AS Float
 DIM dx AS Float
 DIM dxi AS Integer
 DIM k AS Float 
 DIM a AS Float 
 DIM w AS Float
 dymax = DrawingArea1.Height
 dymin = 0 
 ymax = 2
 ymin = -2
 dxmax = DrawingArea1.Width
 dxmin = 0 
 xmax = 2
 xmin = -2 
 'x-axis is drawn 
 FOR x = xmin TO xmax STEP 0.005
 y = 0 
 dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
 dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
 dyi = Fix(dy)
 dxi = Fix(dx) 
 Draw.Begin(DrawingArea1)
 Draw.Point(dxi,DrawingArea1.Height- dyi)
 Draw.End
 NEXT 
 'y - Axis is drawn
 FOR y = ymin TO ymax STEP 0.005
 x = 0 
 dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
 dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
 dyi = Fix(dy)
 dxi = Fix(dx) 
 Draw.Begin(DrawingArea1)
 Draw.Point(dxi,DrawingArea1.Height- dyi)
 Draw.End
 NEXT 
 'Here the spiral starts
 FOR k = -100 TO 150 STEP 0.5
 a = 0.97
 'Distance from 0,0 to the point 
 w = 0.15
 'Angle under whiche the point is seen from the origin
 a = a ^k 
 w = w * k 
 x = a * Cos(w)
 'x coordinate of each point
 y = a * Sin(w)
 'y coordinate of each point
 dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
 dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
 dyi = Fix(dy)
 dxi = Fix(dx) 
 Draw.Begin(DrawingArea1)
 Draw.Point(dxi,DrawingArea1.Height- dyi)
 Draw.End
 NEXT
 END
华夏公益教科书