To save files after changing the name using Java - with methods from edu.duke library

173 Views Asked by At

I have recently started learning how to code in Java from coursera.org. But I am unable to progress further as I am not able to figure out my code doesnt work.

This code changes the name and saves the file with the new name.

public class Copyfile {

public void doSave()
{
    DirectoryResource dr = new DirectoryResource();
    for(File f : dr.selectedFiles())
    {
        ImageResource ir = new ImageResource(f);
        String name = ir.getFileName();
        ImageResource r = new ImageResource(ir);
        String x = "copy-"+name;
        r.setFileName(x);
        r.save();
        System.out.println(name);
    }
}

}

Where as, this code doesnt.Although it is very similar.

public class GREyscale {
public ImageResource convertImage(ImageResource inimage)
{
  ImageResource outimage = new ImageResource(inimage.getWidth(), inimage.getHeight());
  for(Pixel pixel : outimage.pixels())
  {
      Pixel inPixel = inimage.getPixel(pixel.getX(),pixel.getY());
      int val =(inPixel.getRed() + inPixel.getBlue() + inPixel.getGreen())/3;
      pixel.setRed(val);
      pixel.setBlue(val);
      pixel.setGreen(val);
    }

    return outimage;
}

public void convert()
{
  DirectoryResource dr = new DirectoryResource();
  for(File f : dr.selectedFiles())
  {
      ImageResource ir = new ImageResource(f);
      ImageResource r = convertImage(ir);
      String name = ir.getFileName();
      String x ="grey-"+name;
      r.setFileName(x);
      r.save();
      System.out.println(r.getFileName());
      r.draw();
    }
}

And I am unable to find out why , even after trying every possible alteration.

0

There are 0 best solutions below