Advertisement
Guest User

InteractiveArrayVisualizer.java

a guest
Aug 24th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. /****************************************************************************
  2.  *  Dependencies: DynamicArray<String>.java StdDraw.java StdOut.java
  3.  *
  4.  *  This program takes the maximum array capacity N as a command-line argument.
  5.  *  Then, the user repeatedly clicks cells in the array to fill with "X".
  6.  *  Cells that are in between the size and the capacity of the array are
  7.  *  marked with a full black square.
  8.  *
  9.  ****************************************************************************/
  10. package prove;
  11.  
  12. import edu.princeton.cs.introcs.StdDraw;
  13. import edu.princeton.cs.introcs.StdOut;
  14.  
  15. public class InteractiveArrayVisualizer {
  16.  
  17.     public static void main(String[] args) throws Exception {
  18.         // Array of maximum capacity N (wouldn't fit the line otherwise...)
  19.         int N = 20;          
  20.         if (args.length == 1) N = Integer.parseInt(args[0]);
  21.  
  22.         // repeatedly open site specified my mouse click and draw resulting system
  23.         StdOut.println(N);
  24.  
  25.         StdDraw.show(0);
  26.         DynamicArray<String> v = new DynamicArray<String>();
  27.         v.add("X");
  28.         ArrayVisualizer.draw(v);
  29.         StdDraw.show(0);
  30.  
  31.         while (true) {
  32.  
  33.             // detected mouse click
  34.             if (StdDraw.mousePressed()) {
  35.  
  36.                 // screen x coordinates (don't care about y)
  37.                 double x = StdDraw.mouseX();
  38.  
  39.                 // convert to position i
  40.                 int i = (int) (Math.floor(x+0.5) );
  41.  
  42.                 // open site i provided it's in bounds
  43.                 if (i >= 0 && i <= N ) {
  44.                     StdOut.println(i);
  45.                     if (i < v.size() - 1) {
  46.                         v.set(i, "X");
  47.                     }
  48.                     else if (i == v.size() - 1) {
  49.                         v.pop();
  50.                     }
  51.                     else  {
  52.                         v.add("X"); // Random value
  53.                     }
  54.                     StdOut.println("Size: " + v.size());
  55.                     StdOut.println("Capacity: " + v.capacity());
  56.                 }
  57.  
  58.                 // draw the array
  59.                 StdDraw.show(0);
  60.                 ArrayVisualizer.draw(v);
  61.             }
  62.             StdDraw.show(100);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement