Download as:
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Language:EN
Pages: 8
Words: 2985

And most the common image file formats such asjpg

3 int b = (int)(Math.random()*256);
4 Color color = new Color(r, g, b);

Suppose the file called SomeText.txt contains the following:

1003 laptop 1400

03         Scanner scan = new Scanner (new File("SomeText.txt"));
04         int max = 100;
07             String token = scan.next ();
08             int y = scan.nextInt ();
11             }
12             UI. println (max + " for " + token + " : " + x);
15         scan.close ();
16     }

Exercise

The first two exercises are in the ImageExercise class. You are to complete two methods for drawing images. Both methods involve a nested loop.

  • drawCircles should 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, then it should draw a circle centered at (120, 350). You can make them all the same colour or random colours.

  • drawNames should read a file of names and draw them on the graphics pane. Each line of the file has two numbers (x and y), a one word name, followed by three integers specifying the color (red, green, blue). For each line, it sets the color of the UI, and then draws the name at the position.

Advice for the programs: Do the core of both parts first, then go back to do the completions and challenges.

Part 1 ImageRenderer

P3

12 5

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 7 82 71 41 111

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 9 74 72 49

The rest of the file is the color values of the pixels, with three numbers for each pixel. Therefore there will be 36 numbers for the first row (up to 97 92 76 at the beginning 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 be 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 it on the screen.

There are several ppm images that you may test your method on: image-bee.ppmimage-cats.ppmimage-crane.ppmimage-flower.ppmimage-fly.ppm, and image-rose.ppm

There are png forms of the same images at the end of the assignment so you can see what they look like.

There are three animated ppm images:

  • The first four tokens do not need to be placed on separate lines, just so long as they are separated by by white space or newlines.

  • The file can contain comments that are ignored by the renderer. A comment starts with #, and lasts to the end of the line, and it can be placed anywhere between the first token (P3)and the maximum colour value, for example:

# the image was extracted from a larger image

# converted from a free gif file from the web

Furthermore, plain ppm is just one in a family of related formats. Plain pgm("portable grey map") is for grey scale images. pgm files start with the token "P2", and each pixel is represented by a single number (between 0 and the maximum colour value) specifying the grey level of the pixel.

Make your program

  • image-fly-scaled.ppm (colour depth is just 3 bits - max colour value is 7

  • image-fly-comments.ppm and image-fly-comments2.ppm (comments in the header)

  • grey-image-rose.pgm (grey scale, PGM file)

Part 2: ClassTimes

Six of the lines of the file are shown here:

COMP102 Comp-Lab Wed 1600 1700 CO219

COMP102 Tutorial Tue 1610 1700 AM104

The format of the lines is as follows:

  • The last token is the room code (The first part of the room code is always the building code, eg "HM" for Hugh McKenzie.

Note, the data is based on the real course timetable, but has been modified and simplified (and is not necessarily current for this year).

  • printRoom(String targetRoom) 
    The method has one parameter - a room code. It should print out a title: "Classes in ..." and underline it. It should then read the file and print out the course code, class type, day, start time, and end time for each class session in the target room. It should then print a message saying 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 day. It should print out a title: eg "Classes in KK201 or KK202 on Tue" and underline it. It should then read the file and print out the course code and start time for each class session that is in either target room on the day.

Challenge

Complete two methods.

Part 3: Reflection

1. ImageRenderer and ClassTimes both got their input from a file. Explain some of the differences between reading data from a file and reading data directly from the user.

Copyright © 2009-2023 UrgentHomework.com, All right reserved.