Difference between revisions of "W1501 Introduction to Objects"

From Coder Merlin
Line 37: Line 37:


Open main.swift in emacs.
Open main.swift in emacs.
<syntaxhighlight>
<syntaxhighlight lang="bash">
emacs main.swift
emacs main.swift
</syntaxhighlight>
</syntaxhighlight>


Find the function within the Painter object named "setup".  Add a line to create a Text object and then draw that object.
Find the function within the Painter object named "setup".  Add a line to create a Text object and then draw that object.
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift" lang="bash">
     func setup(canvas:Canvas) {
     func setup(canvas:Canvas) {
         let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
         let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
Line 50: Line 50:


Now, suspend emacs and then run the program again.
Now, suspend emacs and then run the program again.
<syntaxhighlight>
<syntaxhighlight lang="bash">
swift run
swift run
</syntaxhighlight>
</syntaxhighlight>
Line 65: Line 65:


Now, suspend emacs and then run the program again.
Now, suspend emacs and then run the program again.
<syntaxhighlight>
<syntaxhighlight lang="bash">
swift run
swift run
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:24, 8 January 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Research[edit]

Create[edit]

First Steps[edit]

Create an Igis shell project within your "project" directory.

cd ~/projects
git clone https://github.com/TangoGolfDigital/IgisShell IgisShell-Introduction

Enter into the Sources directory of the new project.

cd IgisShell-Introduction/Sources/IgisShell/

Build the project. (This may take some time.)

swift build

Run the project.

swift run

Open a browser (or use a new tab on an already-open browser). Go to the URL: http://www.codermerlin.com/users/user-name/dyn/index.html

NOTE: You MUST change user-name to your actual user name. For example, http://www.codermerlin.com/users/john-williams/dyn/index.html

You'll know your successful if you see the title bar change to "Coder Merlin: IGIS".

Hello World[edit]

Stop the running program with ^C (Hold down the CONTROL key and press C)

Open main.swift in emacs.

emacs main.swift

Find the function within the Painter object named "setup". Add a line to create a Text object and then draw that object.

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        canvas.paint(text)
    }

Now, suspend emacs and then run the program again.

swift run

Now, refresh the browser page.

Let's change the font. Interrupt the running program and then resume emacs and edit the line where the text object is instantiated.

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!", font:"30pt Arial")
        canvas.paint(text)
    }

Now, suspend emacs and then run the program again.

swift run

Now, refresh the browser page.

Let's add a rectangle around the text. Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!", font:"30pt Arial")
        canvas.paint(text)

        let rect = Rect(topLeft:Point(x:20, y:10), size:Size(width:300, height:50))
        let rectangle = Rectangle(rect:rect, fillMode:.stroke)
        canvas.paint(rectangle)
    }

Now, suspend emacs and then run the program again.

swift run

Now, refresh the browser page.

Exercises[edit]

  1. Draw a single rectangle (without any text)
  2. Draw a grid (at least 3 x 3)
  3. Draw a brick wall (at least 5 x 5)
  4. Draw a pyramid constructed of bricks (at least seven rows high)