I need to be able to choose a .txt file filled with RGB numbers for each pixel, and then create the image using the RGB numbers. I have everything figured out, except I don't know how to create a loop to actually read and set the pixels of the new picture. It's frustrating because I know that it should't be this hard. here is my code so far...
public static Picture convertRGBPic()
{
// set up file for reading
String fileName = FileChooser.pickAFile();
File file = new File(fileName);
// declare an empty picture file
Picture pic = new Picture();
try {
Scanner pictRGB = new Scanner(file);
int width = pictRGB.nextInt();
int height = pictRGB.nextInt();
// nested loops to read RGB numbers (no alpha) and set each pixel of the pic below
for (int row = 0; row < pic.getHeight(); row++)
{
for (int col = 0; col < pic.getWidth(); col++)
{
Pixel fromPix = pic.getPixel(col , row );
Pixel toPix = pic.getPixel(col,row);
toPix.setRed(fromPix.getRed());
toPix.setGreen(fromPix.getGreen());
toPix.setBlue(fromPix.getBlue());
}
}
// end your nested loop
pictRGB.close(); //close file after reading
} // end of try and begin catch error if file cannot be read
catch (FileNotFoundException e)
{
e.printStackTrace();
}//end catch
return pic;
} // end of method
Aucun commentaire:
Enregistrer un commentaire