RandomText.java


Below is the syntax highlighted version of RandomText.java from §1.5 Input and Output.


/******************************************************************************
 *  Compilation:  javac RandomText.java
 *  Execution:    java RandomText text n
 *  Dependencies: StdDraw.java
 *
 *  Plots the specified text n times at random locations, in random colors.
 *
 *  % java RandomText Hello 10
 *
 *  % java RandomText World 15
 *
 *  % java RandomText Java 20
 *
 *
 ******************************************************************************/

import java.awt.Color;
import java.awt.Font;

public class RandomText {

    public static void main(String[] args) {
        String text = args[0];
        int n = Integer.parseInt(args[1]);
        StdDraw.clear(Color.BLACK);
        Font font = new Font("Arial", Font.BOLD, 60);
        StdDraw.setFont(font);

        for (int i = 0; i < n; i++) {
            StdDraw.setPenColor(Color.getHSBColor((float) Math.random(), 1.0f, 1.0f));
            double x = Math.random();
            double y = Math.random();
            StdDraw.text(x, y, text);
        }
    }

}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:14:17 EDT 2022.