COMP 110 Spring 2008
Lab 6
25 points
Assigned: Friday, March 28
Due: Friday, April 4 by 3:00pm
Description
You are given some artistic freedom in lab 6. In this lab you will be creating a graphics applet to draw a picture that draws the same shape multiple times. While completing lab 6 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 (or two) as a method.
-
Choose your shape by looking at the
java Graphics Class You should choose between fill or draw and polygons, ovals, or rectangles. You may choose more than one shape to draw.
-
The first two arguments of any shape in the graphics class are the x and y location. You need to change the location for each shape that is displayed. A good way to do this is to look at your java Math Class and design a mathematical function. Here is code for how to have your shapes drawn in a circle with radius 20:
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(canvas, x, y, 10);
}
Notice that your shape is being drawn many times within this code. This code could go in your paint method.
-
Your shape will randomly change color between at least 5 colors (think switch statements). Go to the java Color Class to choose your colors.
-
A random number generator is provided for you in the java Math Class. Take a look at the random method. The method returns a double between 0.0 and 1.0. How do you get random to return 5 (or however many colors you have) random integers? What is the range if you multiple the random number by 5? How would you turn your random number into an int?
More Info
-
I have given you example code in Lab6.java. Modify this code by changing it and adding your own methods.
-
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. 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 objects.
- Loops are your friend. You could try using a double for loop (like the one on the midterm).
- Have fun with this assignment and be creative
Here is an example image from my program:
Grading
- 16 points 4 points for each description bullet
- 5 points Program design (breaking the assignment into appropriate methods)
- 4 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_lab6.jar, where yourlastname is your last name.
The .jar file should include Lab6.java.
Your java files should have the appropriate header.
Follow these instructions to create yourlastname_lab6.jar.
-
Attach yourlastname_lab6.jar to an email with
subject COMP110 Lab 6 yourlastname to tpeck@cs.unc.edu by 3:00 Friday April 4.
-
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.