Guest User

Untitled

a guest
Jan 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. /***************** FILE: ImageMatrix.java **********/
  8.  
  9. public class ImageMatrix extends Image
  10. {
  11.  
  12. // Your data members here
  13. private int[][] bitArray;
  14. private int int_k;
  15.  
  16. public ImageMatrix(String inputFile) {
  17.  
  18. /*--------- READ A FILE ------------*/
  19. Scanner inputScanner = null; // initialize the scanner
  20.  
  21. // try to read the file, if an error occurs throw exception and tell the user
  22. try {
  23. // create a new scanner for the specified input file
  24. inputScanner = new Scanner(new BufferedReader(new FileReader(inputFile)));
  25. int_k = Integer.parseInt(inputScanner.nextLine());
  26.  
  27. // Initialize the bitArray[][] used for storing the input file
  28. bitArray = new int[(int)Math.pow(2, int_k)][(int)Math.pow(2, int_k)];
  29.  
  30. // Extraction of the the matrix from the specified file
  31. for(int i = 0 ; i < (int)Math.pow(2, int_k) ; i++) {
  32. String temp = inputScanner.nextLine();
  33. for(int j = 0 ; j < temp.length() ; j++) {
  34. char tempInt = temp.charAt(j);
  35. bitArray[i][j] = (int)tempInt;
  36.  
  37. } // end for loop through the temporary string to get each integer
  38. } // end for loop that goes through each individual line of the input file
  39. } // end try
  40. catch (IOException e) {
  41. // if not found then throw exception
  42. System.out.println("Sorry Sir The File You Requested Has Not Been Found.");
  43. e.printStackTrace();
  44. } // end catch exception
  45.  
  46. /*-------- END READ FILE ----------*/
  47.  
  48. }
  49.  
  50. public ImageMatrix clone() {
  51.  
  52. // get the value of k from the ImageMatrix to copy
  53. int logDim = this.int_k;
  54.  
  55. // create a new matrix with of all zeros
  56. ImageMatrix newMatrix = new ImageMatrix(logDim);
  57.  
  58. // this doubly nested for loop will replace each element of the matrix
  59. // with the corresponding value from the original matrix
  60. for(int i = 0; i < (int)Math.pow(2,logDim) ; i++) {
  61. for(int j = 0; j < (int)Math.pow(2,logDim) ; j++) {
  62. newMatrix.bitArray[i][j] = this.bitArray[i][j];
  63. } // end for loop through 'j' (inside loop) values of 2d array
  64. } // end for loop through 'i' (outside loop) values of 2d array
  65.  
  66. return newMatrix; // return the copy of the original array
  67. }
  68.  
  69. // parameters must be independent
  70. public ImageMatrix stitch(ImageMatrix nw, ImageMatrix ne, ImageMatrix se, ImageMatrix sw){
  71. return null;
  72. }
  73.  
  74. /// creates an image of size 2^logDim x 2^logDim
  75. /// of all 0's
  76. public ImageMatrix(int logDim) {
  77.  
  78. // Initialize a new integer[][] array with 2^logDim by 2^logDim dimensions
  79. bitArray = new int[(int)Math.pow(2, logDim)][(int)Math.pow(2, logDim)];
  80.  
  81. // loop through each element in the integer array and place a '0' inside
  82. for(int i = 0 ; i < (int)Math.pow(2, logDim) ; i++) {
  83. for(int j = 0 ; j < (int)Math.pow(2, logDim) ; j++) {
  84. bitArray[i][j] = 0; // place a '0' at that element in the array
  85. } // end for loop through the temporary string to get each integer
  86. } // end for loop that goes through each individual line of the input file
  87. } // end ImageMatrix(int logDim)
  88.  
  89. /// returns -1 if i or j out of bounds
  90. public boolean setPixel(int i, int j, int bit) {
  91. // get the max value that i/j can be and store as type integer
  92. int ij_Max = (int)Math.pow(2, this.int_k);
  93.  
  94. // Check to see both i, j passed in are less than the max
  95. if(i < ij_Max && j < ij_Max) {
  96. this.bitArray[j][i] = bit; // change the particular bit to user specification
  97. return true; // return true because i,j were in range
  98. } // end if i,j are in range
  99. else
  100. return false; // i,j were not in range
  101. }
  102. /// returns -1 if i or j out of bounds
  103. public int getPixel(int i, int j){
  104. // get the max value that i/j can be and store as type integer
  105. int ij_Max = (int)Math.pow(2, this.int_k);
  106. int temp; // used to store the value of the specified bit
  107.  
  108. // Check to see both i, j passed in are less than the max
  109. if(i < ij_Max && j < ij_Max) {
  110. temp = this.bitArray[j][i]; // change the particular bit to user specification
  111. return temp; // return true because i,j were in range
  112. } // end if i,j are in range
  113. else
  114. return -1; // i,j were not in range
  115. }
  116.  
  117. public void rotateRight(){
  118.  
  119. // make a newMatrix for the rotation only to turn into 'this'
  120. int[][] newMatrix = new int[(int)Math.pow(2, this.int_k)][(int)Math.pow(2, this.int_k)];
  121.  
  122. // for look through the bitArray int[][]
  123. for(int i = (int) Math.pow(2,this.int_k); i > 0 ; i--) {
  124. for(int j = 0 ; j < (int)Math.pow(2,this.int_k); j++) {
  125. // rotate the matrix by 90 degrees
  126. newMatrix[Math.abs(j-(int)Math.pow(2,this.int_k))][i] = this.bitArray[i][j];
  127. }
  128. }
  129. // update the bitArray to the newly rotated one
  130. this.bitArray = newMatrix;
  131. }
  132.  
  133. public boolean andWith(Image otherImage){
  134.  
  135.  
  136.  
  137. return false;
  138. }
  139. public boolean orWith(Image otherImage){
  140. return false;
  141. }
  142.  
  143. public void mirrorX() {
  144. }
  145.  
  146. public void mirrorY() {
  147. }
  148.  
  149. /// writes ASCII file (matrix format) - required
  150. public boolean write(String outputFile) {
  151. return false;
  152. }
  153. }
  154. /***************** END: ImageMatrix.java **********/
Add Comment
Please, Sign In to add comment