Writing a Java applet with moving objects
In this assignment, you are to extend the moving object examples shown
in class, using the
moving
objects applet
shown on Wednesday night.
Copy the example mObjApplet.java, including the classes mPoint.java,
mRectangle.java, mOval.java, and mTriangle.java from the link above.
-
First, you are to either write a new child class to add to the collection
of moving objects, or extend the existing classes by choosing
one
of the following:
-
Make a class mImage. It should be a child of mRectangle. The constructor
will have a URL parameter that allows it to download an image to store
in an instance variable of the class. Later, it will use the method g.drawImage
in its paint method to draw the image. Add "get" and "set" methods to change
the width and height of the image. And decide what other "get" and "set"
methods to allow.
-
Make a class mString. It should be a child of mPoint, but will be very
similar to mRectangle in keeping variables w and h for the bounding box
of the string (in addition to a variable for the string itself, and possibly
a font). The constructor will accept a location x,y and an initial string
(and font) and use the FontMetrics class to figure out what w and h are
for that string. Decide what "get" and "set" methods to allow. The methods
checkboundary and isInside can be the same as for mRectangle. The paint
method will use g.drawString to draw the string.
-
Change the class mPoint so that it has a boolean instance variable called
"fill". Then in each child class, test the value of fill to decide whether
to draw an outlined object or a filled object. (For example, for mRectangle,
you just decide whether to call g.drawRect or g.fillRect.) Add "get" and
"set" methods for the value of fill.
-
Write an applet similar mObjApplet.java that uses two or more instances
or the class that you wrote in part 1 above. Make sure that it tests the
various features of the class. For mImage, have the applet sometimes change
the size of the image. For mString, be sure to have different strings.
For the classes with the fill variable, change whether the objects are
filled or not.
What to put on your homework page for submission of the assignment:
Submit your assignment by linking the description of your problem and
program as described in the
homework guidelines
and also links to the applet itself and the Java source code.