We have provided most of the code for implementing the persian rug algorithm. In fact, we have implemented everything except the specifics for a particular rug: 1) the number and choice of colors to be used; 2) the border color; and 3) the function newColor that computes a new color from the four corner colors. The program uses an interface to supply these specifics:
To create a RugDemo, you need only interact with the PersianRugMaker class. The RugDemo class could simply provide the usual main method as follows:package persianRug; import java.awt.*; public interface RugSpecs { int colorListLength(); int getBorderColor(); Color getColor(int i); int newColor(int a, int b, int c, int d); }
Here 8 is the value of n that determines the size of the rug (in this case, 257 x 257 pixels). MyRugSpecs is a class you write to implement the RugSpecs interface.public class RugDemo { static public void main(String arg[]) { RugSpecs myrug = new MyRugSpecs(); PersianRugMaker prm = new PersianRugMaker(8, myrug); prm.makeRug(); } }
MyRugSpecs.java:
The complicated entries are getColor and newColor. getBorderColor is a choice of border color from the color list you will supply with getColor. colorListLength() should simply return the total number of colors you are using. getColor(i) should return the color associated with number i. For example, suppose you decide to use 5 colors: blue, green, orange, red, and yellow. You could implement getColor so thatimport java.awt.*: public class MyRugSpecs implements RugSpecs { public int colorListLength() { ... } public int getBorderColor() { ... } Color getColor(int i) { ... } int newColor(int a, int b, int c, int d) { ... } }
Finally, to implement newColor, compute an integer based on the four integers passed in, all representing colors from the color list. The value you return will be used (mod the length of the color list) to determine the new color. For example, suppose you simply implement newColor as
Then, if the four corners of a square are 0 (blue), 4 (yellow), 3 (red) and 1 (green), the new color will be 0 + 4 + 3 + 1 = 8 mod 5 = 3 (red).int newColor(int a, int b, int c, int d) { return a+b+c+d; }
Symmetry. The best persian rugs are those that exhibit the most symmetrical patterns. The nature of the pattern produced by a particular implementation of RugSpecs is controlled entirely by the newColor function.
To see an example, go to this site.