CSC 120 Lab 3

In this lab we will:
  1. Take a quick look at the Java Graphics Tutorial page to see some available methods of the Graphics class.

    1. Refer back to the Graphics Tutorial page as needed, while working on this lab.

  2. Create a new NetBeans project called Lab3 & Copy the Boat & MUPanel classes.

    1. Visit the CSC 120 Resources Page, click the link at the bottom of the page to "MU Java Classes" and save the Boat.java file to the src folder of your project.

    2. In your web browser right-click on this MUPanel.java and save the file into the src folder of your Lab3 project.

    3. In NetBeans open the MUPanel.java file to edit it. Look over the comment header at the top, and change the name and date to your name and today's date. In the MUPanel constructor, set the panel name to "CSC 120 Lab 3 authored by" whatever your name is (instead of "Java Program authored by Samantha Student").

    4. Notice where we have declared a Boat object called boat1, instantiated/constructed the Boat object passing specific values for the name of the Boat and where it is located in the display window to the Boat constructor method, and used the Boat object by calling its draw method and its toString method.

    5. Run the program to see what it does. System.out.println "prints" (displays) stuff in the Java Console window (the Output window in the bottom part of NetBeans). System.out.println always expects you to pass it a String parameter or stuff that is concatenated (glued together) to make a String. Using + with literal numbers and integer variables adds them together as expected, but using + between a literal String or a String variable and another String or even a number does String concatenation. We'll use String concatenation and System.out.println often.

  3. Edit the project's MUPanel.java file to declare, instantiate, and use more Boats.

    1. In the MUPanel.java file edit the Java code to declare Boat objects called boat2 and boat3. You may either add a separate line for each declaration and mimic the style we used to declare boat1, or you may declare all three Boat objects with one line of code. Don't forget the semicolon on the end of every line.

    2. In the constructor method of the MUPanel.java file, where we have instantiated our boat1, add lines of Java code to instantiate boat2 and boat3, using different values for the overs and downs and the names of the boats. Also, add lines of Java code to use System.out.println and the toString method of these Boats to display information about them in the Java Console.

    3. In the paintComponent method of the MUPanel.java file we have asked boat1 to draw itself. Add lines of Java code to ask the other Boats to draw themselves.

    4. Run the program, fix any errors that are noted, etc. until you get it to run properly, showing all three Boats in the program display window and printing information about all three Boats in the Java Console at the bottom of the NetBeans window.

  4. Edit the Boat.java file to make changes to the Boat class.

    1. Click HERE to see an example of how your Boats should look after you change the Boat class. Your Boats may not look exactly like this: the positions (overs & downs) will be different, the names will be different, and the port holes may be located differently, but this shows you the ideas of the names on the sails, the port holes, and the mast support lines.

    2. Open the Boat.java file and make the following changes. You are strongly encouraged to draw a picture of a Boat on paper, and label coordinates and distances to help you figure out what values to use in the following drawOvals, fillOvals, drawString, and drawLines!!

      1. In the draw method right after the comment "// port holes" add lines of Java code to make three port holes. For each port hole set the color for the Graphics object called g to Color.YELLOW, use the fillOval method of g to make a solid yellow circle, set the g color to Color.BLACK, and finally use the drawOval method to make a hollow black oval outlining the yellow oval.

        NOTE:  All of your code MUST draw the shapes that will make the portholes relative to the anchorX and anchorY points of the Boat.  In other words:
        Do stuff like this: But NOT like this:
        g.drawOval(anchorX – 15, anchorY + 30, 20, 20); g.drawOval(85, 215, 20, 20);

        Rerun the project, then re-edit Boat.java as needed, getting your port holes the way you want them before proceeding.

      2. In the draw method right after the comment "// name on the sail" add lines of Java code to set the color to Color.BLACK and use the drawString method of the g object to display the boatName property of the Boat on the sail.  Hint:  you cannot use a string literal surrounded by "" characters in the g.drawString method call to do this.

        Rerun, fix, run, test, fix, etc.

      3. In the draw method right after the comment "// lines steadying the mast" add code to set the color to Color.YELLOW and use the drawLine method of the g object to draw lines from the front and back of the Boat to the top of the mast. Recompile, run, test, fix, etc.

      4. Continue running the program, fixing any errors that are noted, etc. until you get it to run without errors and display appropriately.

      5. You may add other features to your Boat class, if you like.

  5. Add a fourth Boat object.

    1. Edit the MUPanel.java file to declare, instantiate and draw a fourth Boat object so that it is displayed on the screen in a way that doesn't overlap the other boats, and so that it has a different name than the other boats shown on its sail.  Do NOT make any changes to the Boat class to do this.

    2. If you wrote the draw method of your Boat class correctly, your fourth boat will have portholes in the proper place, lines from the mast to the boat deck, etc.  If you used numbers instead of drawing things relative to the (anchorX, anchorY) point of the Boat, then your new boat won't have portholes.  Ask your instructor for help if your fourth boat isn't displayed correctly.

  6. Hand in your Lab3 Project.

    1. Use Windows Explorer to convert the CSC120\Java\Lab3 folder into a .zip file by right-clicking on the Lab3 folder and choosing "Send To > Compressed (zipped) folder".

    2. Then log into the D2L homepage, go to CSC 120 in your list of courses, choose "Assessments | Dropbox", and upload/submit the Lab3.zip file into the Lab3 dropbox using the techniques from the previous labs.  Ask you instructor for help if you need it.

  7. OPTIONAL:  For more programming practice:

    1. You don't have to complete this part of the lab, but if you have time and would like to try to add some extra features to your Boat objects, you could enhance the draw method of the Boat class with additional code.  Be sure to draw everything relative to the (anchorX, anchorY) point so that the features are part of every Boat object.

    2. Here are some extra features that students in previous semesters have drawn:

      1. Some wooden pieces on the front and back of the boats

      2. A crew member on deck!

      3. A cannon and 3 cannonballs on deck, with a rudder at the bottom of the back of each ship

      4. Pirate ships challenging the boats, written by a student with LOTS of free time!