Lab #4a

 

Course

Instructor

CSC 101 - Spring 2003

Clark S. Turner

Fundamentals of Computer Science I

Email: csturner@csc.calpoly.edu

Section 02 and 03

Website: http://www.csc.calpoly.edu/~csturner

Labs

Office: 14-211

Phone: 756-6133

Office Hours listed on main page.                      

 

Objectives

  1. Modify your ClickMe Applet
  2. Use BlueJ
  3. or other means to test your applet.
  4. If time permits, add comments and run Javadoc

Modify your ClickMe Applet

In this lab you will modify the ClickMe applet from the second lab so it will draw circles of random size and color.  Make a new directory called ClickMeColor and copy the files to the new directory: ClickMe.java and Spot.java.  Open BlueJ or other means, to open the directory 

Make the following changes to the Spot class: 

  1. Make all instance variables private (x, y, and size).  
  2. Write accessor and mutator methods to set and get each instance variable.  

Make the following changes to the ClickMe Applet 

  1. Change the class name to ClickMeColor
  2. Change all references to public instance variables so the program now calls public accessor or mutator methods.  (Note: look for all places where spot.x and spot.y are used)
  3. Test your applet and make sure it still works as it did in Lab3.

The next step is to create a random number generator to get the red/green/blue values and create a random color.  You will change the program so each click draws a circle using a random color.

  1. Write a method called getRandom that returns an integer between 0 and a maximum number.  This method should accept an integer that is the max value of the random number to be generated.  Remember Math.random() returns a double between 0 and 1.  Your getRandom function should return an int between 0 and max.  See Math functions in textbook.
  2. Just before the program prints the spot, declare a variable "randomColor" that will be used to reference a new Color object defining a random color.  You will call the Color constructor defined in the Java API that requires three integer arguments (between 0 and 255) for the red/green and blue values.  (look at instructions for the following constructor Color(int r, int g, int b)). (See instantiation of the Color Purple in the ClickMePurple class created in previous lab, but note that it used float values instead of integers).  
  3. Change the color (using setColor method) to your new randomColor instead of Color.red.
  4. Test your applet.   It should draw a circle of random color when/where you click the screen.

After you are convinced your applet is working correctly, you can modify it so each click will draw a circle of a random size between 0 and 100.

  1. Your applet will no longer use the constant for RADIUS.  Delete the declaration for RADIUS.
  2. Change all appropriate references to RADIUS to call Spot's accessor method to get the spot's size.  
  3. You will also need to find the appropriate places to set the spot's size to a random value when the spot is create and each time the screen is clicked.

Use BlueJ or other tool to test your Applet

When Spot.java and ClickMeColor.java have compiled successfully, you can run your applet from the BlueJ main window.  Right click on the ClickMeColor icon and select Run.

If time permits, add comments and run Javadoc

Your classes should have header comments as you have done in previous labs.  You should also write a comment before each method.  Comments must start with /** and end with */ if you want to have them seen in javadocs.  Be sure to use the following tags (as appropriate) in the comments for your methods.

@param paramname description if the method has one or more parameters and

@return followed by a description of the return value if the method returns a value

Run javadoc, to make sure your comments display correctly.  Use the following command:

S:\   \ClickMePurple> javadoc –author –version Time.java

(Note:  –author and –version are used if you want to see the author and version in the javadoc).

Have your program checked as working by Instructor or Lab Assistant.

            Needed Files: ClickMeColor.java and Spot.java

 


Log out of Windows 2000

Logout of Windows-2000 -- NOTE: You must ALWAYS logout.  NEVER walk away without doing so!

Shutdown the PC -- NOTE: Just turning off the power switch is NOT the correct procedure!