001package algs15.perc; 002import stdlib.*; 003 004/* ************************************************************************** 005 * Compilation: javac InteractivePercolationVisualizer.java 006 * Execution: java InteractivePercolationVisualizer N 007 * Dependencies: PercolationVisualizer.java Percolation.java StdDraw.java StdOut.java 008 * 009 * This program takes the grid size N as a command-line argument. 010 * Then, the user repeatedly clicks sites to open with the mouse. 011 * After each site is opened, it draws full sites in light blue, 012 * open sites (that aren't full) in white, and blocked sites in black. 013 * 014 ****************************************************************************/ 015public class InteractivePercolationVisualizer { 016 private static final int delay = 1; 017 018 public static void main(String[] args) { 019 args = new String[] { "4" }; 020 021 // N-by-N percolation system (read from command-line, default = 10) 022 int N = 10; 023 if (args.length == 1) N = Integer.parseInt(args[0]); 024 025 // turn on animation mode 026 StdDraw.show(0); 027 028 // repeatedly open site specified my mouse click and draw resulting system 029 StdOut.println(N); 030 031 Percolation perc = new Percolation(N); 032 PercolationVisualizer.draw(perc, N); 033 StdDraw.show(delay); 034 while (true) { 035 036 // detected mouse click 037 if (StdDraw.mousePressed()) { 038 039 // screen coordinates 040 double x = StdDraw.mouseX(); 041 double y = StdDraw.mouseY(); 042 043 // convert to row i, column j 044 int i = (int) (N - Math.floor(y) - 1); 045 int j = (int) (Math.floor(x)); 046 047 // open site (i, j) provided it's in bounds 048 if (i >= 0 && i < N && j >= 0 && j < N) { 049 if (!perc.isOpen(i, j)) { 050 StdOut.println(i + " " + j); 051 } 052 perc.open(i, j); 053 } 054 055 // draw N-by-N percolation system 056 PercolationVisualizer.draw(perc, N); 057 } 058 StdDraw.show(delay); 059 } 060 } 061}