Lab8StarterFiles.zipUnzip and Extract All of the files, and copy ALL FOUR of the files you unzipped into the src folder of your project.
RoundHead
objects.
Run the new project to make sure that things are working
before
proceeding.RoundHead
objects "firstOne", "secondOne", etc.RoundHead
objects, and in their place, declare an array of RoundHead
s
called rhArray
.
private RoundHead[] rhArray;In the
MUPanel
constructor method, replace the
instantiations of the separate RoundHead
objects, and
instead instantiate rhArray
so that it contains
19
elements. Then, write a loop that will be repeated once for
each
element in the array.
rhArray = new RoundHead[???]; for (Integer index = 0; index < rhArray.length; index++) { rhArray[index] = new RoundHead( ?, ?, ?, ?, ?, ?, ?, ? ); } // end of for loopThe body of this loop should instantiate one member of the
rhArray
array, using these parameter values: MUPanel
class
that ask RoundHeads to
move, change their mood, and draw themselves. In each of
these methods
in the MUPanel
class, add a for
loop
to call the appropriate method for each RoundHead
object
in the rhArray
array. Run this program and
observe the
results. The output should show 19 roundheads in a diagonal
line, and
they should move, change moods and bounce off walls
correctly.MUPanel
constructor method where we instantiate new RoundHead
objects, as the FIRST line in the loop body, declare a Boolean variable called
isHappy
.
If
the
loop control variable is an EVEN NUMBER, store the value true
in isHappy
; else, store false in isHappy
.
Then
replace
the
true in the constructor call with isHappy
.
The constructor call must be the LAST statement inside the for loop body.
Recompile
and
run
the program. Did it work? Star
class into the src folder of
your
project.In your browser right click on:
and choose Save Target As... and save the file in the src folder of your project.
Star
objects at the top
of
the MUPanel
class in the MUPanel.java
file. MUPanel
class in the MUPanel.java
file, include this object declaration and instantiation:
private Random randGenerator = new Random();(Note: a light bulb will appear beside this line; just click on it and then select the Add import for java.util.Random item from the menu that pops up.
Star
objects in the
constructor method of the MUPanel
class so
that the
array contains 100 elements. Star
objects. The body
of the loop
should:
Integer
variables called x
and y
x
to a random integer
in the range from 0 to 590 y
to a random integer
in the range from 0 to 390 x
and y
as the two
parameters that
are passed into the Star
constructor method. Integer
variable randNum
to a random integer in the range from 0 to upper – 1:
randNum = randGenerator.nextInt(upper);
Put lines similar to this one inside
the for
loop to
store random numbers into x
and y
.
In the paintComponent
method of the MUPanel.java
file, write code that uses a loop to call the draw
method
of each Star
object in the array. Place the
loop that draws
the stars
BEFORE you draw the RoundHeads in the paintComponent
method.
If you run the program now, you won't see any stars, because they are white, and they are being drawn on a white background. Change the program so that the background of the output window is Color.BLACK, and run the program.
paintComponent
method.
Add two new methods to MUPanel.java, as follows. The first method is named "moveGeorge", and it is a void
method that accepts no parameters. In the moveGeorge
method, call the moveVertically
method and
also the moveHorizontally
method for the george object. The second method is named "changeGeorgesMood", and
it also is a void method that accepts no parameters. The changeGeorgesMood
method should call the
changeMood
method for the george object.
Switch to MUFrame.java in the editor, and change to "Design" mode. Add two new buttons to the interface, one labeled "Move George" and the other named "Change George's Mood". The actionPerformed methods for these two buttons should call the appropriate methods of the muPanel object that you wrote in the previous instruction, and then ask muPanel to repaint(). Test the program now, and the new buttons should cause George to move or change mood while the other RoundHeads remain stationary.
Now let's modify MUPanel so that the RoundHeads in the array move on the screen even if we aren't clicking the Move Roundheads button. To do this, start by modifying the class heading for the MUPanel class so that it looks like this:
public class MUPanel extends JPanel implements Runnable {Next, copy the following code and paste it into MUPanel.java right above the comment that says, "Don't change anything below here":
private volatile Thread animator; private Integer animationDelay = 200; public void start() { if (animator == null) { animator = new Thread(this); animator.start(); } // end if } // end of start() public void stop() { animator = null; } // end of stop() public void run() { Thread thisThread = Thread.currentThread(); while (animator == thisThread) { moveObjects(); repaint(); try { Thread.sleep(animationDelay); } catch (InterruptedException e) { } } // end while } // end of run()Then, switch to MUFrame and add two more buttons to the interface, one named "Start Animation" and the other named "Stop Animation". When the Start Animation button is clicked, you should call
muPanel.start()
; when the Stop Animation button is clicked, you should call
muPanel.stop()
. Run the program, and try out the new buttons.
Can you move George around the screen and avoid the marching RoundHeads?
If you would want the animation to start as soon as the program starts, there is a simple way
to do that. In MUFrame.java, scroll to the end of the constructor method, and then right before the
closing brace for the method, call the muPanel.start()
method. The constructor
is executed only once, when the program starts, and so the animation will begin right away instead
of waiting for the user to click the Start Animation button. You should be able to stop
and start the animation with the buttons as before.
The variable named animationDelay
specifies the delay that will be used between
repaintings, and is a number of milliseconds (thousandths of a second). You can make the animation
effects happen faster by using a smaller number for the animation delay, or slow it down with a bigger
number for the delay.
Try changing the value of animationDelay
in MUPanel.java, using 500 (which is a ½ second delay), then
try 100 (a tenth-of-a-second delay), and finally try 25 — that's really fast! Change the delay a final time to
a value that you like.
Add randomness to the instantiation of the RoundHead objects in your program so that the horizontal and vertical movement factors used for each RoundHead in the array is a random integer in the range from -30 to 30. Run the program and watch the RoundHeads break out of their initial positions unpredictably.
ShootingStar
class
definition
based on the Star
class that draws something
that looks
like a shooting star, and then declare, instantiate and draw
an array
of 10 shooting stars. The shooting stars should be
randomly
placed in the output window, in a manner that is similar to
the way the Star
s were placed. You could add a button or two to the interface to increase or decrease the animationDelay
value to speed up or slow down the
animation as it is running. To do that, you would need a setAnimationDelay
method in MUPanel that could
be called from the actionPerformed methods in MUFrame. Or maybe you would like to try a Spinner or a Slider
for changing the animationDelay....