![]() Graphic by Keith Ohlfs |
OPTIONAL Problem Set 10
|
[CS111 Home Page] [Syllabus] [Assignments] [Documentation] [FAQ] [CS Dept.] [CWIS]
This is an entirely optional extra credit problem set. Your grade in the course will not be adversely affected if you completely ignore this problem set. However, any work that you do on this problem set can potentially earn extra credit points that will improve your course grade.
In particular, the main part of this assignment offers 15 points of problem set credit (compared to the 10 points on past problem sets). Every 3 points of problem set credit contributes 1 point out of 100 to your final grade. So earning a perfect score on this problem set can increase your course grade by 5 points out of 100.
Additionally, this problem set offers 10 more points of additional extra credit in the form of an open-ended extra credit challenge. Extra credit challenge submissions from this assignment (as well as from any previous assignment) will be accepted through 11:59pm on Monday, May 13.
The purpose of this problem set is to give you more experience with Java objects in the context of AnimationWorld. AnimationWorld is a fun context in which to experiment with Java objects you create from scratch.
All code for this assignment is available in the ps10_programs folder in the cs111 download directory on nike.
Spinner.java
from Task 1;
Marquee.java
from Task 2;
MorphingPolygon.java
from Task 3;
MyShowcase.java
and any other animation and sprite definitions from the Extra Credit Challenge.
ps10_programs
folder.
All problems on this assignment involve AnimationWorld, which was presented in lecture on May 2-3. As part of doing this assignment, you will probably want to review the details of AnimationWorld. You can do this by:
LectureShowcase
animations
available from /cs111/download/lec25_animations
.
You may also need to consult the contracts of various Java classes while doing the assignment. Real-life Java programmers spend much of their time browsing such contracts!
This problem set description contains several
embedded AnimationWorld applets.
Experience indicates that these applets usually
run OK in the currently installed version of
Internet Explorer but may have trouble
running in the currently installed version of Netscape.
If you cannot get the applets
to work in Netscape, try Internet Explorer instead.
If neither works, you can run all applets
in CodeWarrior from the Test
subfolder of ps10_programs
.
The following Spinners
animation shows "spinning"
disks of various sizes and colors:
In this problem, your goal is to flesh out the declaration
of the Spinner
class
(a subclass of Sprite
)
so that it describes the behavior of spinning two-colored disks.
Instances of Spinner
should be created via the following
constructor method:
public Spinner(int x, int y, int radius, int dRadius, Color color1, Color color2);
Creates a spinning disk with radiusradius
whose center is at the position (x
,y
) in the Java coordinate system. The disks appears to have two "sides", one of which is coloredcolor1
, and the other of which is coloredcolor2
. Initially a circle ofcolor1
should be displayed. The "spinning" motion is simulated by changing the width of the oval by the amount2*dRadius
at every invocation ofupdateState()
. The height of the oval should not change.
For example, the animation shown above is specified as follows:
import java.awt.*; class SpinnerAnimation extends Animation { public SpinnerAnimation() { addSprite(new Spinner(200,100,50,3,Color.red,Color.blue)); addSprite(new Spinner(250,150,125,1,Color.yellow,Color.green)); addSprite(new Spinner(300,200,50,5,Color.pink,Color.gray)); addSprite(new Spinner(400,200,100,2,Color.cyan,Color.magenta)); setNumberFrames(Animation.NO_MAX_FRAMES); } }
To complete this problem, you will have to
Spinner
class.
These should all be private
.
Spinner()
constructor method specified above.
resetState()
,
updateState()
, and drawState()
described in the
Sprite contract.
To test your Spinner
implementation, select
the Spinners
item in PS10Showcase
applet. A correct definition of Spinner
will give rise to the same behavior shown in the test applet.
The following Marquee
animation shows various
lines of text that scroll across the screen.
In this problem, your goal is to flesh out the declaration
of the Marquee
class
(a subclass of Sprite
)
so that it describes the behavior of text that scrolls
across the screen.
Instances of Marquee
should be created via the following
constructor method:
public Marquee(String text, Color color, Font font, int y, int dx);
Creates a scrolling line of text displaying the stringtext
in colorcolor
and fontfont
. The vertical position of the bottom of the text is determined by the Java y-coordinatey
. Ifdx
is negative, the leftmost point of the text starts at the far right end of the applet window and scrolls from right to left, shifting leftward by|dx|
units along the x-axis at each invocation ofupdateState()
. Ifdx
is positive, the rightmost point of the text starts at the far left end of the applet window and scrolls from left to right, shifting rightward by|dx|
units along the x-axis at each invocation ofupdateState()
. Whenever the line of text has scrolled completely off the screen, it begins scrolling again at the opposite side of the screen.
import java.awt.*; class MarqueeAnimation extends Animation { public MarqueeAnimation() { addSprite(new Marquee("Welcome to CS111", Color.magenta, new Font("Serif", Font.BOLD, 75), 100, -5)); addSprite(new Marquee("Buggles and bagels and pictures and turtles", Color.cyan, new Font("SansSerif", Font.ITALIC, 50), 200, -10)); addSprite(new Marquee("Help! I'm scrolling the wrong way!", Color.red, new Font("Monotype", Font.PLAIN, 30), 300, 1)); setNumberFrames(Animation.NO_MAX_FRAMES); } }
To complete this problem, you will have to
Marquee
class.
These should all be private
.
Marquee()
constructor method specified above.
resetState()
,
updateState()
, and drawState()
described in the
Sprite contract.
To test your Marquee
implementation, select
the Marquee
item in PS10Showcase
applet. A correct definition of Marquee
will give rise to the same behavior shown in the test applet.
Notes:
setFont()
and drawString()
methods of the Graphics
class. These are detailed
in the
Graphics contract
.
stringWidth()
method of an instance of the FontMetrics
class.
You can read about this in the FontMetrics
contract.
To get an instance of the FontMetrics
class
within an instance of Marquee
use the incantation this.canvas.getGraphics().getFontMetrics(font)
,
where font
is the desired font.
Here, canvas
references a Sprite
instance variable whose value is a Graphics User Interface
component that indicates the region of the screen in which
the sprite "lives".
Be careful -- canvas
is only
defined after a sprite is added to an animation.
Before that (particularly, within the Marquee()
constructor
method), canvas
is defined to be null
,
an any attempt to send it a message will result in a
NullPointerException
error.
dx
and a positive dx
differently.
The following animation shows a five-sided polygon that "morphs" (changes) over time. It behaves as if each corner of the polygon is a ball that bounces off the edges of the applet window:
The polygon in the above applet is created via the invocation
new MorphingPolygon(5,Color.magenta)
,
which specifies a blue polygon with 5 points. The initial
positions and velocities of the points are determined randomly
every time the animation is reset. Test this by clicking
the Reset button.
There are many other ways to invoke the MorphingPolygon
constructor, which are illustrated via other menu choices in
the above applet:
true
as a third parameter, the polygons will be drawn filled
rather than unfilled. For example, try new MorphingPolygon(5,Color.blue,true)
.
new MorphingPolygon(3)
.
new MorphingPolygon(Color.cyan)
.
new MorphingPolygon()
.
MorphingPolygon
class so that it describes
sprites with the behavior shown above. Your class should
supports all the different kinds of constructor methods
discussed above.
Notes:
dx
and dy
.
width
and height
instance variables of a Sprite
.
resetState()
method
is invoked on a MorphingPolygon
instance,
the positions and velocities of the polygon corners
should be randomly reset.
Randomizer.IntBetween(lo,hi)
to choose a random integer between lo
and hi
, inclusive.
dx
and dy
components
that are between -20 and 20.
BouncingBall
sprite studied in lecture 25. You should study
/cs111/download/lec25_animation/BouncingBall.java
as part of doing this problem.
MorphingPolygon
implementation, select
the Marquee
item in PS10Showcase
applet. A correct definition of Marquee
will give rise to the same behavior shown in the test applet.
You can use AnimationWorld to create dazzling animations that show off your artistry and your programming talents. Here is a chance to be creative and earn some extra credit points at the same time.
For this challenge, build an animation of your own design. You may use existing sprites that we have studied, but you should create and use at least one new kind of sprite from scratch.
The MyAnimation
subfolder of the
ps10_programs
folder contains
a copy of the sprites and animations shown
in lecture 25. This is a good starting point
for your own animations. You may edit the
existing files or create ones of your own.
Add any animations you crete to
MyShowcase.java
.
If you need help on any aspect of this challenge, do not hesitate to talk to the instructors and/or TAs.
The amount of extra credit awarded on this problem will be proportion to the creativity and artistry in your designs, as well as the level of technical challenge. You can earn up to 10 problem set points in extra credit on this challenge. As an example of an animation worth 10 points, see Erin Stadler's Fall'99 animation.