Advertisement
Guest User

ArrayVisualizer.java

a guest
Aug 24th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package prove;
  2. import java.awt.Font;
  3.  
  4. import edu.princeton.cs.introcs.In;
  5. import edu.princeton.cs.introcs.StdDraw;
  6.  
  7. public class ArrayVisualizer {
  8.     /****************************************************************************
  9.      *  Compilation:  javac PercolationVisualizer.java
  10.      *  Execution:    java PercolationVisualizer input.txt
  11.      *  Dependencies: Percolation.java StdDraw.java In.java
  12.      *
  13.      *  This program takes the name of a file as a command-line argument.
  14.      *  From that file, it repeatedly inserts the input, then pops everything out.
  15.      *  Cells that are in between the size and the capacity of the array are
  16.      *  marked with a full black square.
  17.      * @throws Exception
  18.      *
  19.      ****************************************************************************/
  20.  
  21.  
  22.     // draw N-by-N percolation system
  23.     public static void draw(DynamicArray<String> vector) throws Exception {
  24.         int N = 20;
  25.         StdDraw.clear();
  26.         StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
  27.  
  28.         StdDraw.setXscale(0, N);
  29.         StdDraw.setYscale(0, N);
  30.  
  31.  
  32.         for (Integer i=0; i < N; ++i) {
  33.             if (i >= vector.capacity()) break;
  34.             if (i < vector.size() ) {
  35.                 StdDraw.square(i, N/2, 0.5);
  36.                 StdDraw.text(i, N/2, vector.get(i)); // Content
  37.             }
  38.             else if (i < vector.capacity()) {
  39.                 StdDraw.setPenColor(StdDraw.BLACK);
  40.                 StdDraw.filledSquare(i, N/2, 0.5);
  41.             }
  42.             StdDraw.text(i, N/2 - 1, i.toString()); // Label
  43.         }
  44.  
  45.     }
  46.  
  47.     public static void main(String[] args) throws Exception {
  48.         In in = new In(args[0]);      // input file
  49.  
  50.         // repeatedly read in sites to open and draw resulting system
  51.         DynamicArray<String> v = new DynamicArray<String>();
  52.         draw(v);
  53.         while (!in.isEmpty()) {
  54.             StdDraw.show(0);          // turn on animation mode
  55.             String str = in.readString();
  56.             v.add(str);
  57.             draw(v);
  58.             StdDraw.show(1000);        // pause for 100 miliseconds
  59.         }
  60.  
  61.         while (v.size() > 0) {
  62.             StdDraw.show(0);          // turn on animation mode
  63.             v.pop();
  64.             draw(v);
  65.             StdDraw.show(1000);        // pause for 100 miliseconds
  66.         }
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement