Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 175471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:42:18+00:00 2026-05-11T13:42:18+00:00

My instructor sent me this code, which is supposed to be an integral part

  • 0

My instructor sent me this code, which is supposed to be an integral part of an ongoing project. Thing is, it doesn’t work.

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 <0||quality>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 > 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);      }  } 

He says the code should be able to resize both height and width yet his resize method:

public static void resize(File originalFile, File resizedFile, int newWidth, float quality) 

lacks a newHeight parameter.

The RuntimeException that’s thrown says:

Exception in thread 'main' java.lang.IllegalArgumentException: **Width (-1) and height (-1) cannot be <= 0**         at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)         at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)         at ImageUtil.resize(ImageUtil.java:38)         at ImageUtil.main(ImageUtil.java:72) Java Result: 1 

How can I change this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:42:19+00:00Added an answer on May 11, 2026 at 1:42 pm

    Without analyzing the code to see what the problem might be, I can say that if the intention is to keep the images aspect ratio, a new height parameter is not needed. It will always be a fixed ratio of the width.

    UPDATE:

    I’ve run the code against my own test image and it does indeed work as expected. The error you are getting is most likely caused when the program cannot find the source image you specify. I get the same error when I specify a source image that doesn’t exist.

    Try using an absolute path (including drive letter etc.) for your source image.

    UPDATE 2

    Add the following code after the line..

    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());  if(ii.getImageLoadStatus() == MediaTracker.COMPLETE) {     System.out.print('Load image ok\n'); } else {     System.out.print('Load image failed\n'); } 

    This will tell you if the program is failing to load the source image.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code: Entry.h #import <Foundation/Foundation.h> @interface Entry : NSObject { id object;
I am working on a school project which involves managing driving instructor calendars. The
This is the driver code provided by my instructor, It isn't meant to be
I am dealing with an old code designed for iPhone OS 2.0. In this
I have this code: CardView *aCardView = [self prendiCartaDalMazzo]; [aCardView removeFromSuperview]; [self.mieCarte addSubview:aCardView]; when
My instructor gave us example code that is very similiar to the code below.
My instructor said the way to start this is to use the getline() function
Our Java instructor always asks us to prove that Every java program is Object
hi i'm watching a c++ tutorial and instructor includes the libraries like this #
I'm trying to determine which is the right way to code a DAO for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.