COS 130 Computer Programming Methodology Files and Colors

  • Slides: 23
Download presentation
COS 130 Computer Programming Methodology Files and Colors

COS 130 Computer Programming Methodology Files and Colors

Files May be a device, like a printer Or a section of the disk

Files May be a device, like a printer Or a section of the disk (document) By default the input is the keyboard By default the output is the screen We can redirect this with > < To get things into the program we use read instructions • To get things out of the program we use write or print instructions • • •

Letters vs. Numbers “ 11” is not the same as 11 • “ 11”

Letters vs. Numbers “ 11” is not the same as 11 • “ 11” is two letters that happen to be ‘ 1’ and ‘ 1’ • 11 is a number that we can do math with In the computer “ 11” is 00110001 11 is 1011

Text Files • Text files have only characters (letters) • Many programs know how

Text Files • Text files have only characters (letters) • Many programs know how to look at text files (Notepad, etc. ) • It is possible to use only text files However, • For some things text files become very large and inefficient.

Binary Files • May be much smaller • Allows numbers to be saved in

Binary Files • May be much smaller • Allows numbers to be saved in the format we need for computation, without translating back and forth to characters However, • Programs must be written specifically for the format used.

Binary Numbers • “Natural” for the computer • Base 2 • Roll to the

Binary Numbers • “Natural” for the computer • Base 2 • Roll to the next column every time we reach 2

Decimal 123 Ones 100 Tens 101 Hundreds 102 Binary 1010 Ones 20 Twos 21

Decimal 123 Ones 100 Tens 101 Hundreds 102 Binary 1010 Ones 20 Twos 21 Fours 22 Eights 23

Let’s Count Decimal 0 1 2 3 4 5 6 7 Binary 0 1

Let’s Count Decimal 0 1 2 3 4 5 6 7 Binary 0 1 10 11 100 101 110 111

http: //en. wikipedia. org/wiki/RGB_color_model Red, Green, Blue (RGB) Model How to Represent a Color

http: //en. wikipedia. org/wiki/RGB_color_model Red, Green, Blue (RGB) Model How to Represent a Color with 1’s and 0’s

RGB Model • • • For each pixel (spot) use 3 numbers Each number

RGB Model • • • For each pixel (spot) use 3 numbers Each number is between 0 – 255 (inclusive) First number is the Red value Second number is the Green value Third number is Blue value The higher the number the greater the color intensity

Examples 255 0 0 Red 0 255 0 Green 0 0 255 Blue

Examples 255 0 0 Red 0 255 0 Green 0 0 255 Blue

PPM Format • A file header – Tells what the rest of the file

PPM Format • A file header – Tells what the rest of the file is like – Metadata • A sequence of pixels – As many as we said in the header – Each pixel is an RGB triple

PPM Header 1. 2. 3. 4. 5. 6. 7. 8. 9. P 6 White

PPM Header 1. 2. 3. 4. 5. 6. 7. 8. 9. P 6 White spaces Width using ASCII (letters) in decimal White spaces Height using ASCII (letters) in decimal White spaces Max color value using ASCII (letters) in decimal A SINGLE White space # to end of line are comments, they may occur anywhere in the header

PPM Header Example P 6 800 600 255 P 6 #Image by John Hunt

PPM Header Example P 6 800 600 255 P 6 #Image by John Hunt #This is a rainbow 800 #Width 600 #Height 255 #This many shades

Writing to Files • System. out. print(…); – Prints characters – Always takes a

Writing to Files • System. out. print(…); – Prints characters – Always takes a single argument – Converts argument to a String Example System. out. print(“The “); System. out. print(“quick “); System. out. print(“brown fox”); Outputs The quick brown fox

Writing to Files • System. out. println(…); – Like System. out. print – But,

Writing to Files • System. out. println(…); – Like System. out. print – But, goes to the next line – This varies depending on system Example System. out. println(“The “); System. out. println(“quick “); System. out. println(“brown fox”); Outputs The quick brown fox

Writing to Files Both print and println can use special control characters n puts

Writing to Files Both print and println can use special control characters n puts a new line character in. In most situations this causes the text to go to the next line. t puts a tab character in. In most situations this causes multiple spaces to be displayed.

Writing to Files Example Outputs System. out. print(“The n“); System. out. println(“quick n“); System.

Writing to Files Example Outputs System. out. print(“The n“); System. out. println(“quick n“); System. out. println(“brown foxn”); The quick brown fox

Writing to Files A Caution • System. out. println(); uses two characters to move

Writing to Files A Caution • System. out. println(); uses two characters to move to a new line on MS Windows • PPM allows only a single white space character at the end of a header • To avoid this use System. out. print(“n”); which will always use just a single new line character

Writing to Files • System. out. write(…); – Writes binary – Will use the

Writing to Files • System. out. write(…); – Writes binary – Will use the least number of bytes Example System. out. write(255); System. out. write(0); Example int red = 255; int green = 0; int blue = 0; System. out. write(red); System. out. write(green); System. out. write(blue);

Write the whole file • When writing to disk, using write, your program puts

Write the whole file • When writing to disk, using write, your program puts data in memory, then moves it in big blocks to disk • When your program ends the data in memory is thrown away without being written • You must tell your program when to wrap it up • Use System. out. flush();

Example public class Flush { public static void main(String args[]) { for(int i =

Example public class Flush { public static void main(String args[]) { for(int i = 0; i < 800; i=i+1){ System. out. write(i); } System. out. flush(); } }

A Last Trick int num. Times = 100; for(int i=0; i < num. Times;

A Last Trick int num. Times = 100; for(int i=0; i < num. Times; i=i+1) { System. out. println(i%4); } What does this print out?