Difference between revisions of "W1502 Object Attributes"

From Coder Merlin
(Updated formatting)
Line 3: Line 3:


= Research =  
= 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 Color(red:UInt8, green:UInt8, blue:UInt8)
* 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 =
= Experiment =
Line 23: Line 26:
         let rectangle = Rectangle(rect:rect)
         let rectangle = Rectangle(rect:rect)
         let fillStyle = FillStyle(color:Color(.blue))
         let fillStyle = FillStyle(color:Color(.blue))
         canvas.paint(fillStyle)
         canvas.render(fillStyle)
         canvas.paint(rectangle)
         canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now, suspend emacs and then run the program again.
Remember to save the file, then suspend emacs.
<syntaxhighlight lang="bash">
 
./run.sh
{{RunProgram|Run the program and refresh the browser page.}}
</syntaxhighlight>


Now, refresh the browser page.
{{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:
Line 41: Line 43:
         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.paint(strokeStyle)
         canvas.render(strokeStyle)
         canvas.paint(rectangle)
         canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now, suspend emacs and then run the program again.
Remember to save the file, then suspend emacs.
<syntaxhighlight lang="bash">
 
./run.sh
{{RunProgram|Run the program and refresh the browser page.}}
</syntaxhighlight>


Now, refresh the browser page.
{{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:
Line 60: Line 61:
         let strokeStyle = StrokeStyle(color:Color(.green))
         let strokeStyle = StrokeStyle(color:Color(.green))
         let lineWidth = LineWidth(width:5)
         let lineWidth = LineWidth(width:5)
         canvas.paint(strokeStyle)
         canvas.render(strokeStyle)
         canvas.paint(lineWidth)
         canvas.render(lineWidth)
         canvas.paint(rectangle)
         canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now, suspend emacs and then run the program again.
Remember to save the file, then suspend emacs.
<syntaxhighlight lang="bash">
 
./run.sh
{{RunProgram|Run the program and refresh the browser page.}}
</syntaxhighlight>


Now, refresh the browser page.
{{StopProgram|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:
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:
Line 81: Line 81:
         let strokeStyle = StrokeStyle(color:Color(.blue))
         let strokeStyle = StrokeStyle(color:Color(.blue))
         let lineWidth = LineWidth(width:5)
         let lineWidth = LineWidth(width:5)
         canvas.paint(fillStyle)
         canvas.render(fillStyle)
         canvas.paint(strokeStyle)
         canvas.render(strokeStyle)
         canvas.paint(lineWidth)
         canvas.render(lineWidth)
         canvas.paint(rectangle)
         canvas.render(rectangle)
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now, suspend emacs and then run the program again.
Remember to save the file, then suspend emacs.
<syntaxhighlight lang="bash">
 
./run.sh
{{RunProgram|Run the program and refresh the browser page.}}
</syntaxhighlight>


Now, refresh the browser page.
{{StopProgram|Stop the running program.}}


== Exercises ==
== Exercises ==
{{Exercises|
Continuing your previous project of (at least) three skyscrapers:
Continuing your previous project of (at least) three skyscrapers:
# Color the sky and ground
# Color the sky and ground
Line 102: Line 102:


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

Revision as of 23:49, 5 December 2019

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

Prerequisites[edit]

Research[edit]

  • 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)

Experiment[edit]

Enter into the Sources/IgisShellD directory of the W1501 experience.

cd ~/Experiences/W1501/Sources/IgisShellD

Open main.swift in emacs.

emacs main.swift

Find the function within the Painter object named "setup". Edit the contents as follows to specify the fill color of the rectangle:

    func setup(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)
        canvas.render(rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
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:

    func setup(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)
        canvas.render(rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
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:

    func setup(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)
        canvas.render(lineWidth)
        canvas.render(rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
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:

    func setup(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)
        canvas.render(strokeStyle)
        canvas.render(lineWidth)
        canvas.render(rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
Stop button red ex
Stop the running program.

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.