# COMP 110 Spring 2011

## Lab 7

### 25 points

Assigned: Friday, March 25
Due: Friday, April 1 by 1:00pm

Files

Description

You are given some artistic freedom in this lab. You will be creating a graphics applet to draw a picture that draws the same shapes multiple times. While completing this lab you should notice the top-down design of the problem. The big problem -- draw me a picture. You will need to break down the problem into subtasks (your methods) based on the guidelines I provide. You should think of each bullet as a method (or several methods).

1. Choose shapes by looking at the Java Graphics class. There are several methods in the Graphics class. Some of them start with the word draw (these draw an outline of a shape), and others start with the word fill (these draw a filled version of a shape). You may choose between several options for shapes, including but not limited to: rectangles, ovals, lines, and polygons (note that polygons may require some knowledge of arrays, which we have not yet covered, but feel free to explore them if you'd like).

Define your own static methods to draw your shapes. I have given you an example method in FunPatternPanel.java, called `drawCircle`. Follow that example when writing your own methods. Define one method for each different shape you will draw. You should choose at least one shape to draw in your program (in addition to the circle that I have already given you). You may also draw text in addition to your shapes.

2. The first two arguments for most shape drawing methods in the Graphics class are the x and y location. Note that the origin (0, 0) of the drawing area is at the upper left corner. You need to change the location for each shape that is displayed. A good way to do this is to look at the Java Math class and design a mathematical function. Here is example code for how to have your shapes drawn in a circle with radius 20 around the point (0, 0):

``````
for (int i = 0; i < 360; i += 10)
{
int x = (int) (Math.sin(Math.toRadians(i)) * 20);
int y = (int) (Math.cos(Math.toRadians(i)) * 20);
drawCircle(g, x, y, 10);
}
``````

Note that your shape is being drawn many times in this code. Your code that draws your shapes in a pattern (such as the example code above) should go inside of the method `drawPattern` in FunPatternPanel.java. Notice that the `drawPattern` method is being called from inside the `paintComponent` method. Try copying the example code into your `drawPattern` method to see what happens. Remember that you have to run the file Lab7.java, because that is the class that has a `main` method in it.

3. Before you draw each shape, you should change your drawing color to a random color (select from at least 5 colors) -- think about `switch` statements. Go to the Java Color class to choose your colors. If you'd like, you can create custom colors by using the Color class' constructors.

A random number generator is provided for you in the Java Math class. I have created a static method called `setRandomColor` for you, but it is incomplete. It calls the `Math.random` method to get a random double value >= 0.0 and < 1.0 and puts that value into the variable `rnd`. How can you get a random integer between 1 and 5 (or however many colors you choose) from the value in `rnd`?

You must complete the `setRandomColor` method so that it sets a random color when it is called. You must also call the `setRandomColor` in your `drawPattern` method before you draw any shape.

More information

• You will not have to modify Lab7.java. All of your code should go in FunPatternPanel.java.
• Look through FunPatternPanel.java to find comments that start with ::: -- these tell you about things you should change (or sometimes, can if you want to but don't have to).
• All of your methods will be static. To call a static method (including the Math class methods) always use the class name as the object (e.g., `Math.abs(-2)`).
• Remember to test each of your methods thoroughly before you begin the next part of the program. This is bottom-up testing.
• Extra randomness can be added to your image by using `Math.random()` to randomize the size or location of the shapes.
• Loops are your friend. You could try using nested loops to make some interesting patterns.
• Have fun with this assignment and be creative!
• Here is a quick example from my program, using only circles:

Grading

• 3 points Appropriate header, code commented, jar file runs
• 12 points 4 points for each description bullet
• 5 points Program design (breaking the assignment into appropriate methods)
• 5 points Having fun with the assignments and adding extra pieces. (More shapes, random sizes, etc)

How to hand in the assignment

• A file named yourlastname_lab7.jar, where yourlastname is your last name. The .jar file should include Lab7.java, FunPatternPanel.java, Lab7.class, and FunPatternPanel.class. Your java files should have the appropriate header. Follow these instructions to create yourlastname_lab7.jar.
• In your MANIFEST file, designate Lab7 as your Main-Class.
• Once your jar file is created, you should be able to double-click it to make sure it was created correctly.
• Submit via Blackboard yourlastname_lab7.jar by 1:00pm Friday April 1.
• You can also lose points for incorrectly handing in the assignment, not using appropriate white space, not using conventional Java names for your variables, and not commenting your code.