comp 14: files and graphics for applets june 19, 2000 nick vallidis
Post on 20-Dec-2015
212 views
TRANSCRIPT
COMP 14: Files and Graphics for Applets
June 19, 2000Nick 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));
orFileReader 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
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
Y
X(0, 0)
(112, 40)
112
40
Drawing a LineX
Y
10
20
150
45
You specify a start point: (10, 20)
You specify an end point: (150, 45)
andand
Drawing a RectangleX
Y
You specify the upper left corner
50
20
100
40
and
You specify the width and the height
Drawing an OvalX
Y
You specify a rectangle the oval fits in(what happens if the rectangle is a square?)
175
20
50
80
boundingboundingrectanglerectangle