File Input and Graphics An extract of slides from set 05_inout.ppt Designed to get you ready for the HW3.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Week 8 - Monday.  What did we talk about last time?  StdAudio.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Video Game Design Lesson 1. Game Designer Person involved in the development of a video game Person involved in the development of a video game Usually.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
Announcement About HW-2 Deadline Extended – 11:55 PM Thursday 5% Extra Credit if you meet the original deadline See the from the TAs for the details.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lesson One: The Beginning
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Getting Started Example ICS2O curriculum
Every week: Sign in at the door If you are new: Fill in Registration Form Ask a Mentor how to get started Make sure you are on the Athenry Parents/Kids.
Chapter 5 - Making Music: An On-Screen Piano
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
G RAPHICS P ROGRAMMING Lecture 3 - Simple Animation - Simple 3D Drawing.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
POS 406 Java Technology And Beginning Java Code
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Copyright © Curt Hill Turtles The beginning of media computation.
How to start Visual Studio 2008 or 2010 (command-line program)
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)
Introduction to Flash Animation CS 318. Topics Introduction to Flash and animation The Flash development environment Creating Flash animations  Layers.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
Introduction to Processing. 2 What is processing? A simple programming environment that was created to make it easier to develop visually oriented applications.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
CIS 3.5 Lecture 2.2 More programming with "Processing"
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Computer Programming Modeling a Passive Solar Home.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Computer Science I Programming in Java (programming using Processing IN Java, using IntelliJ IDE) Classwork/Homework: copy your Processing projects over.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Creating Games with PowerPoint: The SECRET: Sequence – the order of the slides Connection – how the slides link together.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Today's Ninja Challenge: Write Your First Computer Game!
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Example: Card Game Create a class called “Card”
Week 8 - Monday CS 121.
More on Graphical User Interfaces
CS 177 Week 4 Recitation Slides
Game Loop Update & Draw.
Constructors, GUI’s(Using Swing) and ActionListner
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 2/14/11.
The beginning of media computation Followed by a demo
Animation Translation.
Presentation transcript:

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

3 Command-line Input vs. Standard Input Command line inputs. n Use command line inputs to read in a few user values. n Not practical for many user inputs. n Input entered before program begins execution. Standard input. n Flexible OS abstraction for input. By default, stdin is received from Terminal window. n Input entered while program is executing.

4 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 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: n declare a new In object and create it by opening a file. n 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 Download 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 Try it out! Used in HW3. The Princeton Library StdDraw is very very nice!

10 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. (0, 0) (1, 0) (½, ½  3) 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); } % java Triangle

11 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 coordinate system read in points, and plot them

12 Data Visualization % more < USA.txt % java PlotFilter < USA.txt coordinates of 13,509 US cities bounding box If using re-direction, looks like this slide. But see our demo Java code that reads from the file.

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

14 Animation Animation loop. Repeat the following: n Clear the screen. n Move the object. n Draw the object. n Display and pause for a short while. Ex. Bouncing ball. Ball has position (rx, ry) and constant velocity (vx, vy). n Detect collision with wall and reverse velocity. (rx, ry) (vx, vy) (-1, -1) (+1, +1)

15 Bouncing Ball public class BouncingBall { public static void 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.clear(StdDraw.GRAY); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); StdDraw.show(50); } bounce position constant velocity update position clear background draw the ball turn on animation mode: display and pause for 50ms rescale coordinates radius

16 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: n Add following code upon collision with wall: StdAudio.play("boing.wav"); StdDraw.picture(rx, ry, "earth.gif");

17 Computer animation. Display a sequence of closely related images in rapid succession to produce the illusion of movement. Frame rate. Use 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. Computer Animation

18 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