Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. // This program is copyright VUW.
  2. // You are granted permission to use it to construct your answer to a COMP10PIXEL_SIZE assignment.
  3. // You may not distribute it in any other way without permission.
  4.  
  5. /* Code for COMP10PIXEL_SIZE - PIXEL_SIZE018T1
  6. * Name: Marcus Rathod
  7. * Username: rathodmarc
  8. * ID: 3004347PIXEL_SIZE5
  9. */
  10.  
  11. import ecs100.*;
  12. import java.util.*;
  13. import java.io.*;
  14. import java.awt.Color;
  15.  
  16. /** Renders plain ppm images onto the graphics panel
  17. * ppm images are the simplest possible colour image format.
  18. */
  19.  
  20. public class ImageRenderer{
  21. public static final int TOP = 20; // top edge of the image
  22. public static final int LEFT = 20; // left edge of the image
  23. public static final int PIXEL_SIZE = 2;
  24.  
  25. /** Core:
  26. * Renders a ppm image file.
  27. * Asks for the name of the file, then calls renderImageHelper.
  28. */
  29. public void renderImageCore(){
  30. /*# YOUR CODE HERE */
  31. try{
  32. File myFile = new File(UIFileChooser.open());
  33. Scanner scan = new Scanner(myFile);
  34. this.renderImageHelper(scan);
  35. }
  36. catch(IOException e){UI.printf("File Failure %s \n", e);}
  37.  
  38.  
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45. /** Core:
  46. * Renders a ppm image file.
  47. * Renders the image at position (LEFT, TOP).
  48. * Each pixel of the image is rendered by a square of size PIXEL_SIZE
  49. * Assumes that
  50. * - the colour depth is PIXEL_SIZE55,
  51. * - there is just one image in the file (not "animated"), and
  52. * - there are no comments in the file.
  53. * The first four tokens are "P3", number of columns, number of rows, PIXEL_SIZE55
  54. * The remaining tokens are the pixel values (red, green, blue for each pixel)
  55. */
  56. public void renderImageHelper(Scanner scan){
  57. /*# YOUR CODE HERE */
  58. boolean correctFormat = false;
  59. boolean recievedDimensions = false;
  60.  
  61. double columns = 0;
  62. double rows = 0;
  63. double colorDepth = 0;
  64.  
  65. int red = 0;
  66. int green = 0;
  67. int blue = 0;
  68.  
  69. int numPixels = 0;
  70. int numRows = 0;
  71.  
  72. int eachRow = 0;
  73. int eachColumn = 0;
  74.  
  75. if(correctFormat == false){
  76. if(scan.nextLine().contains("P3")){
  77. correctFormat = true;
  78. UI.println("Valid");
  79. }
  80. else{
  81. UI.println("Invalid");
  82. return;
  83. }
  84. }
  85.  
  86. if(recievedDimensions == false){
  87. columns = Double.parseDouble(scan.next());
  88. rows = Double.parseDouble(scan.next());
  89. colorDepth = Double.parseDouble(scan.next());
  90.  
  91. UI.drawRect(LEFT, TOP, columns, rows);
  92. recievedDimensions = true;
  93. }
  94.  
  95. while(eachRow < rows){
  96. eachColumn = 0;
  97.  
  98. while(eachColumn < columns){
  99. red = scan.nextInt();
  100. green = scan.nextInt();
  101. blue = scan.nextInt();
  102.  
  103. Color color = new Color(red, green, blue);
  104. UI.setColor(color);
  105.  
  106. UI.fillRect(LEFT+PIXEL_SIZE*eachColumn, TOP+PIXEL_SIZE*eachRow, PIXEL_SIZE, PIXEL_SIZE);
  107.  
  108. eachColumn ++;
  109. }
  110.  
  111. eachRow ++;
  112. }
  113. }
  114.  
  115.  
  116.  
  117.  
  118. /** Completion
  119. * Renders a ppm image file which may be animated (multiple images in the file)
  120. * Asks for the name of the file, then renders the image at position (LEFT, TOP).
  121. * Each pixel of the image is rendered by a square of size PIXEL_SIZE
  122. * Renders each image in the file in turn with PIXEL_SIZE00 mSec delay.
  123. * Repeats the sequence 3 times.
  124. */
  125. public void renderAnimatedImage(){
  126. /*# YOUR CODE HERE */
  127. double columns = 0;
  128. double rows = 0;
  129. double colorDepth = 0;
  130.  
  131. int red = 0;
  132. int green = 0;
  133. int blue = 0;
  134. int numPixels = 0;
  135. int numRows = 0;
  136.  
  137. int eachRow = 0;
  138. int eachColumn = 0;
  139.  
  140. int counter = 0;
  141.  
  142. String fileType;
  143.  
  144. try{
  145. File myFile = new File(UIFileChooser.open());
  146. Scanner scan = new Scanner(myFile);
  147.  
  148. while(scan.hasNext()){
  149. fileType = scan.next();
  150. columns = scan.nextInt();
  151. rows = scan.nextInt();
  152. colorDepth = scan.nextInt();
  153. eachRow = 0;
  154.  
  155. while(eachRow < rows){
  156. eachColumn = 0;
  157.  
  158. while(eachColumn < columns){
  159. red = scan.nextInt();
  160. green = scan.nextInt();
  161. blue = scan.nextInt();
  162.  
  163. Color color = new Color(red, green, blue);
  164. UI.setColor(color);
  165.  
  166. UI.fillRect(LEFT+PIXEL_SIZE*eachColumn, TOP+PIXEL_SIZE*eachRow, PIXEL_SIZE, PIXEL_SIZE);
  167.  
  168. eachColumn ++;
  169. }
  170.  
  171. eachRow ++;
  172. }
  173. UI.sleep(200);
  174.  
  175. if(scan.hasNext() == false){
  176. scan = new Scanner(myFile);
  177. counter ++;
  178.  
  179. if(counter == 3){
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. catch(IOException e){UI.printf("File Faluire %s \n", e);}
  186.  
  187.  
  188.  
  189. }
  190.  
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement