Difference between revisions of "W1502 Object Attributes"

From Coder Merlin
(Updated formatting)
Line 1: Line 1:
= Prerequisites =
[[File:RGB color wheel 24.svg|thumb|link=|RGB color wheel 24]]
== Prerequisites ==
* [[W1501 Introduction to Objects]]
* [[W1501 Introduction to Objects]]
== Background ==
* Read [https://en.wikipedia.org/wiki/Color Color] (Wikipedia)
== Prepare ==
Create a new {{SwiftLibrary|Scenes}} shell project within your {{Pathname|Experience}} directory:
{{ConsoleLine|ty-cam@codermerlin:~$ |cd ~/Experiences}}
{{ConsoleLine|ty-cam@codermerlin:~/Experiences$ |git clone <nowiki>https://github.com/TheCoderMerlin/ScenesShellBasic</nowiki> W1502}}


= Research =
* Predefined color names may be found here:  [https://www.w3.org/wiki/CSS/Properties/color/keywords CSS Color Keywords], or, colors may be created by their RGB values using the constructor:
<syntaxhighlight lang="swift">
Color(red:UInt8, green:UInt8, blue:UInt8)
</syntaxhighlight>


= Experiment =
Enter the Sources/ScenesShell directory of the new project:
{{ConsoleLine|ty-cam@codermerlin:~/Experiences$ |cd W1502/Sources/ScenesShell/}}
 
Open a browser (or use a new tab on an already-open browser).  Then, go to the URL: 
<pre>
http://www.codermerlin.com/users/user-name/dyn/index.html
</pre>
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|It's useful to bookmark this page in your browser.}}
 
== Experiment ==
{{StopProgram|Stop the running program.
Return to the ''console'' and press {{SpecialKey|CONTROL|C}}
}}


Enter into the Sources/IgisShellD directory of the W1501 experience.
<syntaxhighlight lang="bash">
cd ~/Experiences/W1501/Sources/IgisShellD
</syntaxhighlight>


Open main.swift in emacs.
Open the file {{Pathname|Background.swift}} in emacs.
<syntaxhighlight lang="bash">
emacs main.swift
</syntaxhighlight>


Find the function within the Painter object named "setup".  Edit the contents as follows to specify the fill color of the rectangle:
Add a new method (below ''init'') as follows:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
     override func setup(canvasSize:Size, canvas:Canvas) {
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rectangle = Rectangle(rect:rect)
         let rectangle = Rectangle(rect:rect)
         let fillStyle = FillStyle(color:Color(.blue))
         let fillStyle = FillStyle(color:Color(.blue))
         canvas.render(fillStyle)
         canvas.render(fillStyle, rectangle)
        canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Remember to save the file, then suspend emacs.
Remember to save the file, then suspend emacs.
{{Hint|
Predefined color names may be found here:  [https://www.w3.org/wiki/CSS/Properties/color/keywords CSS Color Keywords], or, colors may be created by their RGB values using the constructor:
<syntaxhighlight lang="swift">
Color(red:UInt8, green:UInt8, blue:UInt8)
</syntaxhighlight>
}}


{{RunProgram|Run the program and refresh the browser page.}}
{{RunProgram|Run the program and refresh the browser page.}}
{{Observe|: Section 1|
# What do you observe?
# Is the rectangle wider than it is tall or taller than it is wide?  Why?
# Is the rectangle filled or only an outline?  Why?
# What color is the rectangle?  Why is it that specific shade rather than a similar color?
}}


{{StopProgram|Stop the running program.}}
{{StopProgram|Stop the running program.}}


Let's stroke the rectangle in green instead of filling it.  Interrupt the running program and then resume emacs and edit the setup function so it appears as follows:
Let's stroke the rectangle in green instead of filling it.  Interrupt the running program and then resume emacs and edit the setup function so it appears as follows:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
     override func setup(canvasSize:Size, canvas:Canvas) {
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rectangle = Rectangle(rect:rect, fillMode:.stroke)
         let rectangle = Rectangle(rect:rect, fillMode:.stroke)
         let strokeStyle = StrokeStyle(color:Color(.green))
         let strokeStyle = StrokeStyle(color:Color(.green))
         canvas.render(strokeStyle)
         canvas.render(strokeStyle, rectangle)
        canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>
Line 51: Line 78:


{{RunProgram|Run the program and refresh the browser page.}}
{{RunProgram|Run the program and refresh the browser page.}}
{{Observe|: Section 2|
# What do you observe?
# Is the rectangle filled or only an outline?  Why?
# What color is the rectangle?  Why is it that specific shade rather than a similar color?
}}


{{StopProgram|Stop the running program.}}
{{StopProgram|Stop the running program.}}


Let's thicken the stroke line.  Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:
Let's thicken the stroke line.  Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
     override func setup(canvasSize:Size, canvas:Canvas) {
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rectangle = Rectangle(rect:rect, fillMode:.stroke)
         let rectangle = Rectangle(rect:rect, fillMode:.stroke)
         let strokeStyle = StrokeStyle(color:Color(.green))
         let strokeStyle = StrokeStyle(color:Color(.green))
         let lineWidth = LineWidth(width:5)
         let lineWidth = LineWidth(width:5)
         canvas.render(strokeStyle)
         canvas.render(strokeStyle, lineWidth, rectangle)
        canvas.render(lineWidth)
        canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>
Line 70: Line 104:


{{RunProgram|Run the program and refresh the browser page.}}
{{RunProgram|Run the program and refresh the browser page.}}
{{Observe|: Section 3|
# What do you observe?
# How has the appearance of the rectangle changed from the previous section?  Why?
}}


{{StopProgram|Stop the running program.}}
{{StopProgram|Stop the running program.}}
Line 75: Line 116:
Finally, let's both stroke and fill simultaneously.  Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:
Finally, let's both stroke and fill simultaneously.  Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
     func setup(canvas:Canvas) {
     override func setup(canvasSize:Size, canvas:Canvas) {
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
         let rectangle = Rectangle(rect:rect, fillMode:.fillAndStroke)
         let rectangle = Rectangle(rect:rect, fillMode:.fillAndStroke)
Line 81: Line 122:
         let strokeStyle = StrokeStyle(color:Color(.blue))
         let strokeStyle = StrokeStyle(color:Color(.blue))
         let lineWidth = LineWidth(width:5)
         let lineWidth = LineWidth(width:5)
         canvas.render(fillStyle)
         canvas.render(fillStyle, strokeStyle, lineWidth, rectangle)
        canvas.render(strokeStyle)
        canvas.render(lineWidth)
        canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>
Line 91: Line 129:


{{RunProgram|Run the program and refresh the browser page.}}
{{RunProgram|Run the program and refresh the browser page.}}
{{Observe|: Section 4|
# What do you observe?
# How has the appearance of the rectangle changed from the previous section?  Why?
}}


{{StopProgram|Stop the running program.}}
{{StopProgram|Stop the running program.}}
Before beginning the exercises, remove the above code and then recreate your skyscrapers (and only your skyscrapers) from the previous experience.  (You may copy/paste the required code from [[W1501 Introduction to Objects]].)


== Exercises ==
== Exercises ==

Revision as of 15:37, 19 April 2020

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

Prerequisites[edit]

Background[edit]

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 W1502


Enter the Sources/ScenesShell directory of the new project:

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

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
It's useful to bookmark this page in your browser.

Experiment[edit]

Stop button red ex
Stop the running program.

Return to the console and press CONTROL-C


Open the file Background.swift in emacs.

Add a new method (below init) as follows:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
        let rectangle = Rectangle(rect:rect)
        let fillStyle = FillStyle(color:Color(.blue))
        canvas.render(fillStyle, rectangle)
    }

Remember to save the file, then suspend emacs.

Hint.pngHelpful Hint

Predefined color names may be found here: CSS Color Keywords, or, colors may be created by their RGB values using the constructor:

Color(red:UInt8, green:UInt8, blue:UInt8)


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. Is the rectangle wider than it is tall or taller than it is wide? Why?
  3. Is the rectangle filled or only an outline? Why?
  4. What color is the rectangle? Why is it that specific shade rather than a similar color?


Stop button red ex
Stop the running program.


Let's stroke the rectangle in green instead of filling it. Interrupt the running program and then resume emacs and edit the setup function so it appears as follows:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
        let rectangle = Rectangle(rect:rect, fillMode:.stroke)
        let strokeStyle = StrokeStyle(color:Color(.green))
        canvas.render(strokeStyle, 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 2
  1. What do you observe?
  2. Is the rectangle filled or only an outline? Why?
  3. What color is the rectangle? Why is it that specific shade rather than a similar color?


Stop button red ex
Stop the running program.


Let's thicken the stroke line. Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
        let rectangle = Rectangle(rect:rect, fillMode:.stroke)
        let strokeStyle = StrokeStyle(color:Color(.green))
        let lineWidth = LineWidth(width:5)
        canvas.render(strokeStyle, lineWidth, 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. How has the appearance of the rectangle changed from the previous section? Why?


Stop button red ex
Stop the running program.

Finally, let's both stroke and fill simultaneously. Interrupt the running program and then resume emacs and add the following lines so that the entire function appears as:

    override func setup(canvasSize:Size, canvas:Canvas) {
        let rect = Rect(topLeft:Point(x:100, y:50), size:Size(width:200, height:300))
        let rectangle = Rectangle(rect:rect, fillMode:.fillAndStroke)
        let fillStyle = FillStyle(color:Color(.green))
        let strokeStyle = StrokeStyle(color:Color(.blue))
        let lineWidth = LineWidth(width:5)
        canvas.render(fillStyle, strokeStyle, lineWidth, 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 4
  1. What do you observe?
  2. How has the appearance of the rectangle changed from the previous section? Why?


Stop button red ex
Stop the running program.

Before beginning the exercises, remove the above code and then recreate your skyscrapers (and only your skyscrapers) from the previous experience. (You may copy/paste the required code from W1501 Introduction to Objects.)

Exercises[edit]

ExercisesExercisesIcon.png

Continuing your previous project of (at least) three skyscrapers:

  1. Color the sky and ground
  2. Color the body of each skyscraper in a different color
  3. Color various aspects of your skyscraper (e.g. windows, doors) in different colors

When done, you must have at least seven different colors on your canvas.