0

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;
    }
}
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
  • 1) Add a stacktrace. 2) If baboon.ppm is the image you are trying to load, it is probably not supported by standard java. – Just another Java programmer Feb 13 '17 at 19:50
  • there is a stacktrace, i don't know how else to load this file – Alex Nedvidek Feb 13 '17 at 19:55
  • I can't see any stack trace. Only some code. You are refering to a NullPointerException., so you should have a stack trace. – Just another Java programmer Feb 13 '17 at 19:59
  • `The file type has to stay the same though` - did you try your code with a standard .jpg or .gif file? I would guess .ppm is not supported. – camickr Feb 13 '17 at 20:17
  • The problem is `int[] array = new int [width * height]` it should be `new int [width * height * 3]` to hold 3 values (R, G and B) for each pixel. You can see this form the double `for`-loop in your `makeImage` method too, where you increase the `z` index by 3 for each pixel. – Harald K Feb 14 '17 at 12:05
  • PS: Is this an assignment where you need to implement the reading yourself, or do you just need to read a PPM file? – Harald K Feb 14 '17 at 12:09

0 Answers0