Lab2 -- DrawRect, GrowMan

advertisement
CSIS10A
Lab2
5 pts: Ex 2.8.2 DrawRect
5 pts: Ex 3.12.2 GrowMan
Chapters 2 and 3
Ex 2.8.2 DrawRect
1) Create the Lab2 Project folder
a.
b.
c.
d.
In BlueJ, choose Project/New Project and browse to the proper location
Type in the filename: Lab2 (no spaces) and choose “create”
Now click on “New Class” In BlueJ and call it DrawRect
Go to the website, click on EmptyProgram.java, Ctrl-A, Ctrl-C (select all and
copy)
e. In BlueJ, double click the DrawRect box and then Ctrl-A, Ctrl-V (select all and
paste)
f. Rename EmptyProgram to DrawRect for the class declaration and constructor
method.
g. Compile and run. The program should do nothing (but have no errors).
2) Solve the problam:
a. This program needs an instance variable of type FramedRect (call it rect). Add
this variable now to the region above the constructor method.
b. Rename onMouseClick to onMousePress, then copy it and make an
onMouseDrag and onMouseRelease method.
c. onMousePress: add a statement to assign variable rect a new FramedRect
positioned at the mouse point location.
d. onMouseDrag: use the moveTo method to position rect at the current mouse
point location.
e. onMouseRelease: remove rect from canvas and create a new FilledRect at the
mouse point.
Ex 3.12.2 GrowMan
Hints on GrowMan:
This problem is about the use of primitives (int in this case), variables, and forumulating math
expressions for the kinds of graphical operations we may wish to do in Java.
1) Before beginning this program, familiarize youself with the above chart. Make sure you
understand how the labelled parts relate to the values they are assigned. For instance,
notice that the arms and legs extend both LIMB_SIZE high and LIMB_SIZE wide. The
FEET_Y constant locates the bottom of the feet by adding the LIMB_SIZE to the Y
position of the BODY_END constant.
2) Try ADDING LINES of your own to the above picture, indicating the locations described
by the constants RIGHT_X and ARMPIT_Y
3) In BlueJ Lab2, create a new class called GrowMan, then copy/past the starter
GrowMan.java file from the website into it.
4) There are only two methods we need to define for this project: begin() and
onMouseClick(). The begin method draws the original small man, and the onMouseClick
method grows it.
5) The starter program has already declared instance variables for the head, and the other
body parts (lines).
a. In begin(), assign head a new FramedOval at the correct position (HEAD_START,
HEAD_START) and initial height and width.
b. In onMouseClick, to resize the head you can use the setHeight and setWidth
methods, but what can you put for the values to set them to? To make the head
grow, we have to read the current values of height and width, and set the new
height and width to be 2 units larger (the value of GROW).
c. This could be done in several steps using a local variable of type int (in
onMouseClick):
int
current_size = head.getWidth();
// the width and height are equal
head.setWidth( current_size + GROW); // now head is wider
head.setHeight(current_size + GROW); //
… and taller
d. Notice we don’t say private int current_size. This variable only exists while
the onMouseClick method is executing. It’s just temporary storage for use while
the method is doing its work.
e. An alternative to c) above is to invoke the getWidth method in the parentheses
(parameter) of the setWidth method, etc. This would look like so:
head.setWidth( head.getWidth() + GROW);
head.setHeight( head.getHeight() + GROW);
f. In other words, we will set the width to the current width + GROW.
g. Try doing this both ways in onMouseClick. Which way do you prefer? Which way
is easier for you to understand?
6) Now we can build the rest of the man, and try to grow him. One trick to making the
figure stick together is to use Location variables to track the position of the chin, armpit,
left and right hands, left and right feet, and groin (for lack of a better word!). And use
these Locations in constructing the lines that make up the body parts.
Then, as the man grows, we can move the Locations around using the translate method,
and then use these relocated positions in the setEndPoints method for the different
lines.
Would you expect these variables (chin, armpit, etc) to be instance or local variables?
Why?
7) Let’s declare these Location variables (chin, armpit, L_hand, R_hand, L_foot, R_foot,
groin) as instance variables (did you guess correctly? Yes, instance variables are needed
here, because we need to refer to these variables in both the begin and onMouseClick
methods, and this is the only way they can retain their values as the program runs.
8) Now, in the begin method, initialize your Location variables, following carefully the chart
and the constant definitions given above. For example:
chin = new Location(HEAD_START + HEAD_SIZE/2,
HEAD_START + HEAD_SIZE);
armpit = new Location(BODY_X, ARMPIT_Y);
(finish constructing the other 5 locations
in begin())
9) Once the locations are defined, then you can create the body parts. For example:
body = new Line( chin, groin, canvas);
and so on…create the arms and legs now using your locations for armpit, groin, hands
and feet.
10) Test your program. Does the man appear well connected ? If not, check your program
for errors and adjust the locations you created in step 8, or the lines created in step 9 as
necessary.
11) When you have fixed all the errors, you can now make the man grow! The trick to this
part is to use the translate method to move the body part Locations and then set the
endpoints of your line objects with the newly moved locations.
12) For example, the body needs to grow downward with the head and also get longer by 2x
the GROW rate for the head. It also needs to grow rightward one-half the GROW rate
(because the chin is only halfway across the head from left to right). So, to do this in
java, we will say:
chin.translate( GROW/2, GROW); // move the chin over and down
groin.translate(GROW/2 , GROW + 2*GROW);
// move the groin over and an extra 2*GROW farther down
body.setEndPoints(chin, groin);
13) Add the above statements to onMouseClick and see if the body grows properly.
14) Now, following the same logic, add statements in onMouseClick to translate the
locations of the armpit, left and right hands and feet, then adjust the line objects for the
arms and legs accordingly.
15) Debug your program! If you find there are errors, check your math, your geometry and
see if you can pinpoint where the problem is coming from. We will be using graphical
objects for the rest of the class, so this is a good time to master the different techniques
needed to solve this problem.
16) When you are finished, check over your programs one last time before uploading. Is
your name and email address in the comment block at the top of each file? Did you add
comments similar to the ones provided to make your code more readable? Finally, are
your statements arranged neatly in the file so your program can be easily understood by
others?
17) Close BlueJ, and in Windows (or Mac), ZIP (compress) the Lab2 folder so it all fits inside
one file. Then, visit the CSIS10A website, click on the homework uploader, enter your
passcode, and upload your Lab2.zip file.
Download