跳轉至內容

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
華夏公益教科書