File input and graphics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

File Input and Graphics PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

File Input and Graphics

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

File input and graphics

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

Standard Input and Output

Command line input vs standard input

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.

Standard input and output1

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!)

Reading data from a file

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

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();

  • This opens a file for reading. Looks for mydatafile.txt in the current working directory where your program is running.

  • Reads on int value from the file into k.

  • Then reads double after double into the array. Stops when there are no more values in the file to be read.

  • The library in1

    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);


    What could go wrong when you open a file?

    What do you think happens?

    Getting the princeton libraries

    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.

    • 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

    Standard Drawing

    Read: page 136-146

    Try it out!

    Used in HW3.

    The Princeton Library StdDraw is very very nice!

    Standard draw

    Standard Draw

    Standard drawing. We provide library StdDraw to plot graphics.

    To use. Add Jar file, or ownload 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)

    Data visualization

    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);

    while (!StdIn.isEmpty()) {

    double x = StdIn.readDouble();

    double y = StdIn.readDouble();

    StdDraw.point(x, y);




    rescale coordinatesystem

    read in points,and plot them

    Data visualization1

    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


    % java PlotFilter < USA.txt

    coordinates of13,509 US cities

    Plotting a function

    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]);



    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)

    Bouncing ball

    Bouncing Ball

    publicclass BouncingBall {

    publicstaticvoid main(String[] args) {

    double rx =.480, ry =.860;

    double vx = .015, vy =.023;

    double radius = .05;

    StdDraw.setXscale(-1.0, +1.0);

    StdDraw.setYscale(-1.0, +1.0);

    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;



    StdDraw.filledCircle(rx, ry, radius);;





    constant velocity


    rescale coordinates


    update position

    clear background

    draw the ball

    turn on animation mode:display and pause for 50ms

    Special effects

    Special Effects

    Images. Put .gif, .png, or .jpg file in the working directory anduse StdDraw.picture() to draw it.

    Sound effects. Put .wav, .mid, or .au file in the working directory and use to play it.

    Ex. Modify BouncingBall to display image and play sound upon collision.

    • Replace StdDraw.filledCircle() with:

    • Add following code upon collision with wall:

    StdDraw.picture(rx, ry, "earth.gif");"boing.wav");

    Computer animation


















    Computer Animation

    Computer animation. Display a sequenceof closely related images in rapid successionto produce the illusion of movement.

    Frame rate. Use 15-70 frames per secondto "trick" human eye and brain into seeingsmooth motion.

    Ex 1. Television and motion pictures.

    Ex 2. Java mascot Duke cart-wheeling.

    Java implementation

    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);;




    T1.gif - T17.gif

  • Login