Input/Output: Graphics: Outline and filled shapes

Write a java program, MyRandArt_YI.java to create an abstract painting using the geometric functions in the StdDraw library and the random function to generate points, colors, and thickness. Note: make sure to restrict ranges for each feature.

Geometric Abstract Painting: Mary Heilmann

  • Write a java program, MyCubicArt_YI.java to create an abstract painting using the geometric functions in the StdDraw library in the link below.

Students’ work from previous years

How to create your own colors:

setPenColor(int red, int green, int blue)
Creates an opaque RGB color with the specified red, green, and blue values.

Parameters:
red – the amount of red (between 0 and 255)
green – the amount of green (between 0 and 255)
blue – the amount of blue (between 0 and 255)

  • Write a java program, MyRandCubicArt_YI.java to create an abstract painting using the geometric functions in the StdDraw library and the random function to generate points, colors, and thickness. Note: make sure to restrict ranges for each feature.

How to draw a polygon:

Write a short program with a polygon and play with it until you understand how it works.

[spoiler title=’APoly’]

/**
 * Playing with colors and shapes.
 *
 * @GE
 * @java 1.8.4 
 * 12/5/18
 */
public class APoly
{
   public static void main(String [] args)
   {
       // rescale the coordinate system
        StdDraw.setXscale(0, 2); 
        StdDraw.setYscale(0, 2);
        StdDraw.setPenColor(255,0,255);
        
       double y[] = {.5,.5,1.5,1.5}; // y coordinates of each vertex
       double x[] = {.5,1.5,1.5,.5}; // x coordinates of each vertex
       
       //StdDraw.setPenColor(255,0,255);
       StdDraw.filledPolygon(x,y);
       StdDraw.setPenColor(0,0,255);
       StdDraw.setPenRadius(0.008);
       StdDraw.polygon(x,y);
       
    }
}

[/spoiler]

How to change thickness:

setPenRadius(0.05)

Sets the pen size to the default size (0.002). The pen is circular, so that lines have rounded ends, and when you set the pen radius and draw a point, you get a circle of the specified radius.

Input/Output: Drawing Random Circles

Write a program Circles_YI.java that draws filled circles of random size at random positions in the unit square, producing images like those below. Your program should take four command-line arguments: the number of circles, the probability that each circle is black, the minimum radius, and the maximum radius.

random circles

Submit screenshots and submit them.

Input/Output: Lesson Questions

Classwork: PU Input and Output Standard classes

Book site
Screen Shot 2015-02-23 at 12.19.21 AM

Lesson’s pdf
Screen Shot 2015-11-22 at 4.48.06 PM

You can get the following files from edmodo.com. Add them to your workspace:
StdIn.java
StdOut.java
StdDraw.java
StdAudio.java

    1. What is an abstraction?
    2. What is an input/output abstraction?
    3. What is an abstraction describing something having no limit?
    4. What is an abstraction for an infinite output sequence?
    5. What are the components of the improved input-output abstraction?
    6. What is an abstraction for an infinite input sequence?

stdIn

           7. What is an abstraction for an infinite output sequence?

StdOut

            8. These are the same as System.out. Why not just use System.out?

            9. Interactive input
                   • Prompt user to type inputs on standard input stream.
                   • Mix input stream with output stream.

            10. Average
                   • Read a stream of numbers.
                   • Compute their average.

            11. How do I specify the end of the stream?

            12. What is the limit on the size of the input stream?

            13. Do I always have to type in my input data and print my output?

pipingIO

             14. What can you do if there’s no room for a huge file on my computer?

Formatted printing basics again. In its simplest form, printf() takes two arguments. The first argument, a string, contains a format that describes how the second argument is to be converted to a string for output.
printfexample
printftable

15. How many spaces are printed before the digit 3 in the following line: StdOut.printf(“%8.3f”, Math.PI);?

Input/Output: Plot Filter

Screen Shot 2015-02-23 at 12.19.21 AM

More on re-directing input/output

  1. Include this PlotFilter in your project
    /******************************************************************************
     *  Compilation:  javac PlotFilter.java
     *  Execution:    java PlotFilter < input.txt
     *  Dependencies: StdDraw.java StdIn.java
     *  
     *  % java PlotFilter < USA.txt
     *
     *  Datafiles:    http://introcs.cs.princeton.edu/java/15inout/USA.txt
     *
     ******************************************************************************/
    
    public class PlotFilter { 
    
        public static void main(String[] args) {
    
            // read in bounding box and rescale
            double x0 = StdIn.readDouble();
            double y0 = StdIn.readDouble();
            double x1 = StdIn.readDouble();
            double y1 = StdIn.readDouble();
            StdDraw.setXscale(x0, x1);
            StdDraw.setYscale(y0, y1);
    
            // for bigger points
            StdDraw.setPenRadius(0.005);
    
            // to speed up performance, defer displaying points
            StdDraw.enableDoubleBuffering();
    
            // plot points, one at a time
            while (!StdIn.isEmpty()) {
                double x = StdIn.readDouble();
                double y = StdIn.readDouble();
                StdDraw.point(x, y);
            }
    
            // display all of the points now
            StdDraw.show();
    
        }
    }
    
    
  2. Create a text file with the data from the link below. Right-click on the middle of the page and “save as” in your project folder.
    USA.txt

Assignments:
Standard Drawing
usacities

Use the java programs from the lesson, Triangle.java, PlotFilter.java, and FunctionGraph.java to work on the assignments posted on edmodo.com.

MyPlotFilter_YI.java
Use PlotFilter.java from Filtering data to a standard drawing to create your own drawing.

Read about Standard drawing and the Methods Summary

Input/Output: Graph Paper

Screen Shot 2015-02-23 at 12.19.21 AM

Classwork:
Write a program, MyGraphPaper_YI.java similar to CheckerBoard.java from below but to draw 2 different kinds of graph paper. Think of what graph paper you would use in a math class.

Study program CheckerBoard.java that takes a command-line argument N and plots an N-by-N checkerboard with red and black squares. It colors the lower left square red.

[spoiler title=’Checkerboard.java’]
Below is the syntax highlighted version of Checkerboard.java from §1.5 Input and Output.

/******************************************************************************
 *  Compilation:  javac Checkerboard.java 
 *  Execution:    java Checkerboard n
 *  Dependencies: StdDraw.java
 *
 *  Plots an n-by-n checkerboard.
 *
 ******************************************************************************/

public class Checkerboard { 

    public static void main(String[] args) { 
        int n = Integer.parseInt(args[0]);
        StdDraw.setXscale(0, n);
        StdDraw.setYscale(0, n);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if ((i + j) % 2 != 0) StdDraw.setPenColor(StdDraw.BLACK);
                else                  StdDraw.setPenColor(StdDraw.RED);
                StdDraw.filledSquare(i + 0.5, j + 0.5, 0.5);
            }
        }
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. 
Last updated: Fri Oct 20 14:12:12 EDT 2017.

[/spoiler]


RandomBlock_YI.java Animation
Use the tools from the BouncingBall program to implement RandomBlock_YI.java. This program re-uses the program from the graph paper assignment and randomly select squares on the grid that will become a solid color or “alive” and then randomly it will become back to white or “dead” again.

Input/Output: Class StdOut

Class StdOut


  • public final class StdOut
    extends Object
    This class provides methods for printing strings and numbers to standard output.Getting started. To use this class, you must have StdOut.class in your Java classpath. If you used our autoinstaller, you should be all set. Otherwise, either download stdlib.jar and add to your Java classpath or download StdOut.java and put a copy in your working directory.

    Here is an example program that uses StdOut:

       public class TestStdOut {
           public static void main(String[] args) {
               int a = 17;
               int b = 23;
               int sum = a + b;
               StdOut.println("Hello, World");
               StdOut.printf("%d + %d = %d\n", a, b, sum);
           }
       }
    

    Differences with System.out. The behavior of StdOut is similar to that of System.out, but there are a few technical differences:

    • StdOut coerces the character-set encoding to UTF-8, which is a standard character encoding for Unicode.
    • StdOut coerces the locale to Locale.US, for consistency with StdInDouble.parseDouble(String), and floating-point literals.
    • StdOut flushes standard output after each call to print() so that text will appear immediately in the terminal.

    Reference. For additional documentation, see Section 1.5 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static void main(String[] args)

      Unit tests some of the methods in StdOut.
      static void print()

      Flushes standard output.
      static void print(boolean x)

      Prints a boolean to standard output and flushes standard output.
      static void print(byte x)

      Prints a byte to standard output and flushes standard output.
      static void print(char x)

      Prints a character to standard output and flushes standard output.
      static void print(double x)

      Prints a double to standard output and flushes standard output.
      static void print(float x)

      Prints a float to standard output and flushes standard output.
      static void print(int x)

      Prints an integer to standard output and flushes standard output.
      static void print(long x)

      Prints a long integer to standard output and flushes standard output.
      static void print(Object x)

      Prints an object to standard output and flushes standard output.
      static void print(short x)

      Prints a short integer to standard output and flushes standard output.
      static void printf(Locale locale, String format, Object... args)

      Prints a formatted string to standard output, using the locale and the specified format string and arguments; then flushes standard output.
      static void printf(String format, Object... args)

      Prints a formatted string to standard output, using the specified format string and arguments, and then flushes standard output.
      static void println()

      Terminates the current line by printing the line-separator string.
      static void println(boolean x)

      Prints a boolean to standard output and then terminates the line.
      static void println(byte x)

      Prints a byte to standard output and then terminates the line.
      static void println(char x)

      Prints a character to standard output and then terminates the line.
      static void println(double x)

      Prints a double to standard output and then terminates the line.
      static void println(float x)

      Prints an integer to standard output and then terminates the line.
      static void println(int x)

      Prints an integer to standard output and then terminates the line.
      static void println(long x)

      Prints a long to standard output and then terminates the line.
      static void println(Object x)

      Prints an object to this output stream and then terminates the line.
      static void println(short x)

      Prints a short integer to standard output and then terminates the line.

    • Method Detail

      • println

        public static void println()
        Terminates the current line by printing the line-separator string.

      • println

        public static void println(Object x)
        Prints an object to this output stream and then terminates the line.
        Parameters:
        x – the object to print

      • println

        public static void println(boolean x)
        Prints a boolean to standard output and then terminates the line.
        Parameters:
        x – the boolean to print

      • println

        public static void println(char x)
        Prints a character to standard output and then terminates the line.
        Parameters:
        x – the character to print

      • println

        public static void println(double x)
        Prints a double to standard output and then terminates the line.
        Parameters:
        x – the double to print

      • println

        public static void println(float x)
        Prints an integer to standard output and then terminates the line.
        Parameters:
        x – the integer to print

      • println

        public static void println(int x)
        Prints an integer to standard output and then terminates the line.
        Parameters:
        x – the integer to print

      • println

        public static void println(long x)
        Prints a long to standard output and then terminates the line.
        Parameters:
        x – the long to print

      • println

        public static void println(short x)
        Prints a short integer to standard output and then terminates the line.
        Parameters:
        x – the short to print

      • println

        public static void println(byte x)
        Prints a byte to standard output and then terminates the line.To write binary data, see BinaryStdOut.

        Parameters:
        x – the byte to print

      • print

        public static void print()
        Flushes standard output.

      • print

        public static void print(Object x)
        Prints an object to standard output and flushes standard output.
        Parameters:
        x – the object to print

      • print

        public static void print(boolean x)
        Prints a boolean to standard output and flushes standard output.
        Parameters:
        x – the boolean to print

      • print

        public static void print(char x)
        Prints a character to standard output and flushes standard output.
        Parameters:
        x – the character to print

      • print

        public static void print(double x)
        Prints a double to standard output and flushes standard output.
        Parameters:
        x – the double to print

      • print

        public static void print(float x)
        Prints a float to standard output and flushes standard output.
        Parameters:
        x – the float to print

      • print

        public static void print(int x)
        Prints an integer to standard output and flushes standard output.
        Parameters:
        x – the integer to print

      • print

        public static void print(long x)
        Prints a long integer to standard output and flushes standard output.
        Parameters:
        x – the long integer to print

      • print

        public static void print(short x)
        Prints a short integer to standard output and flushes standard output.
        Parameters:
        x – the short integer to print

      • print

        public static void print(byte x)
        Prints a byte to standard output and flushes standard output.
        Parameters:
        x – the byte to print

      • printf

        <pre>public static void printf(String format,
                                  Object... args)</pre>
        Prints a formatted string to standard output, using the specified format string and arguments, and then flushes standard output.
        Parameters:
        format – the format string
        args – the arguments accompanying the format string

      • printf

        public static void printf(Locale locale,
                                  String format,
                                  Object... args)
        Prints a formatted string to standard output, using the locale and the specified format string and arguments; then flushes standard output.
        Parameters:
        locale – the locale
        format – the format string
        args – the arguments accompanying the format string

      • main

        public static void main(String[] args)
        Unit tests some of the methods in StdOut.
        Parameters:
        args – the command-line arguments

Input/Output: Graphics and Arrays: A Bar graph – Tracing

Let’s do some tracing with a smaller set of positive integers.

raw data array
{4,5,6,5,5,12,4,5,4,5,4,4,11,5,…} 100 random positive integers of range 1 to 20 inclusive

the goal in this assignment is to have the least number of “if” statements (if any at all)

the counter array initialy all elements are equal to ZERO — this is an array of 20 elements
counter array
{0,0,1,0,5,5,….} this is going to be the height of the rectangle

i: 0 –> 20 barCounter[0,0,0,0,0,0,0,0,0,..]

barCounter[rawData[i]]++

i = 0
rawData[0] –> 4
barCounter[rawData[0]]++
barCounter[4]++ –> 1 barCounter[0,0,0,0,1,0,0,0,0,..]

i = 1
rawData[1] –> 5
barCounter[rawData[1]]++
barCounter[5]++ –> 1 barCounter[0,0,0,0,1,1,0,0,0,..]

i = 2
rawData[2] –> 6
barCounter[rawData[2]]++
barCounter[6]++ –> 1 barCounter[0,0,0,0,1,1,1,0,0,..]


i = 3
rawData[3] –> 5
barCounter[rawData[3]]++
barCounter[5]++ –> 2 barCounter[0,0,0,0,1,2,1,0,0,..]

i = 4
rawData[4] –> 5

barCounter[5]++ –> 3 barCounter[0,0,0,0,1,3,1,0,0,..]

i = 20 [0,1,0,0,1,6,9,0,18,19,17……,1,1] // height of
//each rectangle
// the index is the x coordinate of the bargraph rectangle
for ( int i = 0; i < 100; i++)
barCounter[rawData[i]]++;

for loop to draw the rectangles where x is the index of barCounter array
and y is the value of the array barCounter with index x

// a short program to trace it with print statements
public class bargraph0
{
    public static void main(String [] args)
    {
        int raw = 0;
        int counter =0;
      int [] rawData = {4,5,6,5,5,12,4,5,4,5,4,4,11,5};
      int [] barCounter = new int[13]; // since 12 is the max value
      for ( int i = 0; i < rawData.length; i++)
      {
          System.out.println("i " + i + " rawData " + rawData[i]);
            barCounter[rawData[i]]++;
            raw = rawData[i];
            counter = barCounter[rawData[i]];
            System.out.println("barCounter array");
            for ( int j = 0; j < barCounter.length; j++)
            {
                System.out.print(barCounter[j] + " ");
            }
            System.out.println();
        }
    }
    
}
Take a look at the output:
Inital  state of the barCounter array
0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop with i from 0 to 13
i 0 rawData 4
barCounter array
0 0 0 0 1 0 0 0 0 0 0 0 0 
i 1 rawData 5
barCounter array
0 0 0 0 1 1 0 0 0 0 0 0 0 
i 2 rawData 6
barCounter array
0 0 0 0 1 1 1 0 0 0 0 0 0 
i 3 rawData 5
barCounter array
0 0 0 0 1 2 1 0 0 0 0 0 0 
i 4 rawData 5
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 0 
i 5 rawData 12
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 1 
i 6 rawData 4
barCounter array
0 0 0 0 2 3 1 0 0 0 0 0 1 
i 7 rawData 5
barCounter array
0 0 0 0 2 4 1 0 0 0 0 0 1 
i 8 rawData 4
barCounter array
0 0 0 0 3 4 1 0 0 0 0 0 1 
i 9 rawData 5
barCounter array
0 0 0 0 3 5 1 0 0 0 0 0 1 
i 10 rawData 4
barCounter array
0 0 0 0 4 5 1 0 0 0 0 0 1 
i 11 rawData 4
barCounter array
0 0 0 0 5 5 1 0 0 0 0 0 1 
i 12 rawData 11
barCounter array
0 0 0 0 5 5 1 0 0 0 0 1 1 
i 13 rawData 5
barCounter array
0 0 0 0 5 6 1 0 0 0 0 1 1 
i 0 rawData 4
barCounter array
0 0 0 0 1 0 0 0 0 0 0 0 0 
i 1 rawData 5
barCounter array
0 0 0 0 1 1 0 0 0 0 0 0 0 
i 2 rawData 6
barCounter array
0 0 0 0 1 1 1 0 0 0 0 0 0 
i 3 rawData 5
barCounter array
0 0 0 0 1 2 1 0 0 0 0 0 0 
i 4 rawData 5
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 0 
i 5 rawData 12
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 1 
i 6 rawData 4
barCounter array
0 0 0 0 2 3 1 0 0 0 0 0 1 
i 7 rawData 5
barCounter array
0 0 0 0 2 4 1 0 0 0 0 0 1 
i 8 rawData 4
barCounter array
0 0 0 0 3 4 1 0 0 0 0 0 1 
i 9 rawData 5
barCounter array
0 0 0 0 3 5 1 0 0 0 0 0 1 
i 10 rawData 4
barCounter array
0 0 0 0 4 5 1 0 0 0 0 0 1 
i 11 rawData 4
barCounter array
0 0 0 0 5 5 1 0 0 0 0 0 1 
i 12 rawData 11
barCounter array
0 0 0 0 5 5 1 0 0 0 0 1 1 
i 13 rawData 5
barCounter array
0 0 0 0 5 6 1 0 0 0 0 1 1 
Final state of the barCounter array.

Input/Output: printf

 

Java’s print() and println() methods are the ones that you have been using. The printf() method gives us more control over the appearance of the output.

  • Formatted printing basics. In its simplest form, printf() takes two arguments. The first argument is called the format string. It contains a conversion specification that describes how the second argument is to be converted to a string for output.

Format strings begin with % and end with a one-letter conversion code. The following table summarizes the most frequently used codes:

  • Format string. The format string can contain characters in addition to those for the conversion specification. The conversion specification is replaced by the argument value (converted to a string as specified) and all remaining characters are passed through to the output.
  • Multiple arguments. The printf() function can take more than two arguments. In this case, the format string will have an additional conversion specification for each additional argument.

Here is more documentation on printf format string syntax

[spoiler title=’TestPrintf’]

public class TestPrintf
{
   public static void main(String [] args)
   {
       System.out.println("123456789012345678901234567890");
       System.out.printf("%10.2f%8.2f",3.33333, 1234.5677865);
       System.out.println();
       for (int i = 0; i < 12; i++){
        System.out.printf("%2d%6d",i, i*i);
        System.out.println();
    }
        
    }
}

/**
123456789012345678901234567890
      3.33 1234.57
 0     0
 1     1
 2     4
 3     9
 4    16
 5    25
 6    36
 7    49
 8    64
 9    81
10   100
11   121

 */

[/spoiler]

 

Input/Output: Graphics: Hex. Hex is a two-player board game

Animations using java
duke

Screen Shot 2015-02-23 at 12.19.21 AM

NOTE: INCLUDE GOOD DOCUMENTATION IN YOUR PROGRAMS
Classwork:
Standard Drawing: Animation and images Images.

  1. Hex. Hex is a two-player board game popularized by John Nash while a graduate student at Princeton University, and later commercialized by Parker Brothers. It is played on a hexagonal grid in the shape of an 11-by-11 diamond Write a program Hex.java that draws the board.

Screen Shot 2016-01-06 at 6.27.23 PM