Urgenthomework logo
UrgentHomeWork
Live chat

Loading..

Active aquarium Sample Assignment

Write an application that displays an active aquarium with at least 3 fish of different colours swimming back and forth. Each fish should run on its own thread.

Active aquarium

A fish swims left-to-right until it bumps into the right wall, at which time it flips around and heads right-to-left until it bumps the left wall, and so on. Draw each fish at a different water depth (i.e. different y values) and, for simplicity, keep each fish at its constant depth. Have the fish swim at a different speeds (that is, each fish moves forward a slightly different number of pixels than the other fish in each step).

Active aquarium Sample Assignment

A few thoughts on design:

  • You’ll probably want a Fish class to model a fish, an Aquarium class to model the aquarium and driver class.
  • A fish should know its color, position, direction, speed and shape. It may need to know about the aquarium too (e.g. so it knows when it bumps the wall). It should know how to draw itself, flip its direction and so on.
  • Since each fish runs on a thread make sure you do the necessary things to make that happen.
  • Use a polygon to store the fish’s shape (keep your fish simple). Pass the polygon as one of the parameters to the Fish constructor.
  • There are some methods of the Polygon class that may be useful (e.g. translate, intersects, getBounds). There are also some public variables in that class which you might find helpful (xpoints, ypoints, npoints). Check them out in the Java API.
  • Define the initial coordinates for the polygon relative to x = 0. Adjust the x coordinates based on the fish’s new position as the fish swims forward (the translate method may be useful here).
  • For a fish to flip its direction, you’ll also have to modify the x coordinates of each point in the polygon (the getBounds method might be helpful here).
  • Each fish will either need a timer to control the animation (like we had in the bouncing happy face animation) or can use sleep.
  • You may have to play around a bit with fish speeds, timer delay, etc. to get a nice looking animation.
  • To achieve the appearance of movement you need to “undraw” the fish at its current location before drawing it at its new location. To “undraw”, you could simply redraw the fish (or more efficiently, a rectangle encompassing the fish) in the same color as the background. You could also achieve this by repainting and redrawing all fish.
Copyright © 2009-2023 UrgentHomework.com, All right reserved.