Step 2: Using Equations to Represent the Size and PositionI worked very hard to determine exactly where my ears should go with respect to the head and I do not want to lose that. On the other hand, I know that it is not always the case that I will want to see Mickey that large in the center of the applet. And when I change my mind about his position or size later, I do not want to have to remember (or worse yet, figure out again) how to change each number in my code above to match my new situation. To do this though, I have to modify the code to draw Mickey based only on his center and size. Given that little bit of information, the same information I typically give the shapes used to create Mickey, I should be able to recreate Mickey automatically. Instead of using literal numbers, I can use variables which can be changed easily. Likewise, I will use equations based on those variables to derive the positions and sizes of Mickey's ears. Because I want everything to be based on his position and size, I will need three variables to represent these values --- two for his position coordinates and one for his size (since I am assuming Mickey is made up of perfect circles). Moreover, because I want to make algebraic relationships between these values, I must use doubles to store these values. I cannot do algebra with objects like ovals, strings, or applets. I decided to name these variables cx, cy, and size for center x coordinate, center y coordinate, and size. Looking at the numbers I used above closely,
I was able to determine some relationships between them. The size of the ears is the easiest: 125 / 200 = 5 / 8; the ears are five-eighths the size of the head. The position of the ears is slightly harder because it involves a combination of factors. Of course, the first thing I notice is that one ear is a mirror image of the other, i.e., the y coordinate should be the same and the x coordinate should be the same distance away from that center for each. So this means I only have to figure out two more values. Well, when the center of the head is (200, 200) the ear's center is 100 pixels away horizontally and 80 pixels away vertically. I quickly realize that I cannot simply subtract 100 from the x coordinate, or even make it one-half the value because what happens when my Mickey silhouette is smaller than 100 pixels in total size? There is no way my ears will match up! So clearly my new coordinates must be based on the size of the head in addition to its position. In this case, 100 / 200 = 1 / 2 and 80 / 200 = 4 / 10. For the left ear, both coordinates were smaller than the center, so I subtract half the size from the x coordinate and four-tenths of the size from the y coordinate. To create the mirror image position, I simply add half the size instead of subtracting it; the y coordinate is the same for both. Thus, I will use the following equations:
The modified the code looks like the following: public class Applet extends GP.Containers.Applet { /* The program starts within this function when the applet is created. */ public Applet () { double cx = 200; double cy = 200; double sz = 200; /* create Mickey parts */ GP.Shapes.Oval head = new GP.Shapes.Oval(); GP.Shapes.Oval leftEar = new GP.Shapes.Oval(); GP.Shapes.Oval rightEar = new GP.Shapes.Oval(); /* ears should be same size, head larger */ GP.Attributes.Size size = new GP.Attributes.Size(sz); head.SetSize(size); size = new GP.Attributes.Size(5 * sz / 8); leftEar.SetSize(size); rightEar.SetSize(size); /* set position of Mickey parts */ GP.Attributes.Position center = new GP.Attributes.Position(cx, cy); head.SetPosition(center); center = new GP.Attributes.Position(cx - sz / 2, cy - 4 * sz / 10); leftEar.SetPosition(center); center = new GP.Attributes.Position(cx + sz / 2, cy - 4 * sz / 10); rightEar.SetPosition(center); /* fill each part in as black to make a silhouette */ GP.Attributes.Color black = new GP.Attributes.Colors.Black(); head.SetColor(black); leftEar.SetColor(black); rightEar.SetColor(black); } } To test that my equations are correct, I simply change the initial values of the three variables of type double and rebuild my applet code. Now I can create mickey mouse, positioned anywhere in the applet and at any given size. It is time to take another break and celebrate! |
Comments? |