Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- /***************** FILE: ImageMatrix.java **********/
- public class ImageMatrix extends Image
- {
- // Your data members here
- private int[][] bitArray;
- private int int_k;
- public ImageMatrix(String inputFile) {
- /*--------- READ A FILE ------------*/
- Scanner inputScanner = null; // initialize the scanner
- // try to read the file, if an error occurs throw exception and tell the user
- try {
- // create a new scanner for the specified input file
- inputScanner = new Scanner(new BufferedReader(new FileReader(inputFile)));
- int_k = Integer.parseInt(inputScanner.nextLine());
- // Initialize the bitArray[][] used for storing the input file
- bitArray = new int[(int)Math.pow(2, int_k)][(int)Math.pow(2, int_k)];
- // Extraction of the the matrix from the specified file
- for(int i = 0 ; i < (int)Math.pow(2, int_k) ; i++) {
- String temp = inputScanner.nextLine();
- for(int j = 0 ; j < temp.length() ; j++) {
- char tempInt = temp.charAt(j);
- bitArray[i][j] = (int)tempInt;
- } // end for loop through the temporary string to get each integer
- } // end for loop that goes through each individual line of the input file
- } // end try
- catch (IOException e) {
- // if not found then throw exception
- System.out.println("Sorry Sir The File You Requested Has Not Been Found.");
- e.printStackTrace();
- } // end catch exception
- /*-------- END READ FILE ----------*/
- }
- public ImageMatrix clone() {
- // get the value of k from the ImageMatrix to copy
- int logDim = this.int_k;
- // create a new matrix with of all zeros
- ImageMatrix newMatrix = new ImageMatrix(logDim);
- // this doubly nested for loop will replace each element of the matrix
- // with the corresponding value from the original matrix
- for(int i = 0; i < (int)Math.pow(2,logDim) ; i++) {
- for(int j = 0; j < (int)Math.pow(2,logDim) ; j++) {
- newMatrix.bitArray[i][j] = this.bitArray[i][j];
- } // end for loop through 'j' (inside loop) values of 2d array
- } // end for loop through 'i' (outside loop) values of 2d array
- return newMatrix; // return the copy of the original array
- }
- // parameters must be independent
- public ImageMatrix stitch(ImageMatrix nw, ImageMatrix ne, ImageMatrix se, ImageMatrix sw){
- return null;
- }
- /// creates an image of size 2^logDim x 2^logDim
- /// of all 0's
- public ImageMatrix(int logDim) {
- // Initialize a new integer[][] array with 2^logDim by 2^logDim dimensions
- bitArray = new int[(int)Math.pow(2, logDim)][(int)Math.pow(2, logDim)];
- // loop through each element in the integer array and place a '0' inside
- for(int i = 0 ; i < (int)Math.pow(2, logDim) ; i++) {
- for(int j = 0 ; j < (int)Math.pow(2, logDim) ; j++) {
- bitArray[i][j] = 0; // place a '0' at that element in the array
- } // end for loop through the temporary string to get each integer
- } // end for loop that goes through each individual line of the input file
- } // end ImageMatrix(int logDim)
- /// returns -1 if i or j out of bounds
- public boolean setPixel(int i, int j, int bit) {
- // get the max value that i/j can be and store as type integer
- int ij_Max = (int)Math.pow(2, this.int_k);
- // Check to see both i, j passed in are less than the max
- if(i < ij_Max && j < ij_Max) {
- this.bitArray[j][i] = bit; // change the particular bit to user specification
- return true; // return true because i,j were in range
- } // end if i,j are in range
- else
- return false; // i,j were not in range
- }
- /// returns -1 if i or j out of bounds
- public int getPixel(int i, int j){
- // get the max value that i/j can be and store as type integer
- int ij_Max = (int)Math.pow(2, this.int_k);
- int temp; // used to store the value of the specified bit
- // Check to see both i, j passed in are less than the max
- if(i < ij_Max && j < ij_Max) {
- temp = this.bitArray[j][i]; // change the particular bit to user specification
- return temp; // return true because i,j were in range
- } // end if i,j are in range
- else
- return -1; // i,j were not in range
- }
- public void rotateRight(){
- // make a newMatrix for the rotation only to turn into 'this'
- int[][] newMatrix = new int[(int)Math.pow(2, this.int_k)][(int)Math.pow(2, this.int_k)];
- // for look through the bitArray int[][]
- for(int i = (int) Math.pow(2,this.int_k); i > 0 ; i--) {
- for(int j = 0 ; j < (int)Math.pow(2,this.int_k); j++) {
- // rotate the matrix by 90 degrees
- newMatrix[Math.abs(j-(int)Math.pow(2,this.int_k))][i] = this.bitArray[i][j];
- }
- }
- // update the bitArray to the newly rotated one
- this.bitArray = newMatrix;
- }
- public boolean andWith(Image otherImage){
- return false;
- }
- public boolean orWith(Image otherImage){
- return false;
- }
- public void mirrorX() {
- }
- public void mirrorY() {
- }
- /// writes ASCII file (matrix format) - required
- public boolean write(String outputFile) {
- return false;
- }
- }
- /***************** END: ImageMatrix.java **********/
Add Comment
Please, Sign In to add comment