Graphic JavaScript

Back to index
The student may experience a small amount of frustration when it comes to centering squares in a square applet area. Let's say that we are dealing with a 200 x 200 applet area and that we want to center a square in this area so that there is ten units between it and the applet edge on all sides. Here is the line that would work for this:

   ctx.fillRect(10,10,180,180);
The student might, at first, think that the last two numbers should both be 190. Upon reflection the student will realize that these numbers must both be 180 since 10 units on the right and the left make a total of 20 units and 200 minus 20 is 180. The same thing goes for the top and the bottom of the square.
EXAMPLE:
Inspect the code for this applet. You will notice the embedding of the filled rectangles was accomplished using a for-loop. Also you will notice that color values were stored in an array for easy access within the for-loop. You will not be expected to create an array or a for-loop in this assignment, but note these features for future reference.
ASSIGNMENT:
You will alter this applet in the following ways:
  1. Increase the number of embedded filled squares to six, but leave the area of the last one the same.
  2. Instead of an over-lapping group of five squares in the middle, create an array (spatially speaking) of nine squares evenly spaced out in the middle area.
Your browser does not support the HTML5 canvas tag.