This assignment will provide you with practice on using objects, Java graphics, using other people's code, and writing methods with parameters and returns.
For this assignment, you will need the class Viewer, provided on the course website. This class includes several methods that you will need to use for your assignment, but YOU DO NOT NEED TO UNDERSTAND HOW THEY WORK. The code for these methods involve several things you have not learned yet. Feel free to look at them and try to understand them, but it is not necessary to understand their code to do this assignment.
You will use a Viewer object to display the results of drawing some shapes. Construct a Viewer object by passing two ints as arguments, the width and height (number of pixels) in the display. For instance, Viewer view = new Viewer(300,400);
For each Shape object (of type Rectangle, Line2D.Double, or Ellipse2D.Double) that you would like to display, you will need to add it to the Viewer object to let the Viewer object know that it should display the shape. For instance, if you would like to display a Rectangle object r with Viewer v, you can call v.addShape(r);. There are two options with the addShape method: color and fill. You can change the color of the shape you add (default black) by specifying a color from the Color class: for example, v.addShape(r, Color.pink);. Finally, you can specify that the shape be completely filled with color (rather than a thin border of color around white space) by calling v.addFilledShape(r); or v.addFilledShape(r, Color.magenta);.
Once you have finished adding all the shapes you would like to display, call the view() method of the Viewer object to make the results display on the screen. The main() method in the Viewer class gives a simple example of how to use a Viewer object.
Part of the purpose of this assignment is to get you used to the idea of using somewhat unfamiliar object types. We are asking you to use Shape objects, which you have seen only very briefly. Remember, though, that there are online references available for helping you find all the information you need to use an unfamiliar class.
Here are the javadocs (short for Java documentation) for the classes you will use in this assignment:
Create a Java file called Artist.java. This file should contain the following static methods.
/** Adds N rectangles to Viewer v, all with the same width and height as Rectangle r. The first rectangle starts at the same position as r. Each one after that is dx spots to the right, and dy spots below the previous one. */ public static void addNRectanglesInARow(Viewer v, Rectangle r, int N, int dx, int dy); /** Returns a new ellipse that is the "mirror image" of the original ellipse, if there were a mirror positioned on the row of a viewer object given by mirrorRow. The returned ellipse should have the same shape as the original ellpise (since all elipses are axis-parallel), but the bottom of the original ellipse should be as far from the mirrorRow as the top of the returned ellipse, and the bottom of the original ellipse should be on the opposite side of the mirrorRow from the top of the returned ellipse. */ public static Ellipse2D.Double mirrorEllipseAcrossHorizontalAxis(Ellipse2D.Double original, int mirrorRow); /** Lengthens a line by a factor of lengthMultiplier. The start point (x1,y1) of the line should remain the same, but but the endpoint (x2,y2) should change so that the length of the new line is lengthMultiplier * (length of original line). */ public static void lengthen(Line2D.Double l, double lengthMultiplier); /** Returns a new line whose first endpoint and length are the same as the original, but whose second endpoint is rotated by an angle (in radians). The math for this is slightly tricky, so let me give you some helpful hints: 1) calculate the angle of the original line. Math.acos gives the arc cosine function, with a returned angle in the range of 0.0 to Math.PI. Set the angle of the line to Math.acos((x2-x1) / (length of line)). Then, if y2 is less than y1, update the angle to [2*Math.PI - (the angle from Math.acos)]. 2) calculate the angle for the new line. Just add the angle of the original line and the angle parameter. 3) calculate the new coordinates for x2 and y2. the new x2 equals [x1 + Math.cos(new angle) * (length of line)]. the new y2 equals [y1 + Math.sin(new angle) * (length of line)]. 4) Create a new line with x1, y1 same as in the original, and x2, y2 as calculated above. */ public static Line2D.Double rotateLine(Line2D.Double original, double angle);
You should include a main method that draws a pretty picture (how pretty is entirely up to you) using your methods and the Viewer class. Your drawing should obey the following constraints:
You're welcome to write additional static methods if that helps you organize your code.
In a file called "answers.txt", write your answers to the following questions regarding this assignment. Write at most 3 sentences for each question.
1. What happens if you construct a Rectangle r with width 10, height 10, x position 10, and y position 10, add it to a Viewer object, call the view() method, and then change the x position of the rectangle to 40? Will the Viewer object draw the rectangle once or twice, will it draw it at x position 10 or 40 or both/neither, and why?
2. What happens if you create a Line2D.Double, add it to a Viewer object, then lengthen it using your lengthen method by a factor of 0.5 (effectively shrinking it in half), and add it to the Viewer again with a different color? Does the line appear twice or once? Why?
3. If two shapes overlap in position, what does the Viewer object draw for the overlapping positions?
Email your Artist.java file and your answers.txt file to your TA when finished. You do not need to send the TA the Viewer class.