/** * File: MyYarnPattern.java * ------------------------ * This program creates a pattern that simulates the process of * winding pieces of color yarn around an array of pegs evenly * spaced along the edges of a board. The width of the board and * the number of peg across are defined as constants. * The dimensions of the board is 10:7. */ import acm.graphics.*; import acm.program.*; import java.awt.*; import acm.util.*; public class MyYarnPattern extends GraphicsProgram { public void run() { /* the upper-left corner of the board */ int xStart = (getWidth() - WIDTH) / 2; int yStart = (getHeight() - HEIGHT) / 2; /* draw the board */ GRect board = new GRect(xStart, yStart, WIDTH, HEIGHT); add(board); GPoint[] pegs = new GPoint[N_PEGS]; /* array of peg positions */ initPegArray(xStart, yStart, pegs); int thisPeg = 0; /* first peg */ int nextPeg = -1; /* beginning */ while ((thisPeg != 0) || (nextPeg == -1)) { /* repeat until back to peg 0 */ nextPeg = (thisPeg + DELTA) % N_PEGS; /* advance DELTA pegs */ GPoint p0 = pegs[thisPeg]; GPoint p1 = pegs[nextPeg]; /* draw a line between thisPeg and nextPeg with a random color */ GLine line = new GLine(p0.getX(), p0.getY(), p1.getX(), p1.getY()); line.setColor(rgen.nextColor()); add(line); thisPeg = nextPeg; /* move to the next peg */ pause(PAUSE_TIME); } } /** * Initializes the array pegs along the edges of the board. * @param x The x-coordinate of the upper-left corner of the board * @param y The y-coordinate of the upper-left corner of the board * @param pegs The array of peg positions */ private void initPegArray(int x, int y, GPoint[] pegs) { int pegIndex = 0; /* first peg */ /* initialize the pegs along the top edge */ for (int i = 0; i < N_ACROSS; i++) { pegs[pegIndex++] = new GPoint(i * PEG_SEP + x, y); } /* initialize the pegs along the right edge */ for (int i = 0; i < N_DOWN; i++) { pegs[pegIndex++] = new GPoint(N_ACROSS * PEG_SEP + x, i * PEG_SEP + y); } /* initialize the pegs along the bottom edge */ for (int i = N_ACROSS; i > 0; i--) { pegs[pegIndex++] = new GPoint(i * PEG_SEP + x, N_DOWN * PEG_SEP + y); } /* initialize the pegs along the left edge */ for (int i = N_DOWN; i > 0; i--) { pegs[pegIndex++] = new GPoint(x, i * PEG_SEP + y); } } /* Private constants */ private static final int DELTA = 67; /* number of pegs to advance */ /* a prime number */ private static final int WIDTH = 500; /* board width */ /* less than the window width */ private static final int N_ACROSS = 50; /* pegs across (minus a corner) */ /* a divisor of WIDTH */ private static final int PAUSE_TIME = 200; /* derived constants */ private static final int PEG_SEP = WIDTH / N_ACROSS; /* pixel separating pegs */ private static final int N_DOWN = (N_ACROSS * 7) / 10; /* pegs down (minus a corner) */ private static final int HEIGHT = N_DOWN * PEG_SEP; /* board height */ private final int N_PEGS = 2 * N_ACROSS + 2 * N_DOWN; /* total number of pegs */ /* private instance variables */ RandomGenerator rgen = RandomGenerator.getInstance(); }