CS 123 - Programming Assignment 8

In this assignment you will write a method for the Picture class that covers a picture with disjoint, non-overlapping circles. This sort of has the effect of looking like raindrops on the picture. This assignment makes use of Section 7.1.1 (pages 212-221) from the textbook. This assignment will also use arrays, for-loops, and conditional statements. Download this zip file and un-zip it. In the zip file there is a copy of this web page, which you can read in your browser, and a few other files that you need for this assignment. This assignment is due Monday, November 16.

Add a (non-mutating) method called rainDrops() to the Picture class. The full declaration of this method is

  public Picture rainDrops()
This method should draw non-overlapping, randomly located circles with random diameters between 5 and 20 pixels. The circles should be drawn on the Graphics object that comes from the Picture object that the method was called on (see page 217 of the textbook). This method should keep drawing non overlapping circles until either the circles cover 60% of the area of the picture or a maximum of 3,000 circles are drawn.

To help you get a sense of what this method should do, in the hw8.zip zip file there is a program called raindrops.jar that draws non overlapping circles in a window until the circles cover 60% of the window or until there are 3,000 circles in the window. You should be able to run this program by just double clicking on it. If that does not work, you can run the program using the following command line.

      C:\java -jar raindrops.jar
If you run the program this way, it will also show you some interesting information in the console window. When you run this program, be sure to click on the "re draw" button and also try resizing the window, in particular, maximize the window to cover your whole monitor. (Your method does not need to implement a "re draw" button and your method does not have to deal with the Picture object being resized.)

Here is some information about writing this method.

Notice that a randomly chosen circle has three (random) parameters, two location coordinates (for the center) and a diameter. (Make sure that each of your non overlapping circles has its center within the borders of the picture.) Your method will need to keep track of the non-overlapping circles that it has generated and drawn on the picture. It needs this information so that it can compare a new (random) circle to the previously drawn ones and make sure that the new circle does not overlap with any of the previous circles. So for each non overlapping circle that has been drawn, your method must remember its two center coordinates and its radius. Store this information in three arrays called circlesX[], circlesY[], and circlesR[]. These arrays should have size 3,000, since there can possibly be that many circles in a picture.

When your method generates a new random circle, it must test that circle to see if it overlaps with any of the circles already drawn on the picture and stored in the three arrays. So you need to figure out how to test if two circles do not overlap. If the new circle does overlap with a previous circle, you must discard the new circle's information and try again at generating a new random circle. If the new random circle does not overlap with any previous circle, then draw the new circle, store its information in the three arrays, increment a counter that is keeping track of how many circles you have drawn (and how many circles are stored in the arrays) and then compute the area of the new circle and add its area to a variable that is keeping track of the total area of all the drawn circles. Stop when the area of the drawn circles is 60% of the area of the picture (or when there are 3,000 circles in the picture).

You draw a circle using the drawOval() method from the Graphics class (see page 215 of the textbook). Notice that the drawOval() method does not use the center and radius of a circle as its parameters (that would be too easy). Once you have found a random center and a random radius that do not overlap with any previously drawn circle, you need to draw the circle using appropriate drawOval() parameters, so you need to translate the circle's center coordinates and radius value into drawOval's x1, y1, w, and h parameters.

Write a program called RainDrops.java that tests your rainDrops() method. Your test program's main() method should create a blank Picture object that is 500 by 500 pixels in size. Then it should send the raindrops() message to the picture object and save the result as the file testresult2.jpg. Then have your test program should open the picture file C:\cs123\mediasources\pictures\butterfly1.jpg, send the raindrops() message to the picture object, and save the result as the file testresult2.jpg.

Turn in a zip file called CS123Hw8Surname.zip containing your Picture.java file, your RainDrops.java file, a file called rainDrops.java that contains just your rainDrops() method, and your two resulting image files testresult1.jpg and testresult2.jpg. This assignment is due Monday, November 16.


Return to the main homework page.
Return to the CS 123 home page.


compliments and criticisms