Chris Pollett>Old Classes>PIC 20, Winter 2000>Hw6

Winter 2000 PIC 20 HW #6

Programs are due in your \submit folder by 5:30 March 17

For this assignment you will submit the following files: AqFrame.java, MyTimer.java, Aquarium.java, AqComponent.java, Fish.java, Seaweed.java, Fish1.gif, Fish2.gif, Fish3.gif, Seaweed1.gif, Seaweed2.gif, and Seaweed3.gif. The point of this assignment is to get you familiar with menus, exceptions, threads, and multimedia in Java.

Here is my version of the application:

An applet consisting of a button that when clicked creates a new window in which a fish tank is drawn.

The goal is to write an application, AqFrame (which extends JFrame) with a menubar that has the following components: The menubar has a menu with the name Options. Under Option you can select the New submenu on which the two options Fish and Seaweed appear. Beneath the new menu there should be a MenuItem clear to clear the aquarium and CheckBoxMenuItem Sound. Selecting either Fish or Seaweed pops up a new window in which the Fish or Seaweed Object can be configured before placing it in the aquarium. The AqFrame has an AudioFile which it plays in loop mode unless the Sound CheckBoxMenuItem is not set. The AqFrame also has on it an Aquarium object. An Aquarium is a class that extends JPanel that you will need to write. The Aquarium classes add method should override the JPanel add method and is used to add an AqComponent to the Aquarium. The Aquarium also has a method move() which calls the move() method of each AqComponent that has been added. The AqFrame should have a MyTimer which every so many milliseconds calls the Aquarium move() method and then does a repaint() of itself.

MyTimer is a class that extends Thread (not allowed to extend Timer) written by you, but which otherwise does the same thing as the built-in Java class Timer. That is it should have a constructor MyTimer(int m, ActionListener act) so that act.actionPerformed(ActionEvent e) is called every m milliseconds. MyTimer should also implement the functions isRunning(), restart(), myStop() acts like the Timer classes stop() method. also

The drawing of the aquarium should have a sky, the border of the tank, the water in the tank and a sandy bottom to the tank. After filling out the new dialog for a Fish or Seaweed object in the Fish or Seaweed is then drawn in the aquarium. However, Fish can only be added to the water part, Seaweed can only by added to the sandy bottom.

The AqComponent class has four fields an ImageIcon icon, and float xLoc, float yLoc, float scale for its location within the aquarium. All the variables except scale appear in the constructor for AqComponent. The value of scale is set to 0.5. The float fields are intended to range from 0 and 1. The meaning of say xLoc =0 is the far left of the Aquarium. Floats were used since it makes it easier to resize the Aquarium if the window changes size. AqComponent has a move method that does nothing.

Both Seaweed and Fish extend AqComponent. Fish also contains float fields xVel and yVel. (These should range over small values between say 1/1000 between 1/100) When a Fish's move() method is called it changes the xLoc and yLoc according to xVel and yVel. If the resulting value greater than 1 or less than 0, xVel is set to -xVel or yVel is set to -yVel depending on which direction caused the movement out of bounds. Seaweed contains a constructor which in addition to the three fields of the AqComponent constructor uses the scale field. The idea of the scale field is that it is multiplied against the dimension of its icon to get how big the Seaweed is drawn as. The window that pops up to create a new Seaweed or a new Fish should have a JList to allow the user to choose from a list of ImageIcons you predefine. There should be sliders to alter the fields as in my example and there should be a Create and Cancel button on the bottom. Create makes the Fish or Seaweed, Cancel gets rid of the window (by calling the dispose() method of JFrame). The window for a Fish should have labels and sliders to get the xVel and yVel. The window for a Seaweed should have labels and a slider to get the scale.

Since the class AudioClip lives inside java.applet.*, you will need to import this package. You will also need to import java.net.* to be able to create URL objects. The constructor for a URL object might throw a MalformedURLException. So when you load in your AudioClip using


     AudioClip myClip = Applet.newAudioClip( new URL("whatever url"));   

you will need a try catch block (the catch block should print an error). For what you turn into your submit folder make sure the sound URL is "http://www.math.ucla.edu/~cpollett/20.1.00w/sparcle.wav".

Remember to include program documentation and your honesty pledge.

Homework 6 FAQ.

Homework 6 Solution.