Guest User

Untitled

a guest
May 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. CS 1331 Homework 9
  2. Due: Monday, November 10, 2008
  3. New due date: Wednesday, November 12, 2008
  4. Introduction
  5.  
  6. This assignment will give you some experience working with classes, subclasses, interfaces, polymorphism and dynamic binding. You will develop some classes that will be interrelated via has-a and is-a relationships. The program will have you develop a simple simulation in which the classes you create are used by a third party driver (main) program. This assignment is relatively open-ended so you should be creative and have some fun with it.
  7.  
  8. Remember that you are free to collaborate with other students on the homework as long as you acknowledge the collaboration in your collaboration statement. Because this program requires several related classes, it lends itself more to teamwork development, although you are still responsible for understanding the concepts involved in every part of the program.
  9. Here Fishy, Fishy
  10.  
  11. In this program you will develop a simple aquarium simulation. The aquarium will be modeled by a class itself, and then each of the objects inside it will have their own class too. We will set up specific methods for each class that you must implement.
  12.  
  13. To begin, an aquarium is a rectangle with water inside. (We suggest a color like (109, 171, 177) for a nice sea blue.) An aquarium should simply be a subclass of a JFrame. Aquariums hold a number of background-style objects such as sand, plants, and aerators. We will create an interface that models background objects and each such object must implement this interface. An aquarium then will also have an addObject() method that affiliates one of these objects with the aquarium. Finally, the aquarium will have a refresh() method that redraws the aquarium in the following order: First redraw the background, then draw each object in the order they were added. This effectively wipes clean everything that was there before.
  14.  
  15. Here are the respective classes, interfaces and methods that you must implement. Feel free to add any methods that you want. The ones listed here are only the minimal subset that must be provided.
  16.  
  17. public class Aquarium extends JFrame {
  18. public Aquarium(int width, int height)
  19. public Color getWaterColor()
  20. public Dimension getSize()
  21. public Graphics getGraphicsContext()
  22. public void addObject(AquariumObject ao)
  23. public void refresh(Graphics g) // Note that a Graphics parameter is required to use the updated AquariumTest!
  24. // See the "double-buffering" section below for details
  25. }
  26.  
  27. Here is the interface for an AquariumObject. Its source code is in the file AquariumObject.java. Again, you may add additional methods to the interface or to Aquarium.java if you wish, but you must keep the existing one so that your program will still work with our provided test file (explained below).
  28.  
  29. public interface AquariumObject {
  30. public void paint(Graphics g);
  31. }
  32.  
  33. Here are two AquariumObjects that you must create. Also, feel free to create any other AquariumObject classes that you would like to add to make your aquarium look good.
  34.  
  35. public class Sand implements AquariumObject { // Simulates the
  36. // gravel or sand that sits on the bottom of an aquarium
  37.  
  38. public Sand(int x, int y, int width) // The parameters specify
  39. // the bottom-center position of the sand within its
  40. // graphical context and the width specifies how wide it
  41. // should be. It should be relatively short too.
  42. public void paint(Graphics g) // Redraw the sand
  43. }
  44.  
  45. public class Aerator implements AquariumObject { // Represents an
  46. // aquarium air bubbler
  47.  
  48. public Aerator(int x, int y, int height) // The x and y parameters
  49. // denote the bottom center position of the aerator and the
  50. // height parameter specifies how high the bubbles should go
  51. // above that coordinate.
  52. public void paint(Graphics g) // Redraw the aerator. NOTE:
  53. // subsequent calls to paint should slightly change the
  54. // positions of bubbles so it looks like the aerator is running
  55. // and sending bubbles up to the surface.
  56. }
  57.  
  58. OK, now that we've dealt with the background elements, we can provide the more fun elements, the aquarium's inhabitants.
  59.  
  60. For fish, we will have a superclass Fish that represents a stationary fish. There are two constructors. One is more tightly specified and the other is designed to be more random and free. Fish will have a subclass MovingFish that has the ability to swim around to different positions. The call setBounds() on Moving Fish is important to define the edges of the aquarium that they should bump up against but not go outside. The important difference on a MovingFish is that each subsequent call to paint() should slightly move the fish to a different position, thus giving the impression that the fish is swimming. Think about how fish move---don't just have it randomly wiggle. Be creative.
  61.  
  62. public class Fish implements AquariumObject {
  63. public Fish(Color c, Polygon p, int x, int y) // Use the
  64. // specified polygon as the fish outline. The coords are
  65. // relative to the x,y. Make it the given solid color.
  66. public Fish(int x, int y) // Create some random (but decent)
  67. // looking fish at the given position
  68. public void paint(Graphics g) // Draw the fish
  69. }
  70.  
  71. public class MovingFish extends Fish implements AquariumObject {
  72. public MovingFish(Color c, Polygon p, int x, int y) // Use the
  73. // specified polygon as the fish outline. The coords are
  74. // relative to the x,y. Make it the given solid color.
  75. public MovingFish(int x, int y) // Create some random (but decent)
  76. // looking fish at the given position
  77. public void setBounds(int minx, int miny, int maxx, int maxy) // Defines
  78. // the boundaries for swimming
  79. public void paint(Graphics g) // Draw the fish
  80. }
  81.  
  82. Feel free to define your own subclasses of Fish and/or MovingFish. You might create an AngelFish, CatFish or some such animal that has its own unique look and behavior. You can add more methods to these and the superclasses too, but the objects should be able to run using only the ones specified above as a minimal, but sufficient, subset.
  83.  
  84. In testing your program, we will use two example driver classes with main() in them. In the first, we will design a basic driver that looks much like the example below. This will test the basic minimal, functionality of your program. Source code for this file can be found in AquariumTest.java. Update (11/7/08): AquariumTest now uses double-buffering. The link points to the new version, see the "double-buffering" section below for details.
  85.  
  86. Remember, if you modify AquariumObject, your program must still work with AquariumTest.
  87.  
  88. The second class should be created by you. Call your class AquariumSimulation. In it, you should show off all the interesting and different features you added. For instance, you might create instances of any fish subclasses you created. Have fun with this one!
  89. Double-Buffering
  90.  
  91. If you started the homework before 11/7/08, you may have noticed the aquarium tends to flicker badly if you have a lot of objects. The flickering is ok--you can turn in your program with the flickering for no penalty. However, I have updated AquariumTest.java to use double-buffering instead which should help smooth the animation. In order to use the new AquariumTest, you must modify your Aquarium's refresh method to take a Graphics object. If you've done the update method right, you should only need to update two lines in the method with the new parameter.
  92.  
  93. If you do not want to update your program to use the new file, you will not lose points. The old version is no longer available on T-Square but you can find the source code at the end of this assignment description. It doesn't matter which version of AquariumTest you choose, just be sure to turn in a version that compiles with your program.
  94. Extra Credit
  95.  
  96. You can earn up to twenty additional points of extra credit on this assignment (i.e. the maximum score is 70/50) for creativity, innovative features, and generally going above and beyond the minimum requirements. Examples of additional features might include new decorations for the aquarium, fish that age and "die" after a while, controls to manipulate the aquarium (maybe a button to add more fish?), etc.
  97.  
  98. Here are even more ideas for other objects you might add to your aquarium:
  99.  
  100. public class UnderwaterPlant implements AquariumObject { // Represents
  101. // a plant. Feel free to make each one created have its
  102. // own unique appearance.
  103.  
  104. public UnderwaterPlant(int x, int y, int width, int height)
  105. // The x and y parameters denote the bottom center coordinate
  106. // of the plant. The width and height denote the maximum size
  107. // this plant can be (smaller is OK).
  108. public void paint(Graphics g) // Redraw the plant
  109. }
  110.  
  111. public class Crab implements AquariumObject {
  112. public Crab(int x, int y) // The x and y parameters denote the
  113. // bottom-center of the crab. You can make it some arbitrary
  114. // (possibly random) reasonable size.
  115. public void paint(Graphics g) // Draw the crab
  116. }
  117.  
  118. and of course, feel free to create any other aquarium decorations you like. The more effort you demonstrate, the more extra credit you will earn (up to 20 additional points). Include a readme.txt file where you list out your additional features.
  119. Turn-in Procedures
  120.  
  121. After you have finished your program, turn the files in via T-Square. You will be submitting multiple files, including the files provided to you (AquariumObject.java and AquariumTest.java). Please make sure they are named as shown below:
  122.  
  123. * Aquarium.java
  124. * AquariumObject.java
  125. * AquariumTest.java
  126. * Sand.java
  127. * Aerator.java
  128. * Fish.java
  129. * MovingFish.java
  130. * AquariumSimulation.java
  131. * Any other classes that you create for use in the assignment
  132. * If you added extra features, include a readme.txt file listing them.
  133.  
  134. Make sure you have turned in ALL files required for your program to compile! You should be able to download your submission from T-Square and compile and run it without any additional files.
  135. Source code for the original, non-buffered AquariumTest.java
  136.  
  137. import java.awt.*;
  138. import java.awt.event.*;
  139. import javax.swing.*;
  140.  
  141. public class AquariumTest {
  142. private final int DELAY = 20;
  143. private Aquarium aquarium;
  144. private Fish[] fish;
  145. private Graphics g;
  146. private Timer timer;
  147. private int count = 0;
  148.  
  149. public static void main(String[] args) {
  150. AquariumTest at = new AquariumTest();
  151. at.start();
  152. }
  153.  
  154. public void start() {
  155. int[] x = {0, 30, 40, 50, 60, 70, 90, 100, 115,
  156. 130, 115, 100, 90, 70, 60, 50, 40, 30, 0, 15};
  157. int[] y = {5, 15, 10, 7, 0, 7, 12, 15, 22, 25, 28, 35, 40,
  158. 43, 50, 43, 40, 35, 45, 25};
  159.  
  160. aquarium = new Aquarium(800,600);
  161. g = aquarium.getGraphicsContext();
  162.  
  163. Sand s = new Sand(400,600,800);
  164. aquarium.addObject(s);
  165.  
  166. Aerator a = new Aerator(700, 550, 550);
  167. aquarium.addObject(a);
  168.  
  169. aquarium.refresh();
  170.  
  171. Polygon poly = new Polygon(x, y, x.length);
  172. fish = new Fish[4];
  173. fish[0] = new Fish(280, 120);
  174. fish[1] = new Fish(new Color(234,200,167), poly, 560, 400);
  175. fish[2] = new MovingFish(315, 567);
  176. ((MovingFish)fish[2]).setBounds(0,0,800,600);
  177. fish[3] = new MovingFish(new Color(214,30,230), poly, 700, 120);
  178. ((MovingFish)fish[3]).setBounds(0,0,800,600);
  179.  
  180. timer = new Timer(DELAY, new AnimListener());
  181. timer.start();
  182. }
  183.  
  184. public void nextFrame() {
  185. aquarium.refresh();
  186. for (int i=0; i<fish.length; i++)
  187. fish[i].paint(g);
  188. }
  189.  
  190. private class AnimListener implements ActionListener {
  191.  
  192. public void actionPerformed(ActionEvent ae) {
  193. nextFrame();
  194.  
  195. count++;
  196. if (count == 1000)
  197. timer.stop();
  198. }
  199. }
  200. }
Add Comment
Please, Sign In to add comment