Course navigation & on this page
InteractivePercolationVisualizer
Original InteractivePercolationVisualizer implementation from the supplied Java collection.
Overview
*************************************************************************** Compilation: javac InteractivePercolationVisualizer.java Execution: java InteractivePercolationVisualizer n Dependencies: PercolationVisualizer.java Percolation.java StdDraw.java StdOut.java This program takes the grid size n as a command-line argument. Then, the user repeatedly clicks sites to open with the mouse. After each site is opened, it draws full sites in light blue, open sites (that aren't full) in white, and blocked sites in black. ****************************************************************************
This reference entry presents original author documentation and source. It is supplementary material, not an additional numbered lecture.
Original course code
Source: code/InteractivePercolationVisualizer.java. Original logic, comments, variable names and attribution are retained.
Download InteractivePercolationVisualizer.java/****************************************************************************** * Compilation: javac InteractivePercolationVisualizer.java * Execution: java InteractivePercolationVisualizer n * Dependencies: PercolationVisualizer.java Percolation.java * StdDraw.java StdOut.java * * This program takes the grid size n as a command-line argument. * Then, the user repeatedly clicks sites to open with the mouse. * After each site is opened, it draws full sites in light blue, * open sites (that aren't full) in white, and blocked sites in black. * ******************************************************************************/ import edu.princeton.cs.algs4.StdDraw;import edu.princeton.cs.algs4.StdOut; public class InteractivePercolationVisualizer { public static void main(String[] args) { // n-by-n percolation system (read from command-line, default = 10) int n = 10; if (args.length == 1) n = Integer.parseInt(args[0]); // repeatedly open site specified my mouse click and draw resulting system StdOut.println(n); StdDraw.enableDoubleBuffering(); Percolation perc = new Percolation(n); PercolationVisualizer.draw(perc, n); StdDraw.show(); while (true) { // detected mouse click if (StdDraw.mousePressed()) { // screen coordinates double x = StdDraw.mouseX(); double y = StdDraw.mouseY(); // convert to row i, column j int i = (int) (n - Math.floor(y)); int j = (int) (1 + Math.floor(x)); // open site (i, j) provided it's in bounds if (i >= 1 && i <= n && j >= 1 && j <= n) { if (!perc.isOpen(i, j)) { StdOut.println(i + " " + j); } perc.open(i, j); } // draw n-by-n percolation system PercolationVisualizer.draw(perc, n); StdDraw.show(); } StdDraw.pause(20); } }}