title: Making this code for image resize() work
body: <p>My instructor sent me this code, which is supposed to be an integral part of an ongoing project. Thing is, it doesn't work.</p>
  
  <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.awt.*;
  import java.awt.image.BufferedImage;
  import java.awt.image.Kernel;
  import java.awt.image.ConvolveOp;
  import com.sun.image.codec.jpeg.JPEGImageEncoder;
  import com.sun.image.codec.jpeg.JPEGCodec;
  import com.sun.image.codec.jpeg.JPEGEncodeParam;
  import javax.swing.*;
  
  
  public class ImageUtil {
  
      public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException{
  
          if(quality &lt;0||quality&gt;1){
              throw new IllegalArgumentException ("quality has to be between 0 and 1");
  
          }
  
          ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
          Image i = ii.getImage();
          Image resizedImage = null;
  
          int iWidth = i.getWidth (null);
          int iHeight = i.getHeight(null);
  
          if (iWidth &gt; iHeight){
              resizedImage = i.getScaledInstance(newWidth,(newWidth*iHeight)/iWidth, Image.SCALE_SMOOTH);
          } else {
              resizedImage = i.getScaledInstance((newWidth*iWidth)/iHeight,newWidth, Image.SCALE_SMOOTH);
          }
  
          Image temp = new ImageIcon (resizedImage).getImage();
  
          BufferedImage bufferedImage = new BufferedImage (temp.getWidth(null), temp.getHeight(null),
                                                           BufferedImage.TYPE_INT_RGB);
  
          // copy to a buffered image
          Graphics g = bufferedImage.createGraphics();
  
          g.setColor(Color.white);
          g.fillRect(0,0,temp.getWidth(null), temp.getHeight(null)); // causea el error de ejecución?
          g.drawImage(temp,0,0,null);
          g.dispose();
  
          float softenFactor = 0.05f;
          float [] softenArray = {0, softenFactor,0, softenFactor, 1-(softenFactor*4), softenFactor,0, softenFactor,0};
          Kernel kernel = new Kernel (3,3,softenArray);
          ConvolveOp cOp = new ConvolveOp (kernel, ConvolveOp.EDGE_NO_OP,null);
          bufferedImage = cOp.filter (bufferedImage,null);
  
          FileOutputStream out = new FileOutputStream(resizedFile);
  
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  
          JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
  
          param.setQuality(quality,true);
  
          encoder.setJPEGEncodeParam(param);
          encoder.encode(bufferedImage);
  
  
  
      }
  
      public static void main(String args[]) throws IOException {
         File originalImage= new File ("/images/goya.jpg");
         resize(originalImage,new File("/images/goyanuevotamanio.jpg"),350, 0.2f);
         resize(originalImage, new File("/images/goyanuevotamanio.jpg"), 350, 1f);
  
      }
  
  }
  </code></pre>
  
  <p>He says the code should be able to resize both height and width yet his resize method:</p>
  
  <pre><code>public static void resize(File originalFile, File resizedFile, int newWidth, float quality)
  </code></pre>
  
  <p>lacks a <code>newHeight</code> parameter. </p>
  
  <p>The RuntimeException that's thrown says: </p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: **Width (-1) and height (-1) cannot be &lt;= 0**
          at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
          at java.awt.image.BufferedImage.&lt;init&gt;(BufferedImage.java:312)
          at ImageUtil.resize(ImageUtil.java:38)
          at ImageUtil.main(ImageUtil.java:72)
  Java Result: 1
  </code></pre>
  
  <p>How can I change this?</p>