Difference between revisions of "W1501 Introduction to Objects"

From Coder Merlin
Line 53: Line 53:


This defines a new {{SwiftKeyword|class}} named {{SwiftClass|Background}}.  The colon and the following identifier, {{SwiftClass|RenderableEntity}}, indicates that the {{SwiftKeyword|class}} {{SwiftClass|Background}} inherits properties and methods from the class {{SwiftClass|RenderableEntity}}.  You'll note that the {{SwiftKeyword|class}} includes a '''constructor''', indicated by the keyword {{SwiftKeyword|init}} on line 2:
This defines a new {{SwiftKeyword|class}} named {{SwiftClass|Background}}.  The colon and the following identifier, {{SwiftClass|RenderableEntity}}, indicates that the {{SwiftKeyword|class}} {{SwiftClass|Background}} inherits properties and methods from the class {{SwiftClass|RenderableEntity}}.  You'll note that the {{SwiftKeyword|class}} includes a '''constructor''', indicated by the keyword {{SwiftKeyword|init}} on line 2:
<syntaxhighlight lang="swift" highlight="2" line>
<syntaxhighlight lang="swift" line>
class Background : RenderableEntity {
class Background : RenderableEntity {
     init() {
     init() {
Line 91: Line 91:
</syntaxhighlight>
</syntaxhighlight>


At this point, we have a {{SwiftClass|Text}} object that "knows" how to render itself on the canvas, but it hasn't yet actually done any rendering.  There are two methods in which we generally render.  The first is called ''setup''.  The ''setup'' function executes once.  It's an opportunity to set up objects and/or render if we're creating a static (non-animated) display.  The other function is ''render'' which executes once per frame.  In this case, using ''setup'' is sufficient.
At this point, we have a {{SwiftClass|Text}} object that "knows" how to render itself on the canvas, but it hasn't yet actually done any rendering.  There are two methods in which we generally render.  The first is called {{SwiftIdentifier|setup}}.  The {{SwiftIdentifier|setup}} function executes once.  It's an opportunity to set up objects and/or render if we're creating a static (non-animated) display.  The other function is {{SwiftIdentifier|render}} which executes once per frame.  In this case, using {{SwiftIdentifier|setup}} is sufficient.


Add a new method (below ''init'') as follows:
Add a new method (below ''init'') as follows:
Line 102: Line 102:
This code instructs the ''text'' object to render itself on the canvas.   
This code instructs the ''text'' object to render itself on the canvas.   


Save the file, then suspend emacs.
Be sure to edit the file as indicated above, save the file, then suspend emacs.


{{RunProgram|Run the program and refresh the browser page.}}
{{RunProgram|Run the program and refresh the browser page.}}

Revision as of 16:57, 31 March 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
PDP-15 Graphics Terminal with Tablet

Prerequisites[edit]

Background[edit]

Introduction[edit]

We'll begin our exploration of objects using Scenes. Scenes is an object-oriented, event-driven platform that enables us to write server-side Swift code which (rapidly) sends a series of instructions to a browser to generate visual effects, and optionally, an interactive experience. Scenes is built upon another framework called Igis.

CautionWarnIcon.png

It's important to understand that while most of the objects we'll be using have an associated visual representation, most of the objects that we build and interact with in programming will not have such a representation.

Prepare[edit]

Create a new Scenes shell project within your Experience directory:

ty-cam@codermerlin:~$  cd ~/Experiences

ty-cam@codermerlin:~/Experiences$  git clone https://github.com/TheCoderMerlin/ScenesShellBasic W1501


Getting started with a shell enables us to avoid writing repetitive code commonly required by all similar projects. This type of code is often called boilerplate code.

Enter the Sources/ScenesShell directory of the new project:

ty-cam@codermerlin:~/Experiences$  cd W1501/Sources/ScenesShell/


Start button green arrow
Run the program.

ty-cam@codermerlin:~/Experiences/W1501/Sources/ScenesShell$  run


Open a browser (or use a new tab on an already-open browser). Then, 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/ty-cam/dyn/index.html

You'll know you're successful if you see the title bar change to "Coder Merlin: IGIS". (The browser window will be blank because we haven't added any graphics yet.)

Hint.pngHelpful Hint
Helpful hint: It's useful to bookmark this page in your browser.

Hello World[edit]

Text[edit]

Stop button red ex
Stop the running program.

Return to the console and press CONTROL-C

Open the file Background.swift in emacs. Read through the file. You'll find some code as follows:

class Background : RenderableEntity

This defines a new class named Background. The colon and the following identifier, RenderableEntity, indicates that the class Background inherits properties and methods from the class RenderableEntity. You'll note that the class includes a constructor, indicated by the keyword init on line 2:

class Background : RenderableEntity {
    init() {
        // Using a meaningful name can be helpful for debugging
        super.init(name:"Background")
    }
}

The constructor is invoked automatically as the object (an instance of the class) is being initialized. This is the place where you'll set up your object. The current constructor in this file sets the object's name using the keywords super and init. super indicates that we're invoking a function (or accessing a property) on our parent class, the class from which we inherited. In this case, that would be the RenderableEntity.

We're going to display some text on our canvas (the virtual surface on which we'll be rendering images in the browser). In order to do that, we'll first declare a Text object as follows:

class Background : RenderableEntity {
    let text : Text

    init() {
        // Using a meaningful name can be helpful for debugging
        super.init(name:"Background")
    }
}

We'll initialize our text object in the constructor:

class Background : RenderableEntity {
    let text : Text

    init() {
        // Initialize objects
        text = Text(location:Point(x:50, y:50), text:"Hello, World!")

        // Using a meaningful name can be helpful for debugging
        super.init(name:"Background")
    }
}

At this point, we have a Text object that "knows" how to render itself on the canvas, but it hasn't yet actually done any rendering. There are two methods in which we generally render. The first is called setup. The setup function executes once. It's an opportunity to set up objects and/or render if we're creating a static (non-animated) display. The other function is render which executes once per frame. In this case, using setup is sufficient.

Add a new method (below init) as follows:

    override func setup(canvasSize:Size, canvas:Canvas) {
        canvas.render(text)
    }

This code instructs the text object to render itself on the canvas.

Be sure to edit the file as indicated above, save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.


ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 1
  1. What do you observe?
  2. How large is the text?

Font[edit]

Stop button red ex
Stop the running program.


Let's change the font. Resume emacs and add a line below where the text object is instantiated.

    init() {
        // Initialize objects
        text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        text.font = "30pt Arial"

        // Using a meaningful name can be helpful for debugging
        super.init(name:"Background")
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.


ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 2
  1. What do you observe?
  2. How large is the text now?

Rectangle[edit]

Stop button red ex
Stop the running program.


Let's add a rectangle around the text. Resume emacs and add the highlighted lines:

class Background : RenderableEntity {

    let text : Text
    let rectangle : Rectangle

    init() {
        // Initialize objects
        text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        text.font = "30pt Arial"

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

        // Using a meaningful name can be helpful for debugging
        super.init(name:"Background")
    }

Then, update the following, highlighted line to the setup function:

    override func setup(canvasSize:Size, canvas:Canvas) {
        canvas.render(text, rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.


ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 3
  1. What do you observe?
  2. What is the purpose of the location parameter in the Text constructor?
  3. What is the corresponding parameter named in the Rectangle?
  4. What is the difference between a Rect and a Rectangle? Hint: Are you able to render a Rect?


Stop button red ex
Stop the running program.

Exercises[edit]

ExercisesExercisesIcon.png

Making use of your accumulated knowledge to date (loops, functions, coding style, etc.) and using only Rectangles and Text on a single canvas:

  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)
  5. Draw a skyscraper with a large sign on the roof containing your name
  6. Draw at least three skyscrapers of different heights on the same Canvas, each containing your name