Why is this code running twice?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why is this code running twice?

Fernando Cabral
I am trying to create a sundial faceplate. I took the analogic clock,
discarded what I did not need and thought I was ready to work on the
calculations and drawing of the hour angles.

Nevertheless, I am mystified by two things I can't understand:
a) The code produces a loop I can't see why (I am not referring to the FOR
loop)
b) I can't modify the drawing area dimensions ( dwgArea.width = 800  does
not work)

Besides, I have a question: when a drawing area is created, it (0,0)
supposed to be the central point?

The code follows:

























*Private Const W As Integer = 2048Private Const H As Integer = 2048Public
Sub DwgArea_Draw()  Dim angle As Float  Dim eScale As Float  Dim i As
Integer  Print "Executing" & Now  dwgArea.width = 800 ' does nothing
dwgArea.Height = 1000 ' does nothing  Print dwgArea.width; dwgArea.Height
Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)   Paint.Brush =
Paint.Color(Color.SetAlpha(Color.Red, 128))   Paint.LineWidth = 1    For i
= -15 To 195 Step 15   angle = Rad(CFloat(i))   Paint.MoveTo(0, 0)
Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)   Paint.Stroke
Paint.Fill  NextEnd*



--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: [hidden email]
Facebook: [hidden email]
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype:  fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Cam Era
Fernando,

is this the code?

*Private Const W As Integer = 2048
Private Const H As Integer = 2048
Public Sub DwgArea_Draw()
Dim angle As Float
Dim eScale As Float
Dim i As Integer
Print "Executing" & Now
dwgArea.width = 800 ' does nothing
dwgArea.Height = 1000 ' does nothing
Print dwgArea.width; dwgArea.Height
Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)
Paint.Brush = Paint.Color(Color.SetAlpha(Color.Red, 128))
Paint.LineWidth = 1
For i = -15 To 195 Step 15
  angle = Rad(CFloat(i))
  Paint.MoveTo(0, 0)
  Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)
  Paint.Stroke
  Paint.Fill
Next
End*

Does the whole DwgArea_Draw() routine get called twice?

Can you set some breakpoints (F9) and check the calling stack to see what's
going on?


On Wed, Aug 2, 2017 at 11:38 AM, Fernando Cabral <
[hidden email]> wrote:

> I am trying to create a sundial faceplate. I took the analogic clock,
> discarded what I did not need and thought I was ready to work on the
> calculations and drawing of the hour angles.
>
> Nevertheless, I am mystified by two things I can't understand:
> a) The code produces a loop I can't see why (I am not referring to the FOR
> loop)
> b) I can't modify the drawing area dimensions ( dwgArea.width = 800  does
> not work)
>
> Besides, I have a question: when a drawing area is created, it (0,0)
> supposed to be the central point?
>
> The code follows:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *Private Const W As Integer = 2048Private Const H As Integer = 2048Public
> Sub DwgArea_Draw()  Dim angle As Float  Dim eScale As Float  Dim i As
> Integer  Print "Executing" & Now  dwgArea.width = 800 ' does nothing
> dwgArea.Height = 1000 ' does nothing  Print dwgArea.width; dwgArea.Height
> Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)   Paint.Brush =
> Paint.Color(Color.SetAlpha(Color.Red, 128))   Paint.LineWidth = 1    For i
> = -15 To 195 Step 15   angle = Rad(CFloat(i))   Paint.MoveTo(0, 0)
> Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)   Paint.Stroke
> Paint.Fill  NextEnd*
>
>
>
> --
> Fernando Cabral
> Blogue: http://fernandocabral.org
> Twitter: http://twitter.com/fjcabral
> e-mail: [hidden email]
> Facebook: [hidden email]
> Telegram: +55 (37) 99988-8868
> Wickr ID: fernandocabral
> WhatsApp: +55 (37) 99988-8868
> Skype:  fernandojosecabral
> Telefone fixo: +55 (37) 3521-2183
> Telefone celular: +55 (37) 99988-8868
>
> Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
> nenhum político ou cientista poderá se gabar de nada.
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Fabien Bodard-4
Ok Fernando ... your code is no so bad :-)


First never call the resizing of the caneva in the drawing event as it
call itself the drawing event ... (normally if I rememember well it
have been disabled)
So you must resize in another place.

then I don't really understand why you are resizing the drawingarea
and not the windows... but you must have your idea.

You need to call DrawingArea.Refresh Function to force the refresh so
the _Draw event calling. Or resising the form, or anything else that
force the need to refresh the area.


For example if you want to make a clock you do :

'Where timer delay is 1000 for example
Public sub MyTimer_Timer()

$dCurTime = GetCurTime()
dwgClock.Refresh

End



==> the code revised

' Gambas class file

Private Const W As Integer = 2048
Private Const H As Integer = 2048

Public Sub _new()

    dwgArea.width = 800 ' does nothing
  dwgArea.Height = 1000 ' does nothing  Print dwgArea.width; dwgArea.Height
  dwgArea.Refresh
End



Public Sub DwgArea_Draw()

  Dim angle As Float
  Dim eScale As Float
  Dim i As Integer

  Print "Executing" & Now

  Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)
  Paint.Brush = Paint.Color(Color.SetAlpha(Color.Red, 128))
  Paint.LineWidth = 1

  For i = -15 To 195 Step 15

    angle = Rad(CFloat(i))
    Paint.MoveTo(0, 0)
    Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)
    Print Cos(angle) * w / 2
    Paint.Stroke
    'Paint.Fill
  Next

End


Best Regards,

Fabien Bodard

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Adrien Prokopowicz-2
In reply to this post by Fernando Cabral
Le Wed, 02 Aug 2017 03:38:33 +0200, Fernando Cabral  
<[hidden email]> a écrit:

> I am trying to create a sundial faceplate. I took the analogic clock,
> discarded what I did not need and thought I was ready to work on the
> calculations and drawing of the hour angles.
>
> Nevertheless, I am mystified by two things I can't understand:
> a) The code produces a loop I can't see why (I am not referring to the  
> FOR
> loop)

By default, DrawingAreas are not cached, which means it will need to  
redraw its
contents a few times depending on the situation (most likely on startup,  
or when
the window is resized). The docs[0] talk about that a little bit.

If you do not want this to happen, you need to set the Cached[1] property  
to True,
which will make the DrawingArea store the image in memory instead of  
redrawing it
every time.
In practice, this means two things :

- Since the whole contents of the DrawingArea is stored, it will increase  
the memory
   usage a bit, however since nowadays every CPU and GPU has access to more  
than 1GB
   of memory, I would say it is negligible.
- If you want to actually update the drawing, you will have to manually  
call the
   Refresh() method[2].

> b) I can't modify the drawing area dimensions ( dwgArea.width = 800  does
> not work)

We would neet your full project to be sure, but most likely your  
DrawingArea is
inside a Form (or another container) that resizes its children, making  
fixed
coordinates useless.

If you want a fixed size window, you should try to resize the Form itself,
however I would recommend to let the user resize the window as they want,
and calculate the size of your drawing by reading the DrawingArea's
ClientWidth and ClientHeight properties.

However, depending on your drawing this may be a little tricky, so for  
small
experiments having a fixed-sized window is okay too. :-)

>
> Besides, I have a question: when a drawing area is created, it (0,0)
> supposed to be the central point?
>

No. Unlike math where the origin (0,0) is usually represented in the  
center,
the origin of screen coordinates is always located in the top-left corner.

If you want the actual center of your DrawingArea, you will have to use
the following coordinates :

x = DrawingArea.Width / 2
y = DrawingArea.Height / 2

Hope this helps. :-)

Regards,

[0] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea
[1] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea/cached
[2] http://gambaswiki.org/wiki/comp/gb.qt4/drawingarea/refresh

--
Adrien Prokopowicz

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Fernando Cabral
2017-08-02 7:05 GMT-03:00 Adrien Prokopowicz <[hidden email]>:


Le Wed, 02 Aug 2017 03:38:33 +0200, Fernando Cabral <

> [hidden email]> a écrit:
>
> Besides, I have a question: when a drawing area is created, it (0,0)
>> supposed to be the central point?
>>
>>
> No. Unlike math where the origin (0,0) is usually represented in the
> center,
> the origin of screen coordinates is always located in the top-left corner.
>
> If you want the actual center of your DrawingArea, you will have to use
> the following coordinates :
>
> x = DrawingArea.Width / 2
> y = DrawingArea.Height / 2
>

Problem is: this simple instruction "Paint.MoveTo(0, 0)" moves the cursor
to the center
of the drawing area.  I expected it to move it to the left superior corner.
I can't figure why.

Regards

- fernando




--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: [hidden email]
Facebook: [hidden email]
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype:  fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Fernando Cabral
In reply to this post by Fabien Bodard-4
2017-08-02 6:40 GMT-03:00 Fabien Bodard <[hidden email]>:

> Ok Fernando ... your code is no so bad :-)
>
> I know. Some explanations bellow.

>
> First never call the resizing of the caneva in the drawing event as it
> call itself the drawing event ... (normally if I rememember well it
> have been disabled)
> So you must resize in another place.
>
> then I don't really understand why you are resizing the drawingarea
> and not the windows... but you must have your idea.
>

This was only a test (as other lines). I am trying to figure out how the
code works.
I got the original code from the software farm (analogic clock) and then I'
ve been pruning it what I need.

But this assignement, besides doing nothing, is not the reason for the loop.
I know so because I can delete it and the behaviour is the same.

>
> You need to call DrawingArea.Refresh Function to force the refresh so
> the _Draw event calling. Or resising the form, or anything else that
> force the need to refresh the area.
>

You mean, for the resizing to take place?


>
>
> For example if you want to make a clock you do :
>
> 'Where timer delay is 1000 for example
> Public sub MyTimer_Timer()
>
> $dCurTime = GetCurTime()
> dwgClock.Refresh
>
> End
>
>
> Yep! That's the functionality the original code had. Nevertheless,
the only thing I want (and I am trying to learn how to do) is to draw some
lines
at certain angles inside a square or a rectangle. (the angles you see in
the
for loop is only for testing the concept. Real angles will be a different).

The "Print "executing" & Now' is also only for testing purpose. I suspected
the code
was in loop but could figure out why. So I placed this printing thing there
to confirm.
And, yes, it prints continuously changing the time.

>
> ==> the code revised
>
> ' Gambas class file
>
> Private Const W As Integer = 2048
> Private Const H As Integer = 2048
>
> Public Sub _new()
>
>     dwgArea.width = 800 ' does nothing
>   dwgArea.Height = 1000 ' does nothing  Print dwgArea.width; dwgArea.Height
>   dwgArea.Refresh
> End
>
>
>
> Public Sub DwgArea_Draw()
>
>   Dim angle As Float
>   Dim eScale As Float
>   Dim i As Integer
>
>   Print "Executing" & Now
>
>   Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)
>   Paint.Brush = Paint.Color(Color.SetAlpha(Color.Red, 128))
>   Paint.LineWidth = 1
>
>   For i = -15 To 195 Step 15
>
>     angle = Rad(CFloat(i))
>     Paint.MoveTo(0, 0)
>     Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)
>     Print Cos(angle) * w / 2
>     Paint.Stroke
>     'Paint.Fill
>   Next
>
> End
>
>
> Best Regards,
>
> Fabien Bodard
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: [hidden email]
Facebook: [hidden email]
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype:  fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Fernando Cabral
In reply to this post by Fabien Bodard-4
2017-08-02 6:40 GMT-03:00 Fabien Bodard <[hidden email]>:

> Ok Fernando ... your code is no so bad :-)
>
>
> ==> the code revised
>
>
Still being executed twice. I have not been able to understand why.

Thanks.

- fernando


> ' Gambas class file
>
> Private Const W As Integer = 2048
> Private Const H As Integer = 2048
>
> Public Sub _new()
>
>     dwgArea.width = 800 ' does nothing
>   dwgArea.Height = 1000 ' does nothing  Print dwgArea.width; dwgArea.Height
>   dwgArea.Refresh
> End
>
>
>
> Public Sub DwgArea_Draw()
>
>   Dim angle As Float
>   Dim eScale As Float
>   Dim i As Integer
>
>   Print "Executing" & Now
>
>   Paint.Translate(dwgArea.W / 2, dwgArea.H / 2)
>   Paint.Brush = Paint.Color(Color.SetAlpha(Color.Red, 128))
>   Paint.LineWidth = 1
>
>   For i = -15 To 195 Step 15
>
>     angle = Rad(CFloat(i))
>     Paint.MoveTo(0, 0)
>     Paint.LineTo(Cos(angle) * W / 2, Sin(angle) * H / 2)
>     Print Cos(angle) * w / 2
>     Paint.Stroke
>     'Paint.Fill
>   Next
>
> End
>
>
> Best Regards,
>
> Fabien Bodard
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: [hidden email]
Facebook: [hidden email]
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype:  fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this code running twice?

Charlie
In reply to this post by Fernando Cabral
Fernando Cabral wrote
I am trying to create a sundial faceplate. I took the analogic clock,
discarded what I did not need and thought I was ready to work on the
calculations and drawing of the hour angles.

Nevertheless, I am mystified by two things I can't understand:
a) The code produces a loop I can't see why (I am not referring to the FOR
loop)
b) I can't modify the drawing area dimensions ( dwgArea.width = 800  does
not work)

Besides, I have a question: when a drawing area is created, it (0,0)
supposed to be the central point?
Have a look at the excellent program 'Painting' which is on the Farm under Example.

Check out www.gambas.one

Loading...