23.3.4.2 Freehand drawing with Paint

Do not expect too much from this project, because the Paint class is designed more for use in Paint scripts than for freehand drawing on a suitable drawing area such as a DrawingArea.

The basic idea for a sufficiently continuous freehand line comes from Tobias Boege. It consists of remembering the point from the last MouseMove event and, instead of setting individual points, connecting the last point with the point of the current event using a straight line segment (distance) with LineTo().

These are the results, the dynamics of which you will only experience when you start the programme yourself:

B1

Figure 23.3.4.2.1: Start screen …

B2

Figure 23.3.4.2.2: Spring with tulip by H.L. from O. 2015

With this source text, you too can become a short and small art painter:

' Gambas class file 
 
Private bStart As Boolean 
Private i As Integer 
Private pLast As PointF 
 
Public Sub Form_Open()  
  FMain.Center()
  FMain.Resizable = False 
 
  txtMouseX.Mouse = Mouse.Blank   '-- TextBox without mouse pointer
  txtMouseY.Mouse = Mouse.Blank  
  btnClose.Mouse = Mouse.Pointing '-- Mouse constant (hand) 
  btnClear.Mouse = Mouse.Custom 
  btnClear.Cursor = New Cursor(Picture["Mauszeiger/erase.png"]) '-- Customised mouse image
 
' The default mouse tip is the top left corner of the mouse image 
' With x=0 and y=15, the mouse tip lies on the pencil lead
 
  DrawingArea1.Mouse = Mouse.Custom 
  DrawingArea1.Cursor = New Cursor(Picture["icon:/16/pen"], 0, 15) 
 
  Paint.Begin(DrawingArea1) 
    Paint.Brush = Paint.Color(Color.Red) 
    Paint.Font = Font["Monospace, 38, bold"] 
  ' Text centred on the drawing page in the specified rectangle
    Paint.DrawText(("Angst vor Mäusen?"), 0, 35, DrawingArea1.W, DrawingArea1.H, Align.Top) 
  Paint.End()
  Timer1.Start()
End
 
Public Sub DrawingArea1_MouseMove() 
  If bStart = True Then 
     bStart = False 
     DrawingArea1.Clear 
  Endif
 
  Timer2.Start() 
  txtMouseX.Text = Mouse.X 
  txtMouseY.Text = Mouse.Y 
  If Mouse.Left = True Then 
     Paint.Begin(DrawingArea1) 
     If Mouse.X>5 And Mouse.X<DrawingArea1.Width - 5 And Mouse.Y>5 And Mouse.Y<DrawingArea1.Height - 5 Then 
        SetPoint(Mouse.X, Mouse.Y, Color.Blue) 
     Else 
        pLast = Null 
     Endif 
     Paint.End 
  Endif
 
End 
 
Public Sub DrawingArea1_MouseDown() 
  DrawingArea1_MouseMove() 
End
 
Public Sub DrawingArea1_MouseUp() 
  Timer2.Stop() 
  pLast = Null 
End
 
Public Sub SetPoint(X As Integer, Y As Integer, cColor As Integer) 
  If Not pLast Then 
     Paint.FillRect(X, Y, 2, 2, cColor) 
  Else 
     Paint.MoveTo(pLast.X, pLast.Y) 
     Paint.LineTo(X, Y) 
     Paint.LineWidth = 2 
     Paint.Background = cColor 
     Paint.Stroke() 
  Endif 
  pLast = New PointF(X, Y) 
End
 
Public Sub btnClear_Click() 
  DrawingArea1.Clear 
  txtMouseX.Text = "0" 
  txtMouseY.Text = "0" 
End
 
Public Sub Timer1_Timer()      
' The fastest mouse in Gambas Land... Speedy Gamzales
  Paint.Begin(DrawingArea1) 
    Paint.Translate(10, 155) 
    Paint.Scale(1, -1) 
    Paint.AntiAlias = False 
      Paint.Brush = Paint.Color(Color.DarkGray) 
      Paint.Arc(10 + i, 30, 4) 
      Paint.Fill()
      Paint.Brush = Paint.Color(&HFFFFDF) 
      Paint.Arc(10 + (i - 8), 30, 4) 
      Paint.Fill() 
      i = i + 8 
      DrawingArea1.Refresh()     
    Paint.AntiAlias = True 
  Paint.End()
  If i > DrawingArea1.Width Then 
     bStart = True 
     Timer1.Stop()
  Endif 
End 
 
Public Sub btnClose_Click() 
  FMain.Close() 
End

Download

Chapter & Projects

Download