comp 14: files and graphics for applets june 19, 2000 nick vallidis

15
COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Post on 20-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

COMP 14: Files and Graphics for Applets

June 19, 2000Nick Vallidis

Page 2: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Announcements

P5 is due tomorrow

Page 3: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Homework

read Appendix J (p. 577-596)P5 (due tomorrow)P6 (due Friday)

Page 4: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 5: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 6: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 7: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Reading Text FilesVisual J++ Demo

Read 10 integers from a file into an array

Print the array

Page 8: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 9: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 10: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 11: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 12: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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

Page 13: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Drawing a LineX

Y

10

20

150

45

You specify a start point: (10, 20)

You specify an end point: (150, 45)

andand

Page 14: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

Drawing a RectangleX

Y

You specify the upper left corner

50

20

100

40

and

You specify the width and the height

Page 15: COMP 14: Files and Graphics for Applets June 19, 2000 Nick Vallidis

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