View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Plug This Family

Family Tree Milestone Case study.

This is Cheyenne Throckmorton's page of the
Plug This Development Team.


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.

Object subclass: #Tree
instanceVariableNames: 'myKids myName hspot vspot depth '
classVariableNames: ''
poolDictionaries: ''
category: 'Tree Classes'





These are the public methods I used.

addNode: someKid
"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.


at: aNode
"This method returns the Node at the value represented by aNode"
^ myKids at: aNode.

content: anObject
" This sets the content of the current tree, particularly the instance variable myName. "

myName := anObject.
^ myName.


display
"Graphically displays the tree."

Tree clearWorld.
self makeTree.

first
"Returns the first kid in the list."

^ myKids first.

last
"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.

getHspot
"This method allows us to access the instance variable hspot."

^hspot.

getVspot
"This method allows us to access the instance variable vspot."

^vspot.

initialize
"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."

makeTree
"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. ].

]

printLegs
"Graphically produces the lines from parent to their children."

myPen index "Local Variables"
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."

(myKids size) timesRepeat:
[myPen place: (hspot) @ ((vspot) + 15);
goto: ( (myKids at: (index)) getHspot) @ ( (myKids at: (index)) getVspot).
index := index + 1. ].

printSelf
" prints its own content out."

| 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."

setChildren
"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 "local variables."

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."

(myKids size) timesRepeat:
[horz := horz + gap.
(myKids at: index) anchor: horz at: vert; setDepth: (depth + 1).
index := index + 1.
"Transcript show: (horz) printString; cr." "Helpful debug statement, commented out."
].

setDepth: newDepth
"Allows us to set the Depth of the instance variable depth."

depth := newDepth.



There is also the class method for clearing some space for the display.

clearWorld
" This opens the form area from 300 to 300 pixels, and proceeds to fill with white, so we have
a place for our tree to be displayed."

(Form extent: 300@300) fillWhite display


Link to this Page