|
| 1 | +/****************************************************************************** |
| 2 | + * Compilation: javac PercolationVisualizer.java |
| 3 | + * Execution: java PercolationVisualizer input.txt |
| 4 | + * Dependencies: Percolation.java |
| 5 | + * |
| 6 | + * This program takes the name of a file as a command-line argument. |
| 7 | + * From that file, it |
| 8 | + * |
| 9 | + * - Reads the grid size n of the percolation system. |
| 10 | + * - Creates an n-by-n grid of sites (intially all blocked) |
| 11 | + * - Reads in a sequence of sites (row i, column j) to open. |
| 12 | + * |
| 13 | + * After each site is opened, it draws full sites in light blue, |
| 14 | + * open sites (that aren't full) in white, and blocked sites in black, |
| 15 | + * with with site (1, 1) in the upper left-hand corner. |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +import java.awt.Font; |
| 20 | + |
| 21 | +import edu.princeton.cs.algs4.In; |
| 22 | +import edu.princeton.cs.algs4.StdDraw; |
| 23 | + |
| 24 | +public class PercolationVisualizer { |
| 25 | + |
| 26 | + // delay in miliseconds (controls animation speed) |
| 27 | + private static final int DELAY = 100; |
| 28 | + |
| 29 | + // draw n-by-n percolation system |
| 30 | + public static void draw(Percolation perc, int n) { |
| 31 | + StdDraw.clear(); |
| 32 | + StdDraw.setPenColor(StdDraw.BLACK); |
| 33 | + StdDraw.setXscale(-0.05*n, 1.05*n); |
| 34 | + StdDraw.setYscale(-0.05*n, 1.05*n); // leave a border to write text |
| 35 | + StdDraw.filledSquare(n/2.0, n/2.0, n/2.0); |
| 36 | + |
| 37 | + // draw n-by-n grid |
| 38 | + int opened = 0; |
| 39 | + for (int row = 1; row <= n; row++) { |
| 40 | + for (int col = 1; col <= n; col++) { |
| 41 | + if (perc.isFull(row, col)) { |
| 42 | + StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); |
| 43 | + opened++; |
| 44 | + } |
| 45 | + else if (perc.isOpen(row, col)) { |
| 46 | + StdDraw.setPenColor(StdDraw.WHITE); |
| 47 | + opened++; |
| 48 | + } |
| 49 | + else |
| 50 | + StdDraw.setPenColor(StdDraw.BLACK); |
| 51 | + StdDraw.filledSquare(col - 0.5, n - row + 0.5, 0.45); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // write status text |
| 56 | + StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12)); |
| 57 | + StdDraw.setPenColor(StdDraw.BLACK); |
| 58 | + StdDraw.text(0.25*n, -0.025*n, opened + " open sites"); |
| 59 | + if (perc.percolates()) StdDraw.text(0.75*n, -0.025*n, "percolates"); |
| 60 | + else StdDraw.text(0.75*n, -0.025*n, "does not percolate"); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) { |
| 65 | + In in = new In(args[0]); // input file |
| 66 | + int n = in.readInt(); // n-by-n percolation system |
| 67 | + |
| 68 | + // turn on animation mode |
| 69 | + StdDraw.enableDoubleBuffering(); |
| 70 | + |
| 71 | + // repeatedly read in sites to open and draw resulting system |
| 72 | + Percolation perc = new Percolation(n); |
| 73 | + draw(perc, n); |
| 74 | + StdDraw.show(); |
| 75 | + StdDraw.pause(DELAY); |
| 76 | + while (!in.isEmpty()) { |
| 77 | + int i = in.readInt(); |
| 78 | + int j = in.readInt(); |
| 79 | + perc.open(i, j); |
| 80 | + draw(perc, n); |
| 81 | + StdDraw.show(); |
| 82 | + StdDraw.pause(DELAY); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments