Streamlining Chapter 07 - Use a single graphics box for the resistor display
Jump to navigation
Jump to search
by StPendl (talk) 14:57, 2 September 2020 (UTC)
Since we have now got rid of a bunch of controls, we further reduce the number of controls by removing the individual graphics boxes, which make up the display of the resistor.
For this we introduce some global variables to keep track of the current color selection:
' define variables to be remembered between calling the sub GLOBAL Band1value, Band2value, Band3value, Band4value GLOBAL ColorValue1$, ColorValue2$, ColorValue3$, ColorValue4$ ' set initial band color to body color ColorValue1$ = "21 228 255" ColorValue2$ = ColorValue1$ ColorValue3$ = ColorValue1$ ColorValue4$ = ColorValue1$
Further we replace the seven resistor display graphics boxes:
'Selected Bands of Body Resistance graphicbox #w.Band1, 155,470,15,35 graphicbox #w.Band2, 185,470,15,35 graphicbox #w.Band3, 215,470,15,35 graphicbox #w.Band4, 255,470,15,35 stylebits #w.Band1, 0,_WS_BORDER,0,0 stylebits #w.Band2, 0,_WS_BORDER,0,0 stylebits #w.Band3, 0,_WS_BORDER,0,0 stylebits #w.Band4, 0,_WS_BORDER,0,0 'General Body Resistance graphicbox #w.37, 130,470,168,35 stylebits #w.37, 0,_WS_BORDER,0,0 'Terminal Wires graphicbox #w.42, 80,485,50,5 graphicbox #w.43, 300,485,50,5
With a single resistor display graphics box:
'General Body Resistance graphicbox #w.resistor, 80,470,270,35 stylebits #w.resistor, 0,_WS_BORDER,0,0
We also have to introduce the code to draw the resistor based on the users selection in the mouse event handler subroutine:
'Draw Resistor #w.resistor "cls;down;home;posxy CenterX CenterY;fill "; BackgroundColor$ BoxWidth = CenterX*2 BoxHeight = CenterY*2 'Draw Wire #w.resistor "color 127 127 127;backcolor 127 127 127" #w.resistor "place 0 "; CenterY-2; ";boxfilled "; BoxWidth; " "; CenterY+2 'Draw Body #w.resistor "color 21 228 255;backcolor 21 228 255" #w.resistor "place 50 0;boxfilled "; BoxWidth-50; " "; BoxHeight 'Draw Bands #w.resistor "color "; ColorValue1$; ";backcolor "; ColorValue1$ #w.resistor "place 70 0;boxfilled 85 "; BoxHeight #w.resistor "color "; ColorValue2$; ";backcolor "; ColorValue2$ #w.resistor "place 100 0;boxfilled 115 "; BoxHeight #w.resistor "color "; ColorValue3$; ";backcolor "; ColorValue3$ #w.resistor "place 130 0;boxfilled 145 "; BoxHeight #w.resistor "color "; ColorValue4$; ";backcolor "; ColorValue4$ #w.resistor "place 160 0;boxfilled 175 "; BoxHeight #w.resistor "flush"
The complete code now looks like this.
'Resistor Calculator.bas 'Author: salaion Yahoo! Group 'Date: 27.11.10 ' ' List of Colors at http://en.wikipedia.org/wiki/List_of_colors ' ' 27.11.10 00:18:50 - Initial as posted ' 27.11.10 00:21:40 - added array for colors and reduced redundant code for color boxes ' 27.11.10 00:34:07 - replaced duplicate command strings by variables ' 27.11.10 02:22:16 - consolidated event handler ' ' always take the time for some sleep or you will make things worse ;-) ' ' 27.11.10 10:56:24 - removed radio buttons ' 27.11.10 11:53:34 - used single graphics box for resistor nomainwin WindowWidth = 820:WindowHeight = 600 UpperLeftX=int( (DisplayWidth- WindowWidth) /2) UpperLeftY=int( (DisplayHeight- WindowHeight) /2) BackgroundColor$ = "green" ' define variables to be remembered between calling the sub GLOBAL Band1value, Band2value, Band3value, Band4value GLOBAL ColorValue1$, ColorValue2$, ColorValue3$, ColorValue4$ ' set initial band color to body color ColorValue1$ = "21 228 255" ColorValue2$ = ColorValue1$ ColorValue3$ = ColorValue1$ ColorValue4$ = ColorValue1$ dim Colors$(10,2), Tolerances$(3,2) [Colors] DATA "Black", " 0 0 0" DATA "Brown", "170 100 20" DATA "Red", "255 0 0" DATA "Orange", "255 180 0" DATA "Yellow", "255 255 0" DATA "Green", " 45 157 40" DATA "Blue", " 0 0 255" DATA "Violet", "170 0 180" DATA "Gray", "127 127 127" DATA "White", "255 255 255" [Tolerances] DATA "Gold", "212 175 55" DATA "Silver", "200 200 250" DATA "None", " 21 228 255" 'Fill the arrays RESTORE [Colors] FOR i = 1 to 10 READ ColorName$, ColorValue$ Colors$(i,1) = ColorName$ Colors$(i,2) = ColorValue$ NEXT RESTORE [Tolerances] FOR i = 1 to 3 READ ColorName$, ColorValue$ Tolerances$(i,1) = ColorName$ Tolerances$(i,2) = ColorValue$ NEXT STATICTEXT #w.Ohm, "" , 500, 200, 300, 20 STATICTEXT #w.Tole,"" , 500, 230, 300, 20 STATICTEXT #w,Colors$( 1,1), 10, 42, 50, 20 STATICTEXT #w,Colors$( 2,1), 10, 82, 50, 20 STATICTEXT #w,Colors$( 3,1), 10, 122, 50, 20 STATICTEXT #w,Colors$( 4,1), 10, 162, 50, 20 STATICTEXT #w,Colors$( 5,1), 10, 202, 50, 20 STATICTEXT #w,Colors$( 6,1), 10, 242, 50, 20 STATICTEXT #w,Colors$( 7,1), 10, 282, 50, 20 STATICTEXT #w,Colors$( 8,1), 10, 322, 50, 20 STATICTEXT #w,Colors$( 9,1), 10, 362, 50, 20 STATICTEXT #w,Colors$(10,1), 10, 402, 50, 20 STATICTEXT #w,Tolerances$(1,1), 420, 42, 50, 20 STATICTEXT #w,Tolerances$(2,1), 420, 82, 50, 20 STATICTEXT #w,Tolerances$(3,1), 420, 122, 50, 20 STATICTEXT #w,"K=Kilo " , 470, 300,100, 20 STATICTEXT #w,"M=Mega " , 470, 330,100, 20 STATICTEXT #w,"Light Blue is The Default Color of Resistance " , 470, 360,400, 20 'Band1 STATICTEXT #w, "First Digit", 70, 8, 80, 20 groupbox #w, "", 70, 20, 70,420 graphicbox #w.Black1 , 90, 40, 30, 30 graphicbox #w.Brown1 , 90, 80, 30, 30 graphicbox #w.Red1 , 90,120, 30, 30 graphicbox #w.Orange1, 90,160, 30, 30 graphicbox #w.Yellow1, 90,200, 30, 30 graphicbox #w.Green1 , 90,240, 30, 30 graphicbox #w.Blue1 , 90,280, 30, 30 graphicbox #w.Violet1, 90,320, 30, 30 graphicbox #w.Gray1 , 90,360, 30, 30 graphicbox #w.White1 , 90,400, 30, 30 'Band2 STATICTEXT #w, "Second Digit", 150, 8, 90, 20 groupbox #w, "", 160, 20, 70,420 graphicbox #w.Black2 , 180, 40, 30, 30 graphicbox #w.Brown2 , 180, 80, 30, 30 graphicbox #w.Red2 , 180,120, 30, 30 graphicbox #w.Orange2, 180,160, 30, 30 graphicbox #w.Yellow2, 180,200, 30, 30 graphicbox #w.Green2 , 180,240, 30, 30 graphicbox #w.Blue2 , 180,280, 30, 30 graphicbox #w.Violet2, 180,320, 30, 30 graphicbox #w.Gray2 , 180,360, 30, 30 graphicbox #w.White2 , 180,400, 30, 30 'Band3 STATICTEXT #w, "Multiplier", 250, 8, 80, 20 groupbox #w, "", 250, 20, 70,420 graphicbox #w.Black3 , 270, 40, 30, 30 graphicbox #w.Brown3 , 270, 80, 30, 30 graphicbox #w.Red3 , 270,120, 30, 30 graphicbox #w.Orange3, 270,160, 30, 30 graphicbox #w.Yellow3, 270,200, 30, 30 graphicbox #w.Green3 , 270,240, 30, 30 graphicbox #w.Blue3 , 270,280, 30, 30 graphicbox #w.Violet3, 270,320, 30, 30 graphicbox #w.Gray3 , 270,360, 30, 30 graphicbox #w.White3 , 270,400, 30, 30 'Band4 STATICTEXT #w, "Tolerance", 340, 8, 80, 20 groupbox #w, "", 340, 20, 70,420 graphicbox #w.Gold4 , 360, 40, 30, 30 graphicbox #w.Silver4, 360, 80, 30, 30 graphicbox #w.None4 , 360,120, 30, 30 'Fram graphicbox #w.title, 488,53,306,44 stylebits #w.title, 0,_WS_BORDER, 0,0 'General Body Resistance graphicbox #w.resistor, 80,470,270,35 stylebits #w.resistor, 0,_WS_BORDER,0,0 open "Resistor Calculator" for window_nf as #w #w "trapclose [quit]" #w "font Times_New_Roman 13" #w.title "cls;down;color blue;backcolor yellow;size 15;place 0 0;boxfilled 306 44" #w.title "font Times_New_Roman 16 bold;color red" #w.title "place 13 28;\Resistor Color Code of 4 Bands" #w.title "flush" FOR Band = 1 to 3 FOR Color = 1 to 10 Handle$ = "#w."; Colors$(Color,1); Band #Handle$ "cls;down;fill "; Colors$(Color,2); ";flush" #Handle$ "when leftButtonUp MouseClick" NEXT NEXT FOR Tolerance = 1 to 3 Handle$ = "#w."; Tolerances$(Tolerance,1); 4 #Handle$ "cls;down;fill "; Tolerances$(Tolerance,2); ";flush" #Handle$ "when leftButtonUp MouseClick" NEXT ' apply initial settings call MouseClick "#w.Black1", 0, 0 call MouseClick "#w.Black2", 0, 0 call MouseClick "#w.Black3", 0, 0 call MouseClick "#w.None4", 0, 0 [none] WAIT [quit] close #w END SUB MouseClick Handle$, PosX, PosY ' parse handle of calling control Band = val(Right$(Handle$, 1)) ColorName$ = Mid$(Handle$, 4, Len(Handle$)-4) ' get position of color FOR i = 1 to 10 if Colors$(i,1) = ColorName$ then exit for if i < 4 then if Tolerances$(i,1) = ColorName$ then exit for NEXT ' if we had no match exit If i = 11 Then Exit Sub Select Case Band Case 1 Band1value = (i-1) * 10 ColorValue1$ = Colors$(i,2) Case 2 Band2value = i-1 ColorValue2$ = Colors$(i,2) Case 3 Band3value = 10 ^ (i-1) ColorValue3$ = Colors$(i,2) Case 4 Band4value = i*5 if Band4value = 15 then Band4value = 20 ColorValue4$ = Tolerances$(i,2) Case Else Exit Sub End Select 'Draw Resistor #w.resistor "cls;down;home;posxy CenterX CenterY;fill "; BackgroundColor$ BoxWidth = CenterX*2 BoxHeight = CenterY*2 'Draw Wire #w.resistor "color 127 127 127;backcolor 127 127 127" #w.resistor "place 0 "; CenterY-2; ";boxfilled "; BoxWidth; " "; CenterY+2 'Draw Body #w.resistor "color 21 228 255;backcolor 21 228 255" #w.resistor "place 50 0;boxfilled "; BoxWidth-50; " "; BoxHeight 'Draw Bands #w.resistor "color "; ColorValue1$; ";backcolor "; ColorValue1$ #w.resistor "place 70 0;boxfilled 85 "; BoxHeight #w.resistor "color "; ColorValue2$; ";backcolor "; ColorValue2$ #w.resistor "place 100 0;boxfilled 115 "; BoxHeight #w.resistor "color "; ColorValue3$; ";backcolor "; ColorValue3$ #w.resistor "place 130 0;boxfilled 145 "; BoxHeight #w.resistor "color "; ColorValue4$; ";backcolor "; ColorValue4$ #w.resistor "place 160 0;boxfilled 175 "; BoxHeight #w.resistor "flush" vv=(Band1value+Band2value)*Band3value Select Case Case vv>=1000000 #w.Ohm "Resistance Value=";vv/1000000;" M Ohm" Case vv>=1000 #w.Ohm "Resistance Value=";vv/1000;" K Ohm" Case Else #w.Ohm "Resistance Value=";vv;" Ohm" End Select if Band4value>0 then #w.Tole "Tolerance Value =";" +/- ";Band4value;"%" END SUB
- Streamlining Chapter 01 - Starting the mission
- Streamlining Chapter 02 - Using arrays to reduce redundant code
- Streamlining Chapter 03 - Use variables for duplicate command strings
- Streamlining Chapter 04 - Consolidate event handlers
- Streamlining Chapter 05 - Apply mouse selection
- Streamlining Chapter 06 - Remove radio buttons
- Streamlining Chapter 07 - Use a single graphics box for the resistor display
- Streamlining Chapter 08 - Further reduce the amount of GUI controls (Using hot-spots)
- Streamlining Chapter 09 - Adding some eye-candy
- Streamlining Chapter 10 - Summary