Assignment #6, CS 49J, Section 1

due October 25, 2007
70 points plus 10 points extra credit

 

In this assignment you are to create and display tartans by defining Tartan, TartanComponent, and Sett classes as specified below.

A tartan is specified by a 2-dimensional array of rectangles, indexed by a 1-dimensional structure called a sett. Each entry of a sett specifies a width and a color. Each rectangle in a tartan takes its height from the width of the sett entry corresponding to its row index, and its width from the width of the sett entry corresponding to its column index. It takes its color by combining the colors corresponding to its row and column indices in a manner to be described below.

Your Sett class is to have a constructor that takes an array of Colors, and an array of ints representing the widths. The class is to have a setLocation method that, like the identically-named method for the Rectangle class, specifies the position where the upper-left hand corner is to appear when an object of the class is drawn. The initial position of a Sett object is to be (0,0).

The Sett class is to have a draw method that takes a Graphics2D argument. If the sett has size n, what should be drawn is the array of rectangles corresponding to rows and columns 0, 1, ..., n-2, n-1, n-2, ..., 1, 0 of the sett. This will allow larger portions of the tartan to be drawn by juxtaposing drawings of this sort, and will correctly implement the symmetry with which most tartans are woven.

The color of each rectangle in this array is to be obtained (unless you do the extra credit portion of this assignment) by averaging the colors provided by the row and column. The average of two colors may be created by averaging separately their R, G, and B components, and creating a new color with these averaged components. The Color class provides sufficient accessors and constructors to do this. Your Sett class will almost certainly also need public accessor methods that the Tartan class can use. As always, you may define any private methods and instance fields that are appropriate for the class.

The Tartan class is to have a constructor that takes two integers representing the upper left-hand corner of its position, two integers representing the width and height of the frame area on which the tartan is to be drawn, and a Sett parameter that represents the sett underlying the tartan. The only other method that you need to provide is a draw method that takes a Graphics2D argument. This method is to repetitively draw copies of the underlying sett in both the horizontal and vertical directions, so that no part of the drawing appears in the uppermost, lowest, leftmost, or rightmost 10% of the frame, and so that the maximum number of repetitions is drawn subject to these constraints.

Finally, your TartanComponent class is to extend JComponent, have a constructor that takes a Tartan as argument, and define a paintComponent method that takes a Graphics argument, and draws the corresponding tartan onto a Graphics2D object to which the argument has been cast.

If your Tartan.draw method doesn't compile, crashes your program, or draws something less elaborate than its sett, then you can simply have it invoke the draw method for its Sett component. There will be a 15-point deduction for this, so if your Tartan.draw method can display anything more than Sett.draw, you're probably better off not using this approach.

For this assignment, you are to turn in hard and soft copies of your class definitions. There is no output that you need to turn in. Please make sure that you are familiar with the instructions in the handout on assignments for submitting soft copies of an assignment with multiple files.

EXTRA CREDIT (10 points)
The averaging of colors that is suggested above is of course not what real weavers do, so the texture that you will get on the screen is not what you would see in a real textile. For extra credit, you may simulate the weaving strategy whereby each thread in a row repeatedly goes above two threads that it crosses, then below, then above, then below, etc. At each new row, the pattern is translated by one column. So a 6x6 rectangle with colors red (R) and blue (B) might correspond to an array of pixels looking like:
           R R B B R R
           B R R B B R
           B B R R B B
           R B B R R B
           R R B B R R
           B R R B B R
Here I am not concerned with which color the pattern starts with, or whether you advance it left or right as you move downward. You may if you want assume that every width in a sett is even.