You will find that most of your code for part 1 can be adapted from the functions assignment. This is intentional.
Write the following class:
BalloonThis class constructs and draws a balloon. The constructor takes the following parameters:
The first four parameters are integers describing an elliptical balloon centered at (x, y) in a bounding
box of size w by h, in the given color c. You must store
these values as instance variables (properties) of the Balloon class.
The Balloon class must also have two more instance variables that
describe the x- and y-speed of the balloon. Your constructor will
set these variables to each have a random value in the range 1 to 5 (inclusive).
The Balloon class contains these methods:
display()line()). The string starts at the
center of the balloon at the bottom, and extends to the right one-third the width of the
balloon and the line’s height is one-third the height of the balloon.
The string is outside the balloon’s bounding box.
move()
Your main program will create two Balloon objects of different sizes and colors.
You may make the balloons a fixed size and color or a random size and color.
The draw() function will repeatedly display and move the balloons. Save
this sketch with the name balloons.
This one is in honor of Misha, our campus cat. You will write a class
named CatFace that draws a cat’s face, nose, and mouth, and ears.
You will also write a classes named Eyes
and Whiskers to draw the rest of the features.
Here is the code for the CatFace class; you have to add the
code as shown in the comments. To make life easier, the cat face
is always drawn with a diameter
of 100 pixels.
class CatFace
{
// create an instance variable for the eyes and whiskers
int x;
int y;
CatFace(int tempX, int tempY)
{
x = tempX;
y = tempY;
// call the constructor for the eyes and whiskers.
// the initial length of the whiskers must be 45.
// the initial color of the eyes must be color(0, 255, 0)
}
void display()
{
// outline of face
stroke(0);
fill(255);
ellipse(x, y, 100, 100);
// draw nose
noStroke();
fill(255, 128, 128);
triangle(x - 5, y, x + 5, y, x, y + 10);
// mouth
stroke(0);
noFill();
arc(x, y - 10, 75, 75, PI / 4, 3 * PI / 4);
// ears
line(x, y - 50, x + 30, y - 70);
line(x + 30, y - 70, x + 40, y - 30);
line(x, y - 50, x - 30, y - 70);
line(x - 30, y - 70, x - 40, y - 30);
// display the eyes
// display the whiskers
}
}
Eyes class
The constructor for the Eyes class will take
three arguments: the x and y coordinates of the
center of the cat face, and a color for the eyes.
These will be stored in instance variables.
The class will have two methods:
void display()void setColor(color newColor)newColor.
This method does not draw the eyes. It just sets the color
so that the next time display() is called, they will
be drawn in the new color.Whiskers class
The constructor for the Whiskers class will take
three arguments: the x and y coordinates of the
center of the cat face, and a length for the whiskers.
These will be stored in instance variables.
The class will have two methods:
void display()void lengthen(int amount)amount to the instance
variable that holds the whisker length. If the number is
negative, the whiskers will become shorter; if it is positive,
the whiskers will become longer. The whisker length must always
be between 30 and 80 (use constrain() or if
statements to make this happen).
This method does not draw the whiskers. It just sets their length
so that the next time display() is called, they will
be drawn at the new length.
This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.
You will now write a program named feline that
uses these classes.
In a 200 by 200 window, create a CatFace object
with a center position of (100, 100).
Use the mousePressed() function to detect mouse clicks.
When the left button is pressed: if (mouseButton == LEFT),
turn the cat’s eyes light blue, otherwise change them to green.
Use the keyPressed() function to detect keypresses.
When the user presses a lower-case ‘w’:
if (key == 'w'), shorten the whiskers
by 5 pixels. When the user presses an upper-case ‘W,’
make the whiskers 5 pixels longer. Notice that you must use
single quotes around
'w' and 'W'
because the key is a single character.
Zip up both sketches into a zip file named
objects.zip and upload it.