Joe the Box

47
Joe the Box Chapter 3 Squeak: Object-oriented design with multimedia applications

description

Joe the Box. Chapter 3 Squeak: Object-oriented design with multimedia applications. Story. Generating: How to File In/Out Code Playing with Joe the Box A microworld for learning about Boxes Originally by Adele Goldberg Analyzing Box How to Draw Extending Box - PowerPoint PPT Presentation

Transcript of Joe the Box

Page 1: Joe the Box

Joe the Box

Chapter 3Squeak: Object-oriented design with multimedia applications

Page 2: Joe the Box

Story

Generating: How to File In/Out CodePlaying with Joe the Box

A microworld for learning about Boxes Originally by Adele Goldberg

Analyzing BoxHow to DrawExtending BoxGenerating: How to take apart an example Improving Box

A Start on UML

Page 3: Joe the Box

Filing in

Normal way of passing around code in Smalltalk Plain text file, suffix .st (SmallTalk) or .cs

(Change Set)Load in Boxes by filing it in

(First, get it on your computer)Find it in the FileListClick the File-in button

Page 4: Joe the Box

FileList

Directory Structure

Pattern tomatch tofilenames

Contents of selected file

Files in the directory

Page 5: Joe the Box

Filing out

Simply choose FileOut from the yellow-button menu in the Browser For any of category, class, protocol, or method

Page 6: Joe the Box

Creating Boxes

Box clearWorld "To open the world" joe := Box new. "To create Joe"

Page 7: Joe the Box

Joe is a Box

joe class "PrintIt"Joe is an instance of a Box

It knows how to do things that Boxes know how to doturn:move:grow:

Neat example of what Joe can do:

Page 8: Joe the Box

Joe being a Box

Page 9: Joe the Box

Jill is also a box

jill := Box new. "Make Jill"Jill knows the same things as Joe, in the same

way, but has different data jill turn: 127 "Jill turns, Joe stays the same." Two different boxes, with different

PositionsSizesOrientations

Page 10: Joe the Box

Class Box's Methods

Page 11: Joe the Box

Box's Instance Methods

Page 12: Joe the Box

NamedBox

jane := NamedBox named: 'jane'. "Note: Different creation method"

jane isKindOf: Box "PrintIt to see true"

Page 13: Joe the Box

NamedBox methods

Overriding instance methods: draw, undraw

Add undrawNameColor:New class method: named:

Page 14: Joe the Box

Creating the Box

Class Definition

Class method new

Page 15: Joe the Box

Issues in Creating the Box

Why is it “super” not “self” in the Class method new?

Why do we get a Box out of this new and not an instance of Object (or whatever)?

What if NamedBox did the exact same definition of new? Would it work?

Page 16: Joe the Box

Equivalent method for new

new| temporaryNewObject |

temporaryNewObject := super new. “Create the new object”

temporaryNewObject initialize. “Initialize it”

^temporaryNewObject “Return it”

Page 17: Joe the Box

So what's happening in initialize?

initializeposition := 50@50. "Set instance variables"

size := 50.

tilt := 0.

self draw. "Make it appear."

Page 18: Joe the Box

Class method for clearWorld

clearWorld(Form extent: 600@200) fillWhite display

A Form is a bitmap graphic Form extent: 600@200 creates one of the

given size (as a Point, x@y)

display puts it up at 0@0 on the Display

Page 19: Joe the Box

How to Draw (in Squeak)

Create a form from a file|f|f:= Form fromFileNamed: 'my.gif‘. f display.

Create form from a URLHTTPSocket httpGif:

'www.cc.gatech.edu/gvu/images/headers/titles/gvu-center.gif‘

Get form from the screen Form fromUser “select on screen”

Page 20: Joe the Box

Shrinking and Magnifying Forms

| f s |

f := Form fromUser. "Let the user define a rectangle"

s := f shrink: f boundingBox by: 2 @ 5. "Shrink it"

s displayOn: Display at: Sensor waitButton "Display it"

s := f magnify: f boundingBox by: 5@5. s display.

Page 21: Joe the Box

Rotating a Form

| a f |

f := Form fromDisplay: (0@0 extent: 200@200). "Save the screen"

a := 0. "Rotation value"

[Sensor anyButtonPressed] whileFalse: "Rotate until mousebutton""Grab screen from mousepoint, rotate, and display"

[((Form fromDisplay: (Sensor cursorPoint extent: 130@66))

rotateBy: (a := a+5)) display].

f display "Put the original corner of the screen back"

Page 22: Joe the Box

How Forms work

Forms are manipulated with BitBlt Bit Block Transfer It's what makes overlapping windows and pop-up

menus possible

Squeak includes a new kind of BitBlt called WarpBlt Instead of merely moving one rectangle somewhere

else, it moves one rectangle to a quadrilateral

Try: FishEyeMorph new openInWorld

Page 23: Joe the Box

Making other objects on Forms

From Smalltalk-80, Squeak actually knows about lots of graphical objects than can draw themselves on Forms: Arcs, Splines, Circles

| aCircle aForm |aForm := Form extent: 1@1. "This will define the circle edge"aForm fillBlack.aCircle := Circle new.aCircle form: aForm.aCircle radius: 50.aCircle center: [email protected] displayOn: Display

Page 24: Joe the Box

Teaching Boxes to Draw

draw| myPen |

myPen := Pen new. "Create a new Pen for drawing"

myPen up. "Don't draw while setting it up"

"Set up the Pen with the Boxes' characteristics"

myPen goto: position.

myPen turn: tilt.

myPen color: (Color black).

"Now put the Pen down and draw the box."

myPen down.

4 timesRepeat: [myPen go: size; turn: 90].

Page 25: Joe the Box

Pens as Turtles

Pens have a heading and a pen stateThey draw on Display, or create them

newOnForm:

Page 26: Joe the Box

Putting Pens in Morphic Images

Note that Morphic redraw destroys Pen/Form marks.form := Form extent: [email protected] := Pen newOnForm: form.p down.4 timesRepeat: [p go: 50; turn: 90].(SketchMorph withForm: form) openInWorld“m := ImageMorph new.m setNewImageFrom: form.m openInWorld.”

Page 27: Joe the Box

How Boxes undraw

undraw| myPen |

myPen := Pen new. "Create a new Pen for drawing"

myPen up.

"Set it up for drawing the Box"

myPen goto: position.

myPen turn: tilt.

myPen color: (Color white). "Now draw it with the background color"

myPen down.

4 timesRepeat: [myPen go: size; turn: 90].

Page 28: Joe the Box

All other Box methods

Follow the same pattern Undraw the current Box representation Changing something Draw it again

Page 29: Joe the Box

Two examples

grow: incrementself undraw.

size := size + increment.

self draw.

move: pointIncrementself undraw.

position := position + pointIncrement.

self draw.

Page 30: Joe the Box

Getting input from the user

Recall [Sensor anyButtonPressed] whileFalse: [joe moveTo: (Sensor mousePoint)]

Sensor is a global variable Instance of InputSensor Access point for the Mouse and Keyboard

Page 31: Joe the Box

Sensor for keyboard

Page 32: Joe the Box

Sensor for mouse

Page 33: Joe the Box

Extending Box to create NamedBox

Box subclass: #NamedBox

instanceVariableNames: 'name '

classVariableNames: ''

poolDictionaries: ''

category: 'Boxes'

Page 34: Joe the Box

What would happen if we did new?

jane := NamedBox new.

jane name: 'Jane'.

jane draw.

Here's the result: Why?

Page 35: Joe the Box

How named: works

named: aName| newBox |

newBox := super new. "Do the normal Box draw"

newBox undraw. "Erase it – using NamedBox erase"

newBox name: aName. "Set the name"

newBox draw. "Draw it with the name"

^newBox

Page 36: Joe the Box

How draw and undraw work

drawsuper draw. "Draw myself as a Box"

self drawNameColor: (Color black). "Draw my name in black"

undrawsuper undraw. "Undraw myself as a Box"

self drawNameColor: (Color white). "Erase my name in white"

Page 37: Joe the Box

Where unnamed NamedBox instances get their name

drawNameColor: aColor| displayName |

(name isNil) ifTrue: [name := 'Unnamed']. "If no name, fake one"

displayName := name asDisplayText. "Make the string a kind of Form"

displayName "Set its color"

foregroundColor: aColor

backgroundColor: (Color white).

displayName displayAt: position. "Display it"

Page 38: Joe the Box

Generating: Reusing an Example

Goal: Name in random colors

How do you reuse foregroundColor:backgroundColor:? Try implementors (Alt/Command-M) Try senders (Alt/Command-N) Find example class methods and try them

How generate random colors? Look for instance creation methods for Color Find class Random, read class examples and

comments

Page 39: Joe the Box

Putting together the pieces

myGenerator := Random new.

30 timesRepeat: [name:=‘Fred Flintstone' asDisplayText.

name foregroundColor:(Color r: myGenerator next

g: myGenerator next

b: myGenerator next)

backgroundColor: (Color transparent).

name displayAt: (100 atRandom) @ (100 atRandom).]

Page 40: Joe the Box

Improving Boxes

Design flaws in Boxes Creating new Pens and duplicating

functionalityPens have a location and direction

draw/undraw have too much replication

Improvements Each Box gets its own Pen Rewrite draw/undraw

Page 41: Joe the Box

Improving Box

New Box instance vars: size and pen

initializepen := Pen new. "Put a pen in an instance

variable."

pen place: 50@50.

size := 50.

self draw.

Page 42: Joe the Box

Delegate to Pen

Delegation: Asking another object to perform a service for the original object.

move: pointIncrementself undraw.

pen place: (pen location) + pointIncrement.

self draw.

turn: degreesself undraw.

pen turn: degrees.

self draw.

Page 43: Joe the Box

Re-doing draw/undraw

drawself drawColor: (Color black).

undrawself drawColor: (Color white).

Page 44: Joe the Box

drawColor for new Box

drawColor: colorpen color: color.4 timesRepeat: [pen go: size; turn: 90].

Page 45: Joe the Box

Fixing NamedBox draw

Previous version knew Box position

drawNameColor: aColor| displayName |

(name isNil) ifTrue: [name •'Unnamed'].

displayName := name asDisplayText.

displayName foregroundColor: aColorbackgroundColor: (Color white).

displayName displayAt: (pen location).

Page 46: Joe the Box

How do we talk about designs?

With representationsUML (Unified Modeling

Language) is the O-O Standard

The original Box class structure:

Page 47: Joe the Box

How do we depict the redesign?

Box

size

drawundrawdrawColor:grow:move:moveTo:turn:

1

pen

111

pen

111

NamedBox

name

drawundrawdrawNameColor:

Pen

headinglocation

go:place:turn: