150 likes | 309 Views
COMP 14: Files and Graphics for Applets. June 19, 2000 Nick Vallidis. Announcements. P5 is due tomorrow. Homework. read Appendix J (p. 577-596) P5 (due tomorrow) P6 (due Friday). Reading Text Files Almost Just Like User Input. Section 8.4 in textbook pages 396 - 399 We know:
E N D
COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis
Announcements • P5 is due tomorrow
Homework • read Appendix J (p. 577-596) • P5 (due tomorrow) • P6 (due Friday)
Reading Text FilesAlmost Just Like User Input • Section 8.4 in textbook • pages 396 - 399 • We know: BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String line = stdin.readLine();
What does that really do? • Instantiates two objects! BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); is the same as… InputStreamReader strRdr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(strRdr);
Reading Text FilesAlmost Just Like User Input • File Input: BufferedReader filein = new BufferedReader (new FileReader (filename)); or FileReader fRdr = new FileReader(filename); BufferedReader filein = new BufferedReader (fRdr); filename is the name of the file to read String line = filein.readLine();
Reading Text FilesVisual J++ Demo • Read 10 integers from a file into an array • Print the array
Reading Text FilesIn-Class Exercise • Write code that displays the contents of a text file to the screen. • We’ll write the actual code together
Graphics for applets • Everything you've done so far has been producing output as text. • just a sequence of characters • Now we're going to talk about graphical output • everything is made of colored dots
Introduction to Graphics • Most computer programs have graphical components • A picture or drawing must be digitized for storage on a computer • A picture is broken down into pixels (picture element), and each pixel is stored separately
Representing Color • A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) • A color picture requires more information • every color can be represented as a mixture of the three primary colors Red, Green, and Blue • In Java, each color is represented by three numbers between 0 and 255 that are collectively called an RGB value
(0, 0) X Y Coordinate Systems • Each pixel can be identified using a two-dimensional coordinate system • When referring to a pixel in a Java program, we use a coordinate system with the origin in the upper left corner 112 40 (112, 40)
X Y You specify a start point: (10, 20) and You specify an end point: (150, 45) Drawing a Line 10 150 20 45
X 40 100 Y Drawing a Rectangle 50 20 You specify the upper left corner and You specify the width and the height
X 80 50 Y Drawing an Oval 175 20 bounding rectangle You specify a rectangle the oval fits in (what happens if the rectangle is a square?)