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).
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.
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
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.
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
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
method before you draw any shape.
Math.random()to randomize the size or location of the shapes.
Here is a quick example from my program, using only circles:
How to hand in the assignment