Java Program Reading in a Ppm File Convert Tp Grey Scale Pgm
java programming course help, Coffee Homework Help, Coffee Questions
Hints about Colours:
You can use UI.setColor(new Color(r, k, b)) to set the color of the UI to a new colour if r, k, and b are integers containing the red, green, and blue component values. r, g, and b must be between 0 and 255 (inclusive).
To obtain a random colour, you can employ the Math.random method:
| ane | int r = (int)(Math.random()*256); | |
| 2 | int g = (int)(Math.random()*256); | |
| 3 | int b = (int)(Math.random()*256); | |
| 4 | Colour color = new Color(r, g, b); | |
Exam Do
Suppose the file called SomeText.txt contains the post-obit:
1003 laptop 1400
1009 disk 279
1021 laser 1899
1017 printer 630
What will the following filePrint method print out? Hint: continue track of the values of all the variables
| 01 | public void filePrint (){ |
| 02 | try{ |
| 03 | Scanner browse = new Scanner (new File("SomeText.txt")); |
| 04 | int max = 100; |
| 05 | while ( browse.hasNext() ) { |
| 06 | int x = scan.nextInt (); |
| 07 | String token = scan.next (); |
| 08 | int y = scan.nextInt (); |
| 09 | if (x > y ) { |
| 10 | max = y; |
| 11 | } |
| 12 | UI. println (max + " for " + token + " : " + x); |
| 13 | } |
| fourteen | UI. println ("done: " + max); |
| xv | scan.close (); |
| 16 | } |
| 17 | catch(IOException e){UI.println("Fail: " + east);} |
| 18 | } |
Practice
The first two exercises are in the ImageExercise class. Y'all are to consummate two methods for drawing images. Both methods involve a nested loop.
The 2nd series of exercises is in the FilesExercise form. It contain seven methods that each involve reading some information from a file and doing something with the data. You lot should complete the first method (printFile) and and then consummate methods until you take written at least two past yourself. Because each exercise addresses unlike kinds of file structure, you may notice it helpful to do all the methods.
- sumNumbersshould read numbers from a file called "numbers.txt" which contains merely numbers. Information technology should add all the numbers upward and impress the full at the end. (The total should exist 2174).
- drawCirclesshould read a file containing pairs of numbers. It should draw a circle of radius 20 at each position that it reads from the file. For example, if a line of the file has 120 350 on it, and then it should draw a circle centered at (120, 350). You tin brand them all the aforementioned colour or random colours.
- drawNamesshould read a file of names and draw them on the graphics pane. Each line of the file has two numbers (10 and y), a one word name, followed by iii integers specifying the color (red, green, bluish). For each line, information technology sets the color of the UI, and and so draws the name at the position.
- firstWordsshould read a file chosen "text.txt" and print out merely the first token (word) on each line. You should employ next() to read the first word, andnextLine() to read the rest of the characters on the line.
- longestWordshould read all the words in a file called "text.txt". At the end, it should print out the longest word. You volition need to use the length() method which can be chosen on Strings to find the length of each word that you read.
- largeCoursesshould read data from a file called "course-counts.txt" that has a coursecode and a class size (the number of students) on each line. The method should count (and print out) how many of the courses have more than 100 students.
- totalOrdershould read data from a file called "order.txt" that has an item, a count and a unit-cost on each line. The method should calculate and impress the full cost of the club.
Advice for the programs: Do the cadre of both parts first, and so go back to do the completions and challenges.
Part one ImageRenderer
Digital image files are ubiquitous, and we happily expect our computers to show images in all sorts of contexts. The cadre information in a digital prototype is the color of each individual pixel of the image, so an image file must store that information. A colour can be represented past a triple of three numbers, 1 for each of the cerise, green, and blue components of the colour, so we can stand for an image in a file by storing three numbers for each pixel. Exactly how we do that depends on the format of the image file. A very simple format will just listing all the numbers, then that a 100x100 pixel image (which is pretty small) would accept xxx,000 numbers: three numbers for each of the ten,000 pixels. All the same, this will stop upwardly being a rather big file, and most of the common image file formats (such as jpg, gif, and png) have more circuitous formats in which the information is encoded and compressed to save infinite and remove back-up. Turning the contents of these compressed image files into a coloured prototype on the screen tin can be a very complicated process, but the files tin be very much smaller than when using the simple format.
For this assignment, you will write a plan to render image files in the plainly ppm format ("portable pixel map") - one of the simplest possible (and least efficient) file formats.
A obviously ppm file starts with a 4 pieces of information describing the paradigm, followed by the colour values for each pixel in turn, starting at the top left of the image, and working from left to right along each row and working row past row down the image.
For example, here is a piddling apparently ppm paradigm file ( image-tiny.ppm , which is a small-scale function of the back of the bee from the prototype-bee.ppm file):
P3
12 5
255
200 182 163 215 198 177 130 116 93 37 28 nine 31 22 seven
81 67 38 83 71 42 half-dozen 5 6 0 0 0 57 68 60 97 112 104
97 92 76 202 186 165 97 82 60 32 25 five 38 30 13 103 90
63 158 140 97 58 49 25 43 42 17 107 104 74 127 140
113 95 102 79 66 58 41 71 57 37 41 30 seven 82 71 41 111
95 64 174 157 120 115 101 63 49 43 12 67 65 thirty 126
124 74 133 136 97 88 87 62 98 93 54 78 63 37 108 93
62 121 104 69 135 120 88 190 172 139 36 30 15 1 0 0
16 17 9 64 77 58 l 57 39 seven 2 0 105 106 64 121 103 71
117 100 67 159 144 113 212 197 171 161 146 114 0 0 0
0 0 0 37 48 32 72 88 68 24 26 19 12 12 nine 74 72 49
The first token is the string P3 which identifies the format of this image file every bit a plain ppm file.
The second and tertiary tokens are integers that are the size of the image: the width (number of columns of pixels) and the superlative (the number of rows of pixels). In this instance, there are 5 rows of 12 pixels each.
The fourth token specifies the maximum possible value of the colour values for the pixels. In this example, the colour values for the cherry-red, green, and blue components each go between 0 and 255. 0 would mean nothing for that component; 255 would hateful the maximum strength for the component. That ways that a colour consisting of three 0's would be black; three 255'southward would exist white; a 255 0 0 would be vivid reddish. This means that there are 256x256x256 = almost 17 million distinct possible colours.
[If the color depth were only 7, then each colour value would be between 0 and vii and there would merely be 8x8x8 = 512 possible colors. This would represent to a "colour depth" of 3 bits per component.]
The remainder of the file is the color values of the pixels, with three numbers for each pixel. Therefore there will exist 36 numbers for the first row (upwardly to 97 92 76 at the kickoff of the 3rd line), 36 numbers for the second row, etc. The line breaks don't mean anything special, (although the format specifies that the numbers should exist broken into lines with no more than 70 characters).
You are to complete the ImageRenderer program that reads a a plain ppm file and renders information technology on the screen.
Core
Complete the renderImage method. It should ask the user for a file (for example, using the UIFileChooser.open method), and read the file, rendering the pixels to the graphics pane. It should check that the file has the correct "magic number". If not, it should impress a message and exit the method. Otherwise, it should read the number of columns and the number of rows. It should then read the colour depth, but it can simply ignore information technology and assume that information technology is the "normal" 255. Information technology should and so read the three color values for each pixel in plough, setting the color of the UI and then cartoon a square pixel on the graphics pane. It will demand a nested loop to piece of work along the columns for each row and piece of work down all the rows.
The tiptop left corner of the image should be placed at (twenty,20), and each pixel should be drawn equally a 2x2 square (ie, the image should be zoomed-in 200%). Use the constants divers at the top of the file.
Hint: Y'all will need a nested loop to do this: an outer loop that goes through each row, and an inner loop that goes forth the columns of the current row.
There are several ppm images that you may test your method on: image-bee.ppm, image-cats.ppm, paradigm-crane.ppm, image-blossom.ppm, image-fly.ppm, and image-rose.ppm
There are png forms of the same images at the end of the assignment so yous can come across what they await similar.
Completion
You lot are to consummate the renderAnimatedImage method that renders ppm files containing animated images.
A ppm file may comprise a sequence of images right after each other, representing an "animated" paradigm, which should be rendered by drawing the first image, pausing briefly, then drawing the second prototype in its identify, and repeating until all the images have been drawn. Each image will accept the four "header" tokens, and then the pixel colour values. This is the aforementioned process as an "animated gif", (though gifs are encoded more efficiently and have more than options).
Your renderAnimatedImage method should handle animated images past continuing to return images from the file until there are no more images in the file. Use a 100 - 200 millisecond delay between images. You may brand it display the images but once, or repeat a stock-still number of times, or repeat infinitely (though I recommend confronting the concluding option since it makes testing more difficult).
There are three animated ppm images:
- multi-paradigm-ball.ppm,
- multi-paradigm-athlete.ppm,
- multi-image-flag.ppm
Challenge
The existent plain ppm format is really a little more circuitous than described higher up:
- The first iv tokens practise not need to be placed on divide lines, just so long equally they are separated by by white infinite or newlines.
- The file tin can contain comments that are ignored by the renderer. A annotate starts with #, and lasts to the end of the line, and it tin can be placed anywhere betwixt the first token (P3)and the maximum colour value, for example:
P3 # a plain ppm file
# this is a part of a bee
12 # width of paradigm
5 # height of paradigm
# the epitome was extracted from a larger image
# converted from a free gif file from the web
255
200 182 163 215 198 177 130 116 93 37 28 nine 31 22 7 81
67 38 83 71 42 half-dozen 5 6 0 .....
[Actually, the comments are even more than flexible than this, simply this is good enough]
Furthermore, plain ppm is just one in a family of related formats. Manifestly pgm("portable grey map") is for greyness scale images. pgm files start with the token "P2", and each pixel is represented past a single number (betwixt 0 and the maximum color value) specifying the grayness level of the pixel.
Brand your program
- handle comments (by noticing them and throwing them away).
- handle the colour depth properly: if the colour depth is 15, for example, it should scale up all the values past 255/15 before setting the color.
- Recognise and render plain pgm images also (use the first token to determine which version to render).
There are several images with these additional features which yous can test your method on, some scaled, some with comments, one both scaled and with comments:
- paradigm-fly-scaled.ppm(color depth is merely 3 bits - max colour value is 7
- image-fly-comments.ppmand epitome-fly-comments2.ppm (comments in the header)
- image-fly-scaled-comments.ppm(scaled and with comments)
- multi-paradigm-flag-scaled.ppm
- multi-paradigm-flag-comments.ppm
- multi-paradigm-flag-scaled-comments.ppm
- grey-image-rose.pgm (grey scale, PGM file)
Part 2: ClassTimes
The academy timetable system outputs a listing of all the classes of all the courses in each trimester. However, the list is very long, and using information technology can be anything from irksome ( eg finding all the rooms for a particular course) to downright painful ( eg , finding all the courses that have a lecture in a detail room).
You lot are to complete the ClassTimes program that will assistance a user find certain information from (a simplified version of) this timetable more quickly. The program has viii methods, each of which will read the data from a file containing the class timetable data, and print out (to the text pane and/or to a file) just the data that is relevant to the user. All the methods are closely related and volition have a very similar structure: loop through the file, reading the values on each line and processing them.
The class timetable file is called classdata.txt.
Each class may have lots of unlike classes: information technology may take several lectures and/or tutorials and/or laboratory sessions etc on different days at dissimilar times in different rooms. Each line of the file specifies one grade or session for a form, so that there may be may exist many lines for a each course.
Six of the lines of the file are shown here:
| COMP102 | Comp-Lab | Wednesday | 1600 | 1700 | CO219 |
| COMP102 | Comp-Lab | Midweek | 1600 | 1700 | CO238 |
| COMP102 | Lecture | Mon | 1100 | 1150 | KKLT303 |
| COMP102 | Lecture | Wednesday | 1100 | 1150 | KKLT303 |
| COMP102 | Lecture | Fri | 1100 | 1150 | KKLT303 |
| COMP102 | Tutorial | Tue | 1610 | 1700 | AM104 |
The format of the lines is as follows:
- The get-go token is the course lawmaking
- The second token is the blazon of class
- The tertiary token is the mean solar day of the week
- The fourth and 5th tokens are the offset and end fourth dimension of the session (using 24 hour time)
- The last token is the room lawmaking (The first function of the room code is always the edifice code, eg "HM" for Hugh McKenzie.
Note, the data is based on the real class timetable, but has been modified and simplified (and is non necessarily current for this yr).
Core
Complete four methods:
- printAll
The method should impress out the whole txt file, i line at a time. This method will be easy to implement if you lot accept done the exercise, and will let you look at the contents of the file. - printCourse(String targetCourse)
The method has one parameter - a course code. Information technology should read the classdata file, printing out (to the concluding window) all the class type, day, first and end times, and room for each class session of the given course ( ie , data from all the lines where the beginning token on the line is equal to the course code). If there are no classes for the form, it should impress out a bulletin that the course has no class times.
It volition need to read the beginning token on each line using next(), and can then tin can either read the rest of the line using nextLine() or read the each value on the rest of the line separately. Yous will need to utilize some of the methods in the Cord - printRoom(String targetRoom)
The method has one parameter - a room code. Information technology should print out a title: "Classes in ..." and underline information technology. It should and then read the file and print out the course code, class blazon, day, start time, and cease time for each class session in the target room. It should then impress a message maxim how many classes were in the room, for example "No courses are booked in KKLT302", or "There are 25 classes in VZ103". - printInRoomsOnDay(String targetRoom1, String targetRoom2, String targetDay)
The method has three parameters - two room codes and a twenty-four hour period. It should print out a title: eg "Classes in KK201 or KK202 on Tue" and underline it. It should then read the file and impress out the course code and start time for each form session that is in either target room on the 24-hour interval.
Completion
Consummate two methods.
- bookingsFileForRoom(String targetRoom)
The method has one parameter - a room code. It should construct a new file, whose name is the room code, followed past "_Bookings.txt" [_eg_"HMLT205_Bookings.txt"] with a list of the classes booked in that room. The first line of the file should specify what room the bookings are for, eg:
Bookings for room HMLT205
Each form booked in the room should exist printed to the file on three lines with the course, the time, and the type, followed by a blank line, _eg_
Grade: ACCY130
Time: Mon 1200-1250
Session: LectureThe method should print a message when it has finished writing the file.
Hint: Starting time by press the output to the UI window using UI.println, and then arrive write to the file. - inBuildingAfterTime(String targetBuilding, String targetDay, int targetStart)
The method has three parameters - a building lawmaking, a 24-hour interval, and a offset time. It should impress a list of all the classes (class code, blazon, and room) that are in the specified building and start at or after the specified beginning fourth dimension.
Note that the beginning part of every room name is the edifice code in uppercase.
Challenge
Consummate two methods.
- double meanClassLength(Cord targetRoom)
The method returns (non prints) the average (mean) duration in minutes of all classes scheduled in the specified room.
Hints: be careful with the times - they are not ordinary integers, and
do not cause an error if there are no classes in the room. - potentialDisruptions(String building, String targetDay, int targetStart, int targetEnd)
Lists all the courses (only the grade code) that have classes in a given edifice on a given day such that any office of the class is between the given times. Each course involved should only be listed one time, fifty-fifty if it has several classes in the building in the time catamenia. Note that the data file is ordered by the course code.
This method is like to, but somewhat more than complex than, one of the completion questions.
Testing your program:
To test your plan, compare the output of your program to the output of the demo program.
Part 3: Reflection
- ImageRendererand ClassTimes both got their input from a file. Explain some of the differences between reading information from a file and reading data directly from the user.
- The series of methods inClassTimeswere progressively more tricky. Identify what information technology was well-nigh the later ones that made them more than difficult.
- The lines in classdata.txt were sorted by the coursecode. What queries would exist easier or take nicer output if they had been sorted past one of the other fields?
- Add to your programming "concept map" the boosted concepts about programming and nigh Java that you accept learned this week. (Every bit usual, copy your answer from last calendar week, fixing upwards any of the descriptions if you have a better understanding of them at present, and add the new concepts you lot have acquired this week, along with one judgement descriptions of them.
png images showing what the ppm images should look like.
| image-bee: | paradigm-cats |
| |
| epitome-crane: | paradigm-flower |
| |
| image-fly: | grey-image-rose |
| |
Source: https://www.myassignmenthelp.net/question/colors-in-java
Postar um comentário for "Java Program Reading in a Ppm File Convert Tp Grey Scale Pgm"