Calculator
|
Top Previous Next |
1. | Continue from the previous tutorial or load the file "button1.swi" and save as "mycalculator.swi"
|
2. | Insert a Text Object using the Text Tool or the Menu items Insert | Text. Name the new Text Object radius, tick the 'Target' checkbox and set to the text type to Input in the Text Panel
|
4. | Clear the 'Text' field, so the Object appears as an empty input field. Select right justification, as shown below:
|
5. | Create a Dynamic Text Object to display the result of the calculation by using the Text Tool or Insert | Text to insert a Text Object. Name the new Text Object "area", tick the 'Target' checkbox and set to the text type to Dynamic in the Text Panel. Clear the text so that an empty text field is displayed and select right justification. Use the Selection Tool to stretch the text field using the cross-shaped handles, as shown below. The advanced settings will be the same as those selected for the previous Text Object and do not need to be altered at this stage
|
6. | Use the Text Tool or Insert | Text to insert some Static text ("area" and "radius") to be used as headings
|
· | If the text has a line border around it, use the 'Advanced...' button to turn the border off
|
· | Although the new Text Objects appear in the 'Outline' Panel with the same names as the previously inserted Text Objects, a conflict will not occur at the Scripting level as the new Text Objects are not Scripting Objects (they are unnamed and do not have the 'Target' checkbox set)
|
7. | Use the Select Tool to re-arrange the Objects as shown below:
|
8. | Modify the on(press) Button Event function to calculate the area of the circle when the button is pressed. Select the button then select the 'Script' Panel. Right-click on the trace statement within the on (press) function and select Statements | name = expr;
|
For the 'Target:' parameter, select area (the Dynamic Text Object added in step 5) from the pull-down list.
|
For the 'Name:' parameter, select Text | text from the pull-down list.
|
Leave the operator as "=".
|
Enter the formula "Math.PI * radius.text * radius.text" in the formula area
|
9. | Test the calculator by reselecting the 'Layout' Panel and press the 'Play' button (fonts may appear to change because system fonts are used). Enter the number 2 into the 'radius' text field and click the '=' button. The following should be displayed in the 'Layout' Panel.
|
|