Gambas/Graphics

From Wikibooks, open books for an open world
< Gambas
Jump to: navigation, search

Back to Gambas

If you want to produce some graphics in gambas you should get used to the drawingarea control.

Draw a Line[edit]

This little program will draw a line. You have to start a new graphics project. You take a form as your start form. You need a drawingarea and a commandbutton on the form to get it going.

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

When you want to draw some more lines, then try this example:

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

Some other lines are drawn here:

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

When you want to manipulate the width of your line , then try this example:

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

If you want to change the color of your line to white, then try this example:

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

The next small example will draw a box and fill it with white color.

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

The Spiral Program[edit]

This program shows you a nice spiral. There is no scale command in Gambas (at least, not yet). Therefore the x and y coordinates are transformed in the program. The coordinate system ranges from xmin = - 2 to xmax = 2 and ymin = - 2 to ymax = 2.

You need a Drawingarea and a Commandbutton to get the example going.

How does this look ? See [1]

The code:

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