|
Post by Binary1248 on Jul 25, 2020 16:58:59 GMT
Trying to learn some simple graphics based on code from examples: The attached prgm creats lines and random dots. Liberty V4.5.1 version The listing has 3 line numbers to help where my questions are. 10, 20, 30 #10 How can I code a button in the #mywindow1 to allow keyboard action input ? #20 What does the term "down" in this statement, and what other terms work ? #30 Is there a way to print inside #mywindow1 directly on the window ? . Now I need to go back and see how to add attachment. Fractels003.bas (2.72 KB) . Well that seems strange, so here is the code: This got reformatted, not pretty now ? 'Highly modified, original coolFractal.bas contributed by Rod
'amended by klaactu
'Modified for PEH as learning tool in graphics
' #1 is the screen name, renamed to hjk1 just for test
nomainwin
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
'+++ Not used for now, useful later +++++++++++++++++++++++++++++++++
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)
midx=int(WindowWidth/2)
midy=int(WindowHeight/2)
'++++++++++++++++++++++++++++++++++++++++++++++++++++++
'========== revisit later to get working button =====================================
10 button #mywindow1, "Clear", [clear], LR, 50, 10
'$$$$Not neede for now, $$$$$ jumps or calls label Clear, cleans any previous screen, WHAT is LR, 50, 10
'BUTTON #handle.ext, "label", returnVar, corner, x, y {, width, height}
'====================================================================================
open "Just my header name Fractzals002" for graphics_nsb as #mywindow1
#mywindow1 "trapclose [quit]" 'this sends the prgm to exit on window closing
lpcnt=1 'init loop count for multiple line draws
[clear]
20 #mywindow1 "down ; fill black"
' What is 'down' and what other cmds instead of down, I understand the fill
' color black, or color red, or others
30 'print #mywindow1.txtEdit, " 5 sec delay now"
'print #mywindow1 " 5 sec delay now"
'Hmm, print cmd doesn't work in graphics mode ?
'for j=1 to 10000 'about 5 sec delay
' for k= 1 to 300
' next k
'next j
'
[draw]
'calculate and draw the points
lpcnt=lpcnt+10
scan 'check for kb inputs ??? kb interrupt ???'
for j= 1 to 400
xp=1+j+lpcnt
yp=1+j
'delay between dots
for delayx = 1 to 100
next delayx
#mywindow1 "color "; "red"
#mywindow1 "set ";xp;" ";yp
next j
'NOW DO A GREEN (or other color) line
for j= 1 to 800
xp=lpcnt+j 'add offset for multiple draws
yp=700-j
#mywindow1 "color "; "green"
#mywindow1 "set ";xp;" ";yp
for delayx = 1 to 100
next delayx
next j
'Now draw horz line across top
'NOW DO A Blue (or other color) line
for j= 1 to 800
xp=j 'add offset for multiple draws, later
yp=lpcnt
#mywindow1 "color "; "yellow"
#mywindow1 "set ";xp;" ";yp
for delayx = 1 to 100
next delayx
next j
'random points now, as new lines are drawn, the old lines will begin to fad as
' random seq writes over them point by point
for a = 1 to 100000 'lots of points of lights or we will never see them, they are tiny !
xp= int(rnd(1)*1000) '+ 1
yp= int(rnd(1)*1000) '+ 1
#mywindow1 "color "; "blue"
#mywindow1 "set ";xp;" ";yp
next a
goto [draw]
[quit]
close #mywindow1
end
. .
|
|
|
Post by honkytonk on Jul 25, 2020 17:18:39 GMT
[clear] #mywindow1 "down ; fill black" wait ----------- "down" it is the lowering of the pen on the paper (essential for drawing). There is also: "when leftbutton Down" in association with mx=MouseX: my=MouseY Which gives the click position ------------ For delay: gosub [pause] ....... [pause] timer temp, [temps] 'temp en millisecondes wait [temps] timer 0 return
|
|
|
Post by B+ on Jul 25, 2020 18:07:45 GMT
Paul here is the perfect pause SUB
sub pause mil 'tsh version has scan built-in t0 = time$("ms") while time$("ms") < t0 + mil : scan : wend 'scans so if you quit on pause no problem end sub
Store it at the end of your code where SUBs and FUNCTIONs go. When you need to pause or delay CALL pause 2000 ' for 2 secs CALL pause 200 ' for .2 secs CALL pause 20 ' for .02 secs
|
|
|
Post by Rod on Jul 25, 2020 19:42:56 GMT
This got reformatted, not pretty now ?
If you are posting code use the [c] tags but also be very sure you are on the Post Quick Reply box or the BBCode pane if editing. If you use the preview pane the code will be double spaced.
Looping timers are not my favorite. They make the program unresponsive. Just do timer x,[branch] then wait. Then if something interrupts it can be handled. If you are looping nothing else gets a look in. Scan helps but your program is still doing nothing else while you pause. It could be doing a stack of drawing or computing while you pause.
If you can find the Graphics Command list in the help file there are hundreds of graphic commands that will draw or react to keyboard or mouse events. Orchestrating these events provides massive power over your program flow. Like the Timer, Events need managed off and on when needed.
|
|
|
Post by tsh73 on Jul 25, 2020 20:39:56 GMT
#10
10 button #mywindow1, "Clear", [clear], LR, 50, 10 puts upuir button too low, likely under taskbar (you asked WindowHeight = DisplayHeight, it does not account for taskbar)
If you do
open "Just my header name Fractzals002" for graphics_nsb_fs as #mywindow1 instead of graphics_nsb then
10 button #mywindow1, "Clear", [clear], LR, 50, 10 will work as expected.
#20 as was said, you will not get any drawing until you put pen down
#30 sure we way is print #mywindow1, "\";"anything to print" (or you could use "|" instead of "\" - without it, any repeating "\" will act as a newline)
BUT it prints to past place your drawing pen end to (or placed with print #handle, "place Xnumber Ynumber" ) And if you print as a first thing (no draw), initial position will be (0,0) - upper left And print will go ABOVE THAT
So simplest thing is make a new line with extra "\"
print #mywindow1, "\\";" 5 sec delay now"
|
|
|
Post by Binary1248 on Jul 25, 2020 20:50:55 GMT
Thanks honkytonk, B+, Rod it all helps. As to loop timers, yes they are sloppy and crude and I will use your suggestions, but when writing and testing I often temporarily fall back to my early days of coding. Rod, I am still trying to understand your method of showing code. I used the boxed C next to the Quote button, but I guess none of those are appropriate. When editing I got close spaced lines, various colors of text that make the code easier to read. Example the three line numbers I referenced to my questions were bright red and stood out. I think I also tried "Add Attachment" button but the pretty formatting the editor presents was lost. I will try some more methods of attaching code after I look thru the treads and see how others are posting. Still learning at 81 years, so be patient. . Thanks tsh83, your post showed up just after I posted the above. Will try all your suggestions.
|
|
|
Post by Binary1248 on Jul 25, 2020 21:26:20 GMT
OK, the three code items I asked about now work and will be implemented if needed. As to posting code, I guess the nice colors shown in the Liberty editor just don't transfer over, but the line spacing can be close as Rod explained. Many thanks guys. Now back to working on code I really don't need, but it is a brain massage that I need. Paul
|
|
|
Post by B+ on Jul 26, 2020 17:37:20 GMT
Trying to make some sense with paulironhorse code I ran into a problem myself.
The following code works fine without the button, press <spacebar> and screen clears, press <enter> and draw redraws screen works fine until you try the button. The first time it clears and you can redraw with <enter> anytime after that it bugs out with error about finding clearScreen.
nomainwin
'fix work area so easily fits inside screen WindowWidth = 700 +8 '700 pixels work area plus border this assumes no menu WindowHeight = 700 + 32 '700 pixels work area plus border this assumes no menu
UpperLeftX = int((DisplayWidth-WindowWidth)/2) '>>>>>> this IS used UpperLeftY = int((DisplayHeight-WindowHeight)/2)'>>>>>>> this IS used
'+++ Not used for now, useful later +++++++++++++++++++++++++++++++++ midx=int(WindowWidth/2) midy=int(WindowHeight/2) '++++++++++++++++++++++++++++++++++++++++++++++++++++++
'========== revisit later to get working button =====================================
10 button #mywindow1, "Clear", [clearScreen], LR, 50, 10 '<<<<<<<<<<< made clear a SUB called clearScreen but that didn't work either '$$$$Not neede for now, $$$$$ jumps or calls label Clear, cleans any previous screen, WHAT is LR, 50, 10 'BUTTON #handle.ext, "label", returnVar, corner, x, y {, width, height} '====================================================================================
open "Just my header name Fractzals002" for graphics_nsb as #mywindow1 #mywindow1 "trapclose [quit]" 'this sends the prgm to exit on window closing
' SETUP FOR KEYPRESSES #mywindow1 "when characterInput charIn" '<<<<<<<<<<<<<<< This sets up for keyboard input #mywindow1 "setfocus" ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< need this too for keypresses to go to window
#mywindow1 "down; fill black" 'down only needs to done once for drawing unless doing turtle drawing
call draw wait
[clearScreen] 20 #mywindow1 "down ; fill black" ' fill black is like cls screen with a color 30 'print #mywindow1.txtEdit, " 5 sec delay now" call pause 2000 '5 sec delay is awlful long here is 2 sec wait
[quit] close #mywindow1 end
sub draw 'calculate and draw the points scan 'check for kb inputs ??? kb interrupt ???' for j= 1 to 700 xp=j yp=j #mywindow1 "color "; "red" #mywindow1 "set ";xp;" ";yp next j 'NOW DO A GREEN (or other color) line for j= 1 to 700 xp=j 'add offset for multiple draws yp=700-j #mywindow1 "color "; "green" #mywindow1 "set ";xp;" ";yp next j 'Now draw horz line across top 'NOW DO A Blue (or other color) line for y= 1 to 700 step 10 xp=j 'add offset for multiple draws, later #mywindow1 "color "; "yellow" #mywindow1 "line ";0;" ";y;" ";700;" ";y next y 'random points now, as new lines are drawn, the old lines will begin to fad as ' random seq writes over them point by point for a = 1 to 1000 'lots of points of lights or we will never see them, they are tiny ! xp= int(rnd(1)*700) '+ 1 yp= int(rnd(1)*700) '+ 1 #mywindow1 "color "; "blue" #mywindow1 "set ";xp;" ";yp next a end sub
sub charIn H$, c$ ' H$ is window handle dont worry about it for 1 window c$ is charcter$ keypress if asc(c$) = 32 then ' if spacebar #H$ " fill black" else if asc(c$) = 13 then 'enter keypress call draw else if c$ = "Q" or c$ = "q" then close #mywindow1 end else 'ect.... end if end if end if end sub
sub pause mil 'tsh version has scan built-in t0 = time$("ms") while time$("ms") < t0 + mil : scan : wend end sub
Also I notice when I skip clear button and just do spacebar, enter, spacebar, enter... some kind of ghost screen or error message is appearing just before draw redraws the picture.
|
|
|
Post by Rod on Jul 26, 2020 20:02:18 GMT
Well its one of the reasons I don't like these timer loops. When you are in the timer sub you issue a SCAN statement. That looks for other events like button presses and hauls you out of the sub looking for [clearscreen] which is a branch label in the main program and does not exist in the sub. So it can't be found.
|
|
|
Post by B+ on Jul 26, 2020 20:17:46 GMT
Well its one of the reasons I don't like these timer loops. When you are in the timer sub you issue a SCAN statement. That looks for other events like button presses and hauls you out of the sub looking for [clearscreen] which is a branch label in the main program and does not exist in the sub. So it can't be found. Thanks now I get it. OK so I comment out the CALL pause (which I didn't like either) THEN I click the Clear button, clears but the <Enter> button to call draw won't work. Oh I know, setfocus! [clearScreen] 20 #mywindow1 "down ; fill black" ' fill black is like cls screen with a color 30 'print #mywindow1.txtEdit, " 5 sec delay now" 'call pause 2000 '5 sec delay is awlful long here is 2 sec #mywindow1 "setfocus" wait
fixed! Any idea what that yellow box artifact is just before draw is finished redrawing (after you hit <enter> there is a yellow box flash in screen before the draw drawing appears).
|
|
|
Post by Rod on Jul 27, 2020 8:35:34 GMT
The artifact you see is a Windows thing. Its there because Just BASIC has no way to sync with the screen refresh. So the screen gets refreshed part way through our drawing, that's the artifact, then the screen refreshes again by then the whole drawing has been completed. Windows is in charge of when a window gets repainted and its a bit random, not even 60Hz.
In LB5 Carl is giving us a behind the screen buffer and a mechanism to force a screen redraw. That will make a big difference because part drawn graphics will never appear, you will only ever see the finished graphic. So no more flashing and flickering just rock steady graphics.
|
|
|
Post by Rod on Jul 27, 2020 8:44:32 GMT
When graphics drawing there are a few things to think about. Drawing uses memory, you need to manage that memory use and you can also make it work in your favour.
Here I use down just once and fill black just once. Then I repeatedly clear old drawing memory use with discard and then reuse the previous segment named "bac" to redraw the background. It could be a much more complex background.
#mywindow1 "down; fill black ; flush bac" 'down only once, fill black , save as segment "bac"
#mywindow1 "discard ; redraw bac" 'dump old drawing from memory, redraw the black background, all instead of fill black again
|
|
|
Post by Rod on Jul 27, 2020 11:35:26 GMT
Here is my take on how to write the program using all subs.
nomainwin 'our window needs to be bigger than the pixels we want to see 'windows needs room for the title bar and the frame WindowWidth = 700+8 WindowHeight = 700+32 'center the window UpperLeftX = int((DisplayWidth-WindowWidth)/2) UpperLeftY = int((DisplayHeight-WindowHeight)/2) button #1, "Clear", clearScreen, LR, 50, 10 'open a graphic window with no scroll bars or resizing frame open "Just Demo" for graphics_nsb_nf as #1
'either use all [branchlabel] or all Subs, less mixups that way #1 "trapclose quit" 'start event trapping for keypresses #1 "when characterInput charIn" 'down only once, fill black only once , save as segment "bac" #1 "down; fill black ; flush bac"
call draw wait
sub clearScreen h$ 'this event handler is passed the handle of the screen 'we use it by prepending # #h$ "discard ; redraw bac" #h$ "setfocus" 'button press takes focus away from screen so set focus back to screen end sub
sub quit h$
'this event handler is passed the handle of the screen 'we actually use it by prepending # close #h$ end end sub
sub draw 'calculate and draw the points #1 "color red ; line 0 0 699 699" #1 "color green ; line 699 0 0 699" #1 "color yellow" 'set once for y= 0 to 699 step 10 #1 "line 0 ";y;" 699 ";y next #1 "color blue" 'set once for a = 1 to 10000 #1 "set ";rnd(0)*700;" ";int(rnd(0)*700/10)*10 next end sub
sub charIn h$, c$ 'this event handler is passed the handle of the screen and the key pressed 'here we just pass the handle on, we dont actually use it select case case c$ = chr$(32) call clearScreen h$ 'spacebar case c$ = chr$(13) call clearScreen h$ ' enter call draw case c$ = "Q" or c$ = "q" call quit h$ end select end sub
|
|
|
Post by B+ on Jul 27, 2020 15:35:15 GMT
Thanks Rod that was helpful for me specially about flush and discard.
|
|
|
Post by honkytonk on Jul 27, 2020 17:00:18 GMT
If you click on the closing cross while the program is in a sub-pause, this generates an error; that doesn't happen with a gosub [pause]
|
|