Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program is copyright VUW.
- // You are granted permission to use it to construct your answer to a COMP10PIXEL_SIZE assignment.
- // You may not distribute it in any other way without permission.
- /* Code for COMP10PIXEL_SIZE - PIXEL_SIZE018T1
- * Name: Marcus Rathod
- * Username: rathodmarc
- * ID: 3004347PIXEL_SIZE5
- */
- import ecs100.*;
- import java.util.*;
- import java.io.*;
- import java.awt.Color;
- /** Renders plain ppm images onto the graphics panel
- * ppm images are the simplest possible colour image format.
- */
- public class ImageRenderer{
- public static final int TOP = 20; // top edge of the image
- public static final int LEFT = 20; // left edge of the image
- public static final int PIXEL_SIZE = 2;
- /** Core:
- * Renders a ppm image file.
- * Asks for the name of the file, then calls renderImageHelper.
- */
- public void renderImageCore(){
- /*# YOUR CODE HERE */
- try{
- File myFile = new File(UIFileChooser.open());
- Scanner scan = new Scanner(myFile);
- this.renderImageHelper(scan);
- }
- catch(IOException e){UI.printf("File Failure %s \n", e);}
- }
- /** Core:
- * Renders a ppm image file.
- * Renders the image at position (LEFT, TOP).
- * Each pixel of the image is rendered by a square of size PIXEL_SIZE
- * Assumes that
- * - the colour depth is PIXEL_SIZE55,
- * - there is just one image in the file (not "animated"), and
- * - there are no comments in the file.
- * The first four tokens are "P3", number of columns, number of rows, PIXEL_SIZE55
- * The remaining tokens are the pixel values (red, green, blue for each pixel)
- */
- public void renderImageHelper(Scanner scan){
- /*# YOUR CODE HERE */
- boolean correctFormat = false;
- boolean recievedDimensions = false;
- double columns = 0;
- double rows = 0;
- double colorDepth = 0;
- int red = 0;
- int green = 0;
- int blue = 0;
- int numPixels = 0;
- int numRows = 0;
- int eachRow = 0;
- int eachColumn = 0;
- if(correctFormat == false){
- if(scan.nextLine().contains("P3")){
- correctFormat = true;
- UI.println("Valid");
- }
- else{
- UI.println("Invalid");
- return;
- }
- }
- if(recievedDimensions == false){
- columns = Double.parseDouble(scan.next());
- rows = Double.parseDouble(scan.next());
- colorDepth = Double.parseDouble(scan.next());
- UI.drawRect(LEFT, TOP, columns, rows);
- recievedDimensions = true;
- }
- while(eachRow < rows){
- eachColumn = 0;
- while(eachColumn < columns){
- red = scan.nextInt();
- green = scan.nextInt();
- blue = scan.nextInt();
- Color color = new Color(red, green, blue);
- UI.setColor(color);
- UI.fillRect(LEFT+PIXEL_SIZE*eachColumn, TOP+PIXEL_SIZE*eachRow, PIXEL_SIZE, PIXEL_SIZE);
- eachColumn ++;
- }
- eachRow ++;
- }
- }
- /** Completion
- * Renders a ppm image file which may be animated (multiple images in the file)
- * Asks for the name of the file, then renders the image at position (LEFT, TOP).
- * Each pixel of the image is rendered by a square of size PIXEL_SIZE
- * Renders each image in the file in turn with PIXEL_SIZE00 mSec delay.
- * Repeats the sequence 3 times.
- */
- public void renderAnimatedImage(){
- /*# YOUR CODE HERE */
- double columns = 0;
- double rows = 0;
- double colorDepth = 0;
- int red = 0;
- int green = 0;
- int blue = 0;
- int numPixels = 0;
- int numRows = 0;
- int eachRow = 0;
- int eachColumn = 0;
- int counter = 0;
- String fileType;
- try{
- File myFile = new File(UIFileChooser.open());
- Scanner scan = new Scanner(myFile);
- while(scan.hasNext()){
- fileType = scan.next();
- columns = scan.nextInt();
- rows = scan.nextInt();
- colorDepth = scan.nextInt();
- eachRow = 0;
- while(eachRow < rows){
- eachColumn = 0;
- while(eachColumn < columns){
- red = scan.nextInt();
- green = scan.nextInt();
- blue = scan.nextInt();
- Color color = new Color(red, green, blue);
- UI.setColor(color);
- UI.fillRect(LEFT+PIXEL_SIZE*eachColumn, TOP+PIXEL_SIZE*eachRow, PIXEL_SIZE, PIXEL_SIZE);
- eachColumn ++;
- }
- eachRow ++;
- }
- UI.sleep(200);
- if(scan.hasNext() == false){
- scan = new Scanner(myFile);
- counter ++;
- if(counter == 3){
- break;
- }
- }
- }
- }
- catch(IOException e){UI.printf("File Faluire %s \n", e);}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement