Using DrawArea - Simplified

Post

Posted
Rating:
#1 (In Topic #832)
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
This should not be so difficult.

I created a simple form (600x600) with only two objects.
- DrawingArea1
- Button1

Super simple… the idea is that if I click the button, it draws a line. Should be eazy-peasy… but I just don't understand what Gambas wants from me.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ' Variables
  4. Public x2 As Integer = 15
  5. Public y2 As Integer = 15
  6. Public mytask As Integer = 0
  7.  
  8. Public Sub Form_Open()
  9.  
  10.  
  11. Public Sub form_Close()
  12.  
  13.  
  14. Public Sub Button1_Click()
  15.  
  16.   mytask = 1
  17.   x1 = 100
  18.   y1 = 100
  19.   x2 = 150
  20.   y2 = 150
  21.   DrawingArea1_Draw
  22.  
  23.  
  24. Public Sub DrawingArea1_Draw()
  25.  
  26.   Draw.Begin(DrawingArea1)
  27.  
  28.   If mytask = 0 Then ' <-- This works (automatically)
  29.      Draw.Line(x1, y1, x2, y2)
  30.  
  31.   If mytask = 1 Then ' <-- This fails ( Can not paint outside of DRAW event handler )
  32.        Draw.Line(x1, y1, x2, y2)
  33.  
  34.  

When run, the routine sees that mytask = 0 and it draws a small line… Apparently the Public Sub DrawingArea1_Draw() runs automatically when the form opens. Ok… I get that.

When I click the button, I reset the variables and tell it to run the routine again… <COLOR color="#FF0000">crash and burn</COLOR>.

I was like… Ok… maybe I thought that I needed to put the  Draw.Begin(DrawingArea1) in the Public Sub Form_Open() and the Draw.End in the Public Sub Form_Close()… nope… <COLOR color="#BF0000">CRASH </COLOR>again.

If I can get Gambas to do something graphical when I press a button… then there is hope that I can do stuff on a slider change, or a timer activation… can someone help me please???
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
Hello,
1) the instruction "DrawingArea1_Draw" in Button1_Click() routine makes no sense, as it conforms to an Event.
However, in your case you have to use the "DrawingArea.Refresh" Method.

2) The instruction: "Draw.Begin (DrawingArea1)" is not necessary within the "DrawingArea" Event, in which you have to draw.

3) I would avoid using the "Draw" Class, as it has long been considered obsolete/deprecated. You should use the "Paint" Class.

So… I suggest:

Code (gambas)

  1. ......
  2. .....etc...
  3.  
  4. Public Sub Button1_Click()
  5.  
  6.   ...etc...
  7.   ...etc...
  8.   DrawingArea1.Refresh
  9.    
  10.  
  11. Public Sub DrawingArea1_Draw()
  12.  
  13.   With Paint
  14.     If mytask = 0 Then
  15.       .MoveTo(x1, y1)
  16.       .LineTo(x2, y2)
  17.     Endif
  18.     If mytask = 1 Then
  19.       .MoveTo(x1, y1)
  20.       .LineTo(x2, y2)
  21.     Endif
  22.     .Stroke
  23.     .End
  24.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
<COLOR color="#0000FF">VUOTT - Thank you!</COLOR>

That worked perfectly, and I see now the DrawingArea1.Refresh causes the drawing area to clear, and the subroutine DrawingArea1_Draw() is where all the graphics command must reside. Now I have a handle on it… I can make an array of points for a gauge arrow, then read a sensor in a timer function, update the coordinates with rotation, and send the focus to the DrawingArea1_Draw() routine where it will be updated.

I'll look into circle and fill commands, should be pretty simple now that I understand the process.  :D

Reference: /comp/gb.qt4/paint - Gambas Documentation

<COLOR color="#008000">Thanks again!</COLOR>
Jerry

I will update when I have something interesting to show.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
Simple question… Why isn't the pointer RED? I Used &H00<COLOR color="#BF0000">FF</COLOR><COLOR color="#004000">00</COLOR><COLOR color="#0000FF">00 </COLOR>which as I understand it is Alpha, <COLOR color="#800000">RED,</COLOR> <COLOR color="#008000">GRN</COLOR>, <COLOR color="#0000FF">BLU</COLOR> with alpha 00=100% Opaque.

Am I using the .COLOR command wrong?? (Well obviously… but why?)  :roll:

The screen consist of only 3 items…
<LIST>
  • <LI>Drawarea1 (500,500 pixels)
    Textbox1 (Shows Slider value)
    Slider1 (Min=0 Max=220)</LI>
</LIST>

Help! (Thanks)

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ' Variables
  4. Public x1 As Integer = 250
  5. Public y1 As Integer = 250
  6. Public x2 As Integer = 250
  7. Public y2 As Integer = 250
  8. Public mytask As Integer = 0
  9.  
  10. Public Sub Form_Open()
  11.  
  12. Slider1_Change ' <---- Paint initial slider state.
  13.  
  14.  
  15. Public Sub form_Close()
  16.  
  17.  
  18. Public Sub DrawingArea1_Draw()
  19.   Dim offset_angle As Integer = -210 ' Radians like to be at the 3-O-clock point, I want them to start someplace else.
  20.  
  21.   With Paint
  22.     x1 = 250
  23.     y1 = 250
  24.     .MoveTo(x1, y1)
  25.     x2 = Cos(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  26.     y2 = Sin(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  27.     .LineTo(x2, y2)
  28.     x2 = Cos(Rad(Slider1.value + offset_angle + 0)) * 200 + 250
  29.     y2 = Sin(Rad(Slider1.value + offset_angle + 0)) * 200 + 250
  30.     .LineTo(x2, y2)
  31.     x2 = Cos(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  32.     y2 = Sin(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  33.     .LineTo(x2, y2)
  34.     .LineTo(x1, y1)
  35.     '.Stroke <---------- REM this out to only fill the shape, not draw lines.
  36.     .Color(&H00CC0000)
  37.     .Fill
  38.     .End
  39.  
  40.  
  41. Public Sub Slider1_Change()  
  42.   TextBox1.Text = Slider1.value
  43.   DrawingArea1.Refresh
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
Thanks to a Spanish web page and the wonders of Google Translate… I got it now.

Code (gambas)

  1.   With Paint
  2.     .Brush = Paint.Color(&H00FFCC00)
  3.     x1 = 250
  4.     y1 = 250
  5.     .MoveTo(x1, y1)
  6.     x2 = Cos(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  7.     y2 = Sin(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  8.     .LineTo(x2, y2)
  9.     x2 = Cos(Rad(Slider1.value + offset_angle + 0)) * 200 + 250
  10.     y2 = Sin(Rad(Slider1.value + offset_angle + 0)) * 200 + 250
  11.     .LineTo(x2, y2)
  12.     x2 = Cos(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  13.     y2 = Sin(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  14.     .LineTo(x2, y2)
  15.     .LineTo(x1, y1)
  16.     '.Stroke <---------- REM this out to only fill the shape, not draw lines.
  17.     .Fill
  18.     .End

Now I  can do any color i want… and what is really cool… I can use this to pick my exact colors.
https://www.w3schools.com/colors/colors_picker.asp

It also looks like I can outline things too… pretty cool… I'm sure there is a way to specify an outlined and filled line with a simpler command… but at least this is working for me.

Code (gambas)

  1. Public Sub DrawingArea1_Draw()
  2.   Dim offset_angle As Integer = -210 ' Radians like to be at the 3-O-clock point, I want them to start someplace else.
  3.   Dim Pointer_LEN As Integer = 180
  4.  
  5.   With Paint
  6.     ' Make a filled shape for a pointer.
  7.     .Brush = Paint.Color(&H00FFCC00)
  8.     x1 = Cos(Rad(Slider1.value + offset_angle + 180)) * 30 + 250
  9.     y1 = Sin(Rad(Slider1.value + offset_angle + 180)) * 30 + 250
  10.     .MoveTo(x1, y1)
  11.     x2 = Cos(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  12.     y2 = Sin(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  13.     .LineTo(x2, y2)
  14.     x2 = Cos(Rad(Slider1.value + offset_angle + 0)) * Pointer_LEN + 250
  15.     y2 = Sin(Rad(Slider1.value + offset_angle + 0)) * Pointer_LEN + 250
  16.     .LineTo(x2, y2)
  17.     x2 = Cos(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  18.     y2 = Sin(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  19.     .LineTo(x2, y2)
  20.     .LineTo(x1, y1)
  21.     '.Stroke <---------- REM this out to only fill the shape, not draw lines.
  22.     .Fill
  23.    
  24.     ' Trace lines around the pointer we just made.
  25.     .Brush = Paint.Color(&H000000FF)
  26.     x1 = Cos(Rad(Slider1.value + offset_angle + 180)) * 30 + 250
  27.     y1 = Sin(Rad(Slider1.value + offset_angle + 180)) * 30 + 250
  28.     .MoveTo(x1, y1)
  29.     x2 = Cos(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  30.     y2 = Sin(Rad(Slider1.value + offset_angle + 135)) * 20 + 250
  31.     .LineTo(x2, y2)
  32.     x2 = Cos(Rad(Slider1.value + offset_angle + 0)) * Pointer_LEN + 250
  33.     y2 = Sin(Rad(Slider1.value + offset_angle + 0)) * Pointer_LEN + 250
  34.     .LineTo(x2, y2)
  35.     x2 = Cos(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  36.     y2 = Sin(Rad(Slider1.value + offset_angle + 225)) * 20 + 250
  37.     .LineTo(x2, y2)
  38.     .LineTo(x1, y1)
  39.     .Stroke
  40.     '.Fill  < - - - - - - - - - - REM this out To only draw the shape, lines not fill.
  41.    
  42.     .End
  43.  

So yeah… I can load SVG backgrounds, make programmable components… very exciting to be able to build NICE graphic panels now!!! Finally… so now if I can only learn to read the ADS1115 Chip with piGPIO… I'll have everything sorted out. Well… pretty much.  :D  :shock:  ;)
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
If all works… this is a sample file.

Attached:
Attachment

Feedback Welcome
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
I've made a lot of progress on the graphics and how to make really nice looking controls. Not sure if there is an interest or not… if there is, I'll zip up everything and put it here for you.
<IMG src="%5Battachment=0%5DControl%20Panel%20Demo.png%5B/attachment%5D"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
These are really nice. Just shows what can be done.

Minor point regarding using the Quit command see here.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
 I'll look into ways of ending/quitting/stopping the program.

Once I get this thing running… it will be for school events, robotics meetings, etc.
there won't… or shouldn't be much starting/stopping once we begin the event.

Ever since starting with the Raspberry Pi I've been looking for a way to make eye-catching programs… once I paired PIGPIO with GAMBAS3… I was home.

Jerry
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
seany is in the usergroup ‘Regular’
 Hi

Are your controls available under open source? I would love to use them for my work. Thank you.

PS: Is there a web repo of all available controls, made by community? Thank you
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’

seany said

Hi

PS: Is there a web repo of all available controls, made by community? Thank you

We have been discussing on the gambas mailing list about expanding the Farm to include modules/classes/components/controls/etc as well as whole gambas applications (Benoit included).

Benoit has a million things to do though so it may take a while but something is happening towards gambas having it's own list of community based custom things :)

This forum has a Component Showcase section but there is currently not a lot on it: Gambas One - Gambas ONE
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
Feel free to use anything I have created… I'm still learning.

If you know someone who has figured out the ADS1115 chip in Gambas… that would be incredibly helpful :-)

If you need me to zip up some files I can do that too.

Jerry
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
vuott is in the usergroup ‘Regular’

seany said

Is there a web repo of all available controls, made by community?
I suggest you also search the currently existing international forums of Gambas programmers.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
When I read through the documentation, it said the PAINT functions could be used in a DRAWINGAREA and/or the following..
<LIST>
  • <LI>
  • Picture</LI>
    <LI>
  • Image</LI>
    <LI>
  • DrawingArea</LI>
    <LI>
  • Printer</LI>
    <LI>
  • SvgImage</LI>
</LIST>


DRAWINGAREA works great… I make a PICTUREBOX and load in the SVG image… then I create a DRAWINGAREA and place it over the PICTUREBOX… think a sheet of glass laid on top of an image… then I can draw on it. But if I just create a PICTUREBOX and try to PAINT directly on it… I get errors.

Any ideas?

I mean for now… I'll keep dropping a DRAWINGAREA over the PICTUREBOX(s). I could have an array of them, with a single DRAWINGAREA over them all… then just adjust my math to draw where I like. I was just curious if someone got code working to do it without two separate layers.
Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
vuott is in the usergroup ‘Regular’
Do you mean that, if you use Class Paint directly with PictureBox, you get errors?

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#16
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
 If I create a DRAWINGAREA and paste some code on it… it works.

If I create a PICTUREBOX, then paste the exact same code… fails.
Online now: No Back to the top

Post

Posted
Rating:
#17
Regular
vuott is in the usergroup ‘Regular’
I don't know if I have understood correctly; however, as you yourself reported (https://forum.gambas.o…iewtopic.php?p=5889#p5889), it is not possible to use the Paint Class directly with a PictureBox.
So, you will need to use Paint with an Object of type "Image" or "Picture" (by using Paint.Begin() Method), and then load this Object into the PictureBox.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#18
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
 Ok, I'll stick with DRAWINGAREA… it works and doesn't take much effort to drop in as an overlay.

Thanks,
Jerry
Online now: No Back to the top

Post

Posted
Rating:
#19
Guru
BruceSteers is in the usergroup ‘Guru’
PictureBox is just not in the list of things Paint can operate on but the Picture it contains IS paintable so you must work on the picture not the PictureBox then set the PictureBox.Picture to your edit.

This will operate on a PictureBox.

it loads huge/color icon then superimposes the gambas icon on it.
then makes it the picturebox picture

Code (gambas)

  1.  
  2.  
  3. Public Sub Button1_Click()
  4.  
  5.   pPic = Picture["icon:/huge/color"]
  6.  
  7.   Paint.Begin(pPic)
  8.     Dim p As Picture = Picture["icon:/small/gambas"]
  9.     Paint.DrawPicture(p, 10, 10, p.W, p.H)
  10.   Paint.End
  11.  
  12. PictureBox1.Picture = pPic
  13.  
  14.  
  15.  
  16.  
Online now: No Back to the top

Post

Posted
Rating:
#20
Avatar
Regular
Askjerry is in the usergroup ‘Regular’
 BruceSteers… I'll tinker with this.
Thank you!
Online now: No Back to the top
1 guest and 0 members have just viewed this.