I'm having issues loading images into my program. I have tried to research and trouble shoot as much as I can but I'm at a dead end. I know that my code for loading the image is close but there is some strange arrayoutofboundsexception that is running when loading the file. The only way to do this is to get each pixel from the ppm text file and draw pixel by pixel. I have the loading class below. The file type has to stay the same though because I have to read it and change the pixels by clicking buttons, but i need to figure out this reason and if it will hinder the ability to save images too.
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileClass {
public int width;
public int height;
public int[] loadData(String filename) throws FileNotFoundException {
File foo = new File(filename);
Scanner scan = new Scanner(foo);
scan.nextLine();
scan.nextLine();
width = scan.nextInt();
height = scan.nextInt();
System.out.print(height);
scan.nextLine();
int[] array = new int [width * height];
int i = 0;
while (scan.hasNextInt()) {
array[i] = scan.nextInt();
i++;
System.out.print("yes");
}
scan.close();
return array;
}
public BufferedImage makeImage(int[] array){
BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
int z = 0;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int pixelColor = new Color(array[z], array[z+1], array[z+2]).getRGB();
image.setRGB(y, x, pixelColor);
z+=3;
}
}
return image;
}
}