This was the family tree application, our first milestone. Assignment instructions can be found at Spring 2000 Project Milestones. Here is a screen shot to get us started.
Missing File (/cs2340/uploads/PlugFam.gif)
The displayed tree is my family line at my fraternity, and the code in the workspace is the code used to create that tree. This Tree class is a graphical display of an n-ary tree, in which you can store any object. Instances of trees can be created and then graphically seen in a form. Here is the code itself to create the tree.
Here is the code for the Tree Class itself, it has 5 variables. They store the Ordered Collection of kids, the person's name, where they are horizontally, vertically, and how far down the line they are. In actuality this should maybe be called a branch class, because they way mine works is that each node only knows itself and its children. It still works because that nodes children knows their children who know their children and all the way down.
"This will add a Node to the tree, where someKid is the node to add. It adds at the end of the childlist; meaning that it will display the first Node on the left and the last node on the right under their parent. "
myKids add: someKid.
"This method returns the Node at the value represented by aNode"
^ myKids at: aNode.
" This sets the content of the current tree, particularly the instance variable myName. "
myName := anObject.
"Graphically displays the tree."
"Returns the first kid in the list."
^ myKids first.
"Returns the last kid in the list."
^ myKids last.
These are the private classes, or classes the others use.
anchor: X at: Y
"This method sets the horizontal and vertical values, which are instance variables in Tree."
hspot := X.
vspot := Y.
"This method allows us to access the instance variable hspot."
"This method allows us to access the instance variable vspot."
"Used to initialize the values of the tree."
myKids := OrderedCollection new. "sets instance variable myKids to a new OrderedCollection."
self anchor: 150 at: 10. "anchors hspot to 150 and vspot to 10."
depth := 1. "sets depth to 1, which is later used to determine how far down the tree we are."
"Responsible for acutally graphically putting the tree together."
self printSelf. "Prints the content of its name first."
((myKids size) 1) ifFalse: [ "If no children don't execute"
| index |
self setChildren. "sets the spots for its children"
self printLegs. "Prints the lines that will lead to their children."
index := 1. "uses recursion to call makeTree on itself and uses index"
"to keep track of which kid is being made."
(myKids size) timesRepeat:
[(myKids at: (index)) makeTree.
index := index + 1. ].
"Graphically produces the lines from parent to their children."
myPen := Pen new. "creates an instance of pen."
index := 1.
"Executes as many times as there are kids. Places the pen at hspot and a little below vspot ( this
is so the lines draw below the content and not through it), and proceeds to draw the lines to
their representative spots."
| displayName |
(myName isNil) ifTrue: [myName := '']. "If there is no name, print nothing."
displayName _ myName asString. "Displays content as a string."
"Display it to the right of hspot a little to help center, and right on vspot"
displayName displayAt: ((hspot) - 25) @ (vspot).
"Transcript show: myName; cr." "This is a Debug tool, currently commented out."
"This method takes the initialized hspot and vspot of a node, and sets it down to where it should properly fall in the tree. This method does nothing graphically though, it only creates imaginary placeholders for its children to be used later in printing."
horz vert gap bigGap index
gap := 120 (1/depth). " how far between names. The depth variator in there, allows for the
tree to get smaller as it gets deeper, to help with display room."
bigGap := ( (gap) ((myKids size) - 1) ). " the total width needed to display all of the names"
vert := (vspot) + 50 . " how far in between vertical levels."
horz := ( (hspot) - ((bigGap)/2) - (gap) ). " Sets Left hand side of drawing legs."
index := 1. " Tracks which kid we are setting up"
"Loops the same number of times as there are kids, so that it can go through and re-anchor each child in an appropiate location. It also sets the depth one level deeper."