How to Use the Spirograph
Setting-up Spirograph
To run Spirograph in the lab under Windows NT, do the following:
-
Open an MS DOS Command Prompt (via the Start Button or a shortcut).
It should open to your home directory and display the prompt "U:\yourusername>".
-
Create a directory from which to run Spirograph by entering: mkdir
Spirograph
Then, change to that directory by typing: cd Spirograph
-
From this directory, you can now run Spirograph by typing: java Spirograph
at the prompt. (This will invoke the Sun Java interpreter and
load the Spirograph application, which has already been provided for you.)
When you start up the Spirograph program, two windows will be displayed.
The area with the two large text boxes in it is the code editing area,
in which you will write statements that will control the Spirograph. The
window with the dot in the center is the Spirograph itself. You may
want to move the windows so that you can see both windows at the same time.
Editing Code: The Basics
If you press the Start button in the Spirograph window, you will notice
that nothing happens. This is because the motion rules of the Spirograph
have not been loaded yet. To create a rule, type into the appropriate
text box in the Code Editor, and then press the "Load Horizontal (or Vertical)
Code" button. This will generate a .java file using your code, compile
it into a .class file, and then use this code as the corresponding motion
rule for the Spirograph. (Note: the Spirograph has been designed
so you can load new code even while it is running. However, for more
reliable operation, we recommend that you press the "Stop" button before
compiling, and then press "Start" again after compiling.)
Messages during compilation are displayed in the MS DOS window from
which you initially ran Spirograph. If there are no errors in your
code, a message should appear that says, "Compilation successful."
Otherwise, error messages from the Java compiler will be displayed. You
can then edit your code, and try again.
Try typing a simple statement such as return 10; and compiling
it. Try typing an erroneous statement such as return "Ten" and
observe what happens. (What is wrong with the latter return
statement?) Load simple rules for both axes, and start the Spirograph.
Can you make the dot go to position (10, 20)?
Using the Horizontal and Vertical Rules
If you click on the button that says "Advanced Programing Options" in the
code editing area a window will appear with five buttons to click on. We'll
start by looking at the top three of these buttons. If you click on the
button that says, "Use Horizontal rule for both rules." The vertical text
area and "Load" button will be grayed out. The next time you press
the "Load" button, any code you enter in the Horizontal text area will
now be used to control both the horizontal and vertical motion of the dot.
You can also choose to use the Vertical rule instead for both rules.
To go back to using both rules, click on the button that says "Use seperate
rules", and then click on both "Load" buttons.
Using Names in Your Code
System-Provided Names (Parameters)
In your code, you may make use of the following names, which are
of type double:
-
pos - contains the position
of the dot along the axis where this rule applies.
-
vel - contains the velocity
of the dot along the axis where this rule applies.
-
otherPos - contains the position of the dot
along the other (perpendicular) axis.
-
otherVel - contains the velocity of the dot
along the axis.
-
maxPos - contains the maximum and minimumallowable positions
for the dot along the axis where this rule applies. +maxPos
represents the rightmost (or topmost) edge of the Spirograph, and -maxPos
represents the leftmost (or bottommost) edge of the Spirograph.
For example, if a rule is being used as the horizontal rule, then pos
would return its x coordinate, and vel would return its
velocity in the x direction.
Temporary Storage Names (Variables)
You can also create your own names by declaring them in your code.
These names can be used to hold values throughout a single application
of a rule. The next time the rule is applied, however, the values
of these variables are lost. These types of names (also called variables
or local variables) are useful for providing temporary storage during
computations.
e.g., double nextPos;
nextPos = pos * 2;
return nextPos;
Long-Term Storage Names (Fields)
Spirograph also allows you to create names whose values are preserved across
different applications of a rule. These are called fields.
To create a field, pop up the "Advanced Programing Options" menu and click
on a button to edit either the Horizontal or Vertical fields. Another small
text area should appear in the Code Editing window. You can declare and/or
define fields there. (If your window gets messed-up, just resize
it by dragging a side or corner of the window, and it should fix itself.)
Note that you can initialize a field by using a definition (a declaration
combined with an assignment). In general, it is considered good practice
to initialize your fields to a reasonable initial value.
Example:
Fields: int myInt = 5;
Rule: myInt = myInt + 1;
return myInt;
(Can you predict what this code would do? Think about it first,
then try it.)
Using the Spirograph
At this point you should be ready to write your own code to control the
Spirograph. Try to write code that will produce interesting patterns, and
see if you can predict ahead of time what the results of your code will
be. To use the Spirograph, make sure you have loaded your code, and
compiled it successfully. Then, press the "Start" button on the Spirograph
window, and watch the dot move. You may stop the dot by pressing
the "Stop" button. The "Reset" button allows you to do such things
as clear the lines drawn by the Spirograph, and/or put the ball back in
the center.
Using the Advanced Environment Options
The Spirograph has several Advanced Environment Options.
Wall Properties
You can enable or disable certain properties of the Spirograph window's
edge.
-
Circular mode makes the boundaries of the Spirograph into a circle or ellipse.
When used with bouncing, it can create interesting effects. (However,
for the lab exercises, we will not use circular mode.)
-
When bouncing is turned on, a dot "hitting" a wall (i.e., going beyond
the boundaries of the Spirograph) will be sent back with its velocity negated.
-
When wraparound is turned on, a dot "hitting" a wall will "wraparound"
to the other side of the window and continue moving with the same velocity.
Wraparound mode cannot be used together with bouncing or circular mode.
-
By the default, none of these are turned on. In this case, a dot
hitting a wall be stopped by the wall (i.e., it will not be allowed to
have coordinates beyond the walls, but it does not wraparound and its velocity
is unchanged).
Control Mode
You can select whether the values returned by the rules are used to determine
position, velocity, or acceleration. Note the following:
-
Selecting Position control will set the velocity, and acceleration of the
ball to 0. (However, this does not mean that you cannot implement
velocity and acceleration by using the rules themselves to move the ball.
-
Selecting Velocity control will set the acceleration to 0. (Again
it does not mean that you cannot implement acceleration.)
-
In order to keep the ball from flying off the screen, there is an imposed
"speed limit" of 60. If the ball is in velocity or acceleration control
mode, and the velocity exceeds the speed limit, then its velocity will
be automatically adjusted to a level below the limit.
-
By default, Spirograph starts in Position control mode.
Mouse Mode
You can choose to use the mouse to reposition the dot or to add gravity
sources. Or, you may choose not to use the mouse at all. By
default, the mouse lets you reposition the dot in the Spirograph.
Adding gravity sources is an interesting advanced feature which we will
not use for the lab exercises. You are welcome to play around with
them yourself, however.
Velocity
These entry boxes allow you to set the velocity of the dot. Enter
the desired values first, then press "Set Velocity" to actually effect
the change.
Managing Your Code
Spirograph does not have explicit File management features. However,
there are several ways to save your work, if you want to.
The Rule.java files
The text that you type in the Code Editor window is taken by Spirograph
and inserted into a template that looks something like this:
public class HorizRule implements Accelerator {
// Fields code goes here
public double act (double pos, double vel, double otherPos, double
otherVel, double maxPos) {
// Rule code goes here.
}
}
Everytime you press the "Load" button, Spirograph generates either HorizRule.java
or VertRule.java (depending on which rule you're loading), and
then tries to compile it. The .java file is stored in the directory
from which you ran Spirograph, and remains there even after you exit Spirograph.
Thus, you can do whatever you want with this file, such as, copy it, rename
it, print it, etc.
When you run Spirograph, it checks for the existence of these files,
and loads the Code Editor window with the appropriate code from these files,
if there is any. Thus, one way to save and load back files would
be as follows:
-
Run Spirograph and use it to write and test your code.
-
When you are satisfied with your work, quit Spirograph and rename or copy
the HorizRule.java and VertRule.java files that it generated onto files
with a different name.
-
To load them back, simply copy or rename them back to HorizRule.java and
VertRule.java and run Spirograph.
Another way to maintain different versions of your code is to use the commenting
features of the Java language. In Java, a anything to the right of
a // is considered a comment and is not compiled.
The intent of this feature is to allow programmers to document their code
with useful comments. However, you can also use this feature to allow
you to get different behaviors from a single source code file. Specifically,
you can have a file that contains code for several different behaviors,
and with all but the desired behavior commented out. Although this
maybe a useful trick for simple and short code snippets such as what you
will be writing for this Spirograph Laboratory, please note that in general,
doing this is not really good programming practice, as it can result in
messy and hard-to-read code.
Go back to the main page.