File Input and Graphics • An extract of slides from set 05_inout.ppt • Designed to get you ready for the HW3. Standard Input and Output Command-line Input vs. Standard Input Command line inputs. Use command line inputs to read in a few user values. Not practical for many user inputs. Input entered before program begins execution. Standard input. Flexible OS abstraction for input. By default, stdin is received from Terminal window. Input entered while program is executing. 3 Standard Input and Output Standard input. Book authors provide library StdIn to read text input. Standard output. Book authors provide library StdOut to write text output. (But versions of Java ≥ 5.0 have printf() -- so we can use System.out just like StdOut!) 4 Reading Data from a File Recall how we read from the standard input? StdIn: Princeton provides a library to simplify this Methods called on StdIn include: • readInt(), readDouble(), readString(), …, isEmpty() Here’s a good idea: Let’s use the same interface to read from a data file! (Interface here means same methods.) Issues: Need to “connect to” or “open” the data file. There’s just one standard input, but we could read from multiple files. The library In In: Another Princeton provided library that simplifies the standard Java ways to do this. Use In in a very similar way to StdIn -- but you must: declare a new In object and create it by opening a file. use your new In object by name instead of StdIn. In fileIn = new In(“mydatafile.txt”); int k = fileIn.readInt(); double[] data = new double[100]; int i = 0; while ( ! fileIn.isEmpty() ) data[i++] = fileIn.readDouble(); 1.This opens a file for reading. Looks for mydatafile.txt in the current working directory where your program is running. 2.Reads on int value from the file into k. 3.Then reads double after double into the array. Stops when there are no more values in the file to be read. 6 The library In Declaring an In object: In fileIn = new In(“mydatafile.txt”); Remember what defining a new array looks like? double[] data = new double[100]; In general we’ll see later that in Java we often do things like: Type variable = new Type(some initialization info); Questions: What could go wrong when you open a file? What do you think happens? 7 Getting the Princeton Libraries Java’s standard libraries (e.g. Math, System, etc.) Automatically included when you build a Java program Other libraries (your’s, our’s, Princton’s): •You must add them to your project, folder, etc. using your IDE. •These can be in source files, e.g. StdIn.java •Or, in an archive file called a Jar file We provide a Jar file with all the Princeton libraries: stdlib.jar Downlaod stdlib.jar and then: In DrJava: from Preference menu, choose Resource Locations Then click Add by Extra Classpath and find and select stdlib.jar Standard Drawing Read: page 136-146 Try it out! Used in HW3. The Princeton Library StdDraw is very very nice! Standard Draw Standard drawing. We provide library StdDraw to plot graphics. To use. Add Jar file, or ownload StdDraw.java and put in working directory. public class Triangle { public static void main(String[] args) { double t = Math.sqrt(3.0) / 2.0; StdDraw.line(0.0, 0.0, 1.0, 0.0); StdDraw.line(1.0, 0.0, 0.5, t); StdDraw.line(0.5, t, 0.0, 0.0); StdDraw.point(0.5, t/3.0); } } (½, ½3) % java Triangle (0, 0) (1, 0) 10 Data Visualization Plot filter. Read in a sequence of (x, y) coordinates from standard input, and plot using standard drawing. public class PlotFilter { public static void main(String[] args) { double xmin = StdIn.readDouble(); double ymin = StdIn.readDouble(); double xmax = StdIn.readDouble(); double ymax = StdIn.readDouble(); StdDraw.setXscale(xmin, xmax); StdDraw.setYscale(ymin, ymax); rescale coordinate system while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); StdDraw.point(x, y); } read in points, and plot them } } 11 Data Visualization If using re-direction, looks like this slide. But see our demo Java code that reads from the file. bounding box % more < USA.txt 669905.0 247205.0 1244962.0 490000.0 1097038.8890 245552.7780 1103961.1110 247133.3330 1104677.7780 247205.5560 ... coordinates of 13,509 US cities % java PlotFilter < USA.txt 12 Plotting a Function double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(4*Math.PI*i/N) + Math.sin(20*Math.PI*i/N); StdDraw.setXscale(0, N); StdDraw.setYscale(-2.0, +2.0); for (int i = 0; i < N; i++) StdDraw.line(i, a[i], i+1, a[i+1]); y sin 4x sin20x 13 Animation Animation loop. Repeat the following: Clear the screen. Move the object. Draw the object. Display and pause for a short while. Ex. Bouncing ball. Ball has position (rx, ry) and constant velocity (vx, vy). Detect collision with wall and reverse velocity. (+1, +1) (vx, vy) (rx, ry) (-1, -1) 14 Bouncing Ball public class BouncingBall { public static void main(String[] args) { double rx = .480, ry = .860; position double vx = .015, vy = .023; constant velocity radius double radius = .05; StdDraw.setXscale(-1.0, +1.0); StdDraw.setYscale(-1.0, +1.0); rescale coordinates while(true) { if (Math.abs(rx + vx) > 1.0) vx = -vx; if (Math.abs(ry + vy) > 1.0) vy = -vy; rx = rx + vx; ry = ry + vy; update position StdDraw.clear(StdDraw.GRAY); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); StdDraw.show(50); } } } bounce clear background draw the ball turn on animation mode: display and pause for 50ms 15 Special Effects Images. Put .gif, .png, or .jpg file in the working directory and use StdDraw.picture() to draw it. Sound effects. Put .wav, .mid, or .au file in the working directory and use StdAudio.play() to play it. Ex. Modify BouncingBall to display image and play sound upon collision. Replace StdDraw.filledCircle() with: StdDraw.picture(rx, ry, "earth.gif"); Add following code upon collision with wall: StdAudio.play("boing.wav"); 16 Computer Animation Computer animation. Display a sequence of closely related images in rapid succession to produce the illusion of movement. Frame rate. Use 15-70 frames per second to "trick" human eye and brain into seeing smooth motion. Ex 1. Television and motion pictures. Ex 2. Java mascot Duke cart-wheeling. 1 10 2 11 3 12 4 13 5 14 6 15 7 16 8 17 9 http://java.sun.com/docs/books/tutorial 17 Java Implementation public class Duke { public static void main(String[] args) { int images = 17; int WIDTH = 130, HEIGHT = 80; StdDraw.setCanvasSize(WIDTH, HEIGHT); for (int t = 0; true; t++) { int i = 1 + (t % images); String file = "T" + i + ".gif"; StdDraw.picture(0.5, 0.5, file); StdDraw.show(100); T1.gif } T17.gif } } 18