Java代写:CS104 Drawing Fish and Pond

用Java Swing绘制池塘,然后实现钓鱼的逻辑。

Individual Assignment

Create a shoal of fish and do fishing with them using objects, array, randomness, and mouse interaction

General Requirements & Learning Objectives

Submission and Grading Requirements

  • The UML Design document must be in PDF, named following the format below.
  • For the code the entire project MUST be submitted (not just the source files)
  • The project must be named (in Eclipse) with the following format LabNumber_FirstName_LastName_AssignmentNumber_StudentNumber, e.g. D104_John_Smith_Assignment2_ 1234567
  • To submit, export the project (including all the libraries used) into a zip file (Archive File) and name it with exactly the same name as the project (Please note failure to meet any of these submission requirements above would result in a penalty of 0.25 pt each)
  • No late submission will be accepted. If you do not complete the assignment by the deadline, you will receive 0. For a legitimate reason a late submission might be allowed pending discussion with your TA before the deadline. You may be required to provide the supporting documents.
  • For the coding check-off, make sure your code is syntax error free so that it runs properly on the lab machine. You would receive 0 for the coding part if your code failed to run due to syntax errors.

Learning objectives

  • Work with object array and randomness
  • Use AffineTransform for saving and restoring drawing space
  • Detect collisions among objects
  • Use MouseAdapter class (rather than MouseListener interface) to create mouse-based interactions

Programming Requirements

  • First of all, this assignment must be done using Java System Library (e.g. using Graphics2D for drawing). You will get 0 if you use Processing library (e.g. using PApplet for drawing) or any other 3rd party libraries.
  • This assignment has two iterative steps: Milestone: initial UML design and code for drawing the fishes and Pond, and Final Deliverable: updated UML design and code for fishing
  • For the UML design document, you are graded on completeness and correctness, and for the code on completeness and visual style.

Milestone Code: Design and Drawing Fish and Pond

Using the fish and pond that you’ve created in assignment 1 as the template, redraw them using Graphics2D object and the relevant geom primitive objects. Here are the requirements.

UML Design

Draw the initial UML that should include the Fish class that is designed with strict Encapsulation with minimal public interface methods and meet the following requirements:

  • a. The fish (as fields) and pond (including its decoration objects) must consist of geom primitive objects - except for triangles and quadrangles that you can still use the drawPolygon method to fill/draw for now - and involve at least one feature made of Quadratic or Cubic curve
  • b. Beyond drawFish (must involve translate) and update (or move) methods, it must have the methods for the followings:
    • At least one setter method for setting the locations and sizes of your fish components (Please refer to the setBodyAttributes or setHeadAttributes method of Ladybug for example)
    • Boundary detection
  • c. Design FishPanel class and its relationship with the Fish class (aggregation of a Fish object). Then add as fields a Timer object and pond object of appropriate geom primitive class
  • d. Create FishApp class as a subclass of JFrame, and associate it with the FishPanel class
  • e. The UML diagram must follow the UML notations for class and encapsulation. Also the names of the class, fields and methods must follow the naming convention with appropriate upper- or lower case for the initial and CamelCase thereafter.

Coding per the design

  • Like Assignment 1, the pond size must be smaller than the display window by at least 10 pixels on each of the sides except for the top. As you need to leave enough space for the fishing line (see Fig 1 below), so make sure you have a margin for at least 100 pixels above the pond. The pond should have at least two dcor objects drawn using geom primitive shapes
  • Make your fish move left and right in the pond
  • They reverse and flip around when hitting left/right edges of the pond
  • When the fish turns around, its tail must not go out of the boundaries of the pond

Submission of milestone: submit both the initial UML design (in PDF) and code (in zip) per the submission requirements and the deadline as specified in Section 1.

Final deliverable: Fishing

Update the UML design

Update the UML design to include an array of fish with length of 10, a collision detection (against another fish), and then an inner classes (you can download the membership notation from Canvas|Resource and used it in the UML to indicate the nested relationship), which extends MouseAdaptor and MouseMotionAdaptor inner classes for mouse interaction per the features for fishing below.

Coding per the design

Create a shoal of fish that consists of about 10 fishes that must meet the following requirements:

  • You must use an array to hold your fish objects
  • The fishes must be in randomized sizes and colors
  • They must start at random locations within the pond
  • They must move in wavy form (up and down based on sin or cos function)
  • They bounce off each other and turn around when colliding
  • Collision detection (against another fish)

Draw a fishing line with a hook, which is hanging down (vertically) from and movable with the mouse (please refer to Fig 1, you don’t really need the bait though). Please note draw the fishing line & hook only when the mouse is outside the pond, make them disappear if the mouse moves into the pond or goes off the display window

  1. When the hook touches on any part of a fish, by pressing and holding the mouse left button, the fish is on hook - with its mouth touching on the hook and face turning up (i.e. rotate by 90 degree, please refer to Fig 2) - and while holding the mouse button, the fishing line would shrink (imagine this as you are reeling the fishing line with a roller) to drag the fish up until the line shrinks to 0. If you release the button in-between, the line, hook and fish would hang in there (stop moving)
  2. Once the fish is on hook, the fishing hook and the fish must stop responding to collision with other fish, and the fish should not be blocked by the pond’s top edge so that it can be pulled out of the pond
  3. Once the fishing line shrinks to 0, make the fish disappear, and by releasing the mouse, the fishing line restore to its original state (i.e. hanging down from the mouse and moving with it)
  4. You can only have one fish on hook at a time, i.e. when a fish is on hook, the hook must stop fishing with any other fish

Visual Style

Please note visual style counts! Try to be original and creative when designing your pond (including its dcor objects), fish, and hook, and there will be marks allocated for evaluating the esthetic aspects of your visual design. Beyond those required features listed above, depending on the last digit of your student number, you must have at least one of the following features for your fish*.

  • 0 : Your fish body must be decorated with scales made from arc
  • 1 : Your fish body must be decorated with stripes
  • 2 : Your fish body must be decorated with dots
  • 3 : Your fish body must be decorated with scales made of triangles
  • 4 : Your fish tail must be made of triangle(s)
  • 5 : Your fish tail must be made of arcs
  • 6 : Your fish tail must be made in shape like an opened scissors
  • 7 : Your two body fins must be made of one triangle and one quad
  • 8 : Your body fins must be made of one arc and one quad
  • 9 : Your body fins must be made of quads
  • If you feel that your required feature will affect your design as a whole, talk to your TA or instructor. We may relax the constraint if your design sounds reasonable.

Submission of Final Code/UML: submit these items per the submission requirements and the deadline as specified in Section 1.


You can win up to 2 pts bonus marks by adding some artificial intelligence features and/or animated features to your fish, e.g. fish approaches to the hook when there is some bait on it (up to 1pt), and flees when the hook without bait approaches it (up to 1pt), fish can even look for and hide behind/below decoration objects to avoid being hooked (up to 1pt), fish struggles with its body wobbling when it gets hooked(up to 1pt), or some other equivalent features that you might come up with. Please note the bonus caps at 2 pts even if the add-up of your bonus features goes over it.

Online assessment

To demonstrate that you learned the relevant concepts, you need to answer an online assessment on the work you’ve done.

The assessment will be posted in Canvas and available right after the due date. You’ll have 20 minutes to do it. Make sure you do it in time to get full credit for your assignment. Please note once the online test is closed by the end of its schedule. NO Make-Up is possible. So it is your responsibility to make sure you do it in time to get full credit for your assignment.