Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prove;
- import java.awt.Font;
- import edu.princeton.cs.introcs.In;
- import edu.princeton.cs.introcs.StdDraw;
- public class ArrayVisualizer {
- /****************************************************************************
- * Compilation: javac PercolationVisualizer.java
- * Execution: java PercolationVisualizer input.txt
- * Dependencies: Percolation.java StdDraw.java In.java
- *
- * This program takes the name of a file as a command-line argument.
- * From that file, it repeatedly inserts the input, then pops everything out.
- * Cells that are in between the size and the capacity of the array are
- * marked with a full black square.
- * @throws Exception
- *
- ****************************************************************************/
- // draw N-by-N percolation system
- public static void draw(DynamicArray<String> vector) throws Exception {
- int N = 20;
- StdDraw.clear();
- StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
- StdDraw.setXscale(0, N);
- StdDraw.setYscale(0, N);
- for (Integer i=0; i < N; ++i) {
- if (i >= vector.capacity()) break;
- if (i < vector.size() ) {
- StdDraw.square(i, N/2, 0.5);
- StdDraw.text(i, N/2, vector.get(i)); // Content
- }
- else if (i < vector.capacity()) {
- StdDraw.setPenColor(StdDraw.BLACK);
- StdDraw.filledSquare(i, N/2, 0.5);
- }
- StdDraw.text(i, N/2 - 1, i.toString()); // Label
- }
- }
- public static void main(String[] args) throws Exception {
- In in = new In(args[0]); // input file
- // repeatedly read in sites to open and draw resulting system
- DynamicArray<String> v = new DynamicArray<String>();
- draw(v);
- while (!in.isEmpty()) {
- StdDraw.show(0); // turn on animation mode
- String str = in.readString();
- v.add(str);
- draw(v);
- StdDraw.show(1000); // pause for 100 miliseconds
- }
- while (v.size() > 0) {
- StdDraw.show(0); // turn on animation mode
- v.pop();
- draw(v);
- StdDraw.show(1000); // pause for 100 miliseconds
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement