Hi, I get an error in the terminal window AFTER I compile everything, and it gives me an explanation, but I don't know how to fix it. Could someone please help? Code: import acm.graphics.*; import acm.program.*; import java.awt.event.*; import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; // ------------------------------------------------------------------------- /** * * @author * @version Spring, 2011 CS 1054 Intro to Java */ public class WatermarkImages extends StudentTestableGraphicsProgram { //~ Instance/static variables ............................................. public static final int APPLICATION_WIDTH = 1024; public static final int APPLICATION_HEIGHT = 600; public static final int TOLERANCE = 5; public static final int RGB_MAX = 255; GImage pic; GImage mark; //~ Methods ............................................................... // ---------------------------------------------------------- /** * */ public void init() { addMouseListeners(); String imgFile = chooseImage(); pic = new GImage(imgFile); imgFile = chooseImage(); mark = new GImage(imgFile); GImage marked = watermark(pic,mark); add(pic,0,0); } /** * */ public void run() { /*# add your statements here */ } /** * Manipulates an image to show anything non-white and non-black in a watermark image transposed on it * * @param img is the original image to be manipulated * @param water is the watermark to transpose onto img * @return a GImage that is the size of the smallest width and smallest height between the two images, * with a watermark transposed onto the original */ public GImage watermark(GImage img, GImage water) { int[][] imgArray = img.getPixelArray(); int[][] waterArray = water.getPixelArray(); int[][] combine; int width; int height; //find the smallest height... if(imgArray.length < waterArray.length) { height = imgArray.length; } else { height = waterArray.length; } //find the smallest width... if(imgArray[0].length < waterArray[0].length) { width = imgArray[0].length; } else { width = waterArray[0].length; } //create new 2D array the size of the smallest dimensions of both images... combine = new int[width][height]; //traverse through 2D arrays... for(int i = 0; i < height; i++) { for(int j = 0; j < width; i++) { int pixel = imgArray[i][j]; int red = GImage.getRed(pixel); int green = GImage.getGreen(pixel); int blue = GImage.getBlue(pixel); int maskPixel = waterArray[i][j]; int maskR = GImage.getRed(maskPixel); int maskG = GImage.getGreen(maskPixel); int maskB = GImage.getBlue(maskPixel); int markedPixel; if( (maskR<TOLERANCE && maskG<TOLERANCE && maskB<TOLERANCE) //black || (maskR>(RGB_MAX-TOLERANCE) && maskG>(RGB_MAX-TOLERANCE) &&maskB>(RGB_MAX-TOLERANCE)) ) //or white { markedPixel = pixel; } else { //transpose watermark shape onto original image... int avg = (maskR + maskG + maskB)/3; int newRed = (red+avg)/2; int newGreen = (green+avg)/2; int newBlue = (blue+avg)/2; markedPixel = GImage.createRGBPixel(newRed,newGreen,newBlue); } combine[i][j] = markedPixel; } } return new GImage(combine); } /** * [Author: Kevin Buffardi] Saves a GImage to disk as a png file * @param picture A GImage picture that will be converted to png format and saved on disk * @param filename The name of the file to save (path optional, default to project folder. e.g. "output.png") */ public void saveImage(GImage picture, String filename) { try{ File name = new File(filename); Image img = picture.getImage(); BufferedImage buff = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB); Graphics graph = buff.getGraphics(); graph.drawImage(img, 0, 0, null); graph.dispose(); ImageIO.write(buff,"png",name); } catch(IOException e){ System.out.println("Error when writing file: " + e.toString()); } } /** * [Author: Kevin Buffardi] Shows a file browser dialog so the user can choose a file. * Only image files *should* be chosen, but this method does not prevent other types of files to be chosen. * @return A string representing the path and filename chosen (e.g. "C:\My Documents\image.jpg") */ public String chooseImage() { JFileChooser dialog = new JFileChooser(); int result = dialog.showOpenDialog(this); if(result == JFileChooser.APPROVE_OPTION) { return dialog.getSelectedFile().getPath(); } else return ""; } }
the error is: java.lang.NegativeArraySizeException: In file MediaTools.java on line 329. It appears that the code is trying to create an array with a negative size. Remember, the array must have a size that is a positive number. This error is called a NegativeArraySizeException. at acm.util.MediaTools.getPixelArray(MediaTools.java:329) at acm.graphics.GImage.getPixelArray(GImage.java:336) at WatermarkImages.watermark(WatermarkImages.java:63) at WatermarkImages.init(WatermarkImages.java:41) at acm.program.Program.start(Program.java:803) at acm.program.Program.start(Program.java:1279) java.lang.NegativeArraySizeException: In file MediaTools.java on line 329. It appears that the code is trying to create an array with a negative size. Remember, the array must have a size that is a positive number. This error is called a NegativeArraySizeException. at acm.util.MediaTools.getPixelArray(MediaTools.java:329) at acm.graphics.GImage.getPixelArray(GImage.java:336) at WatermarkImages.watermark(WatermarkImages.java:62) at WatermarkImages.init(WatermarkImages.java:41) at acm.program.Program.start(Program.java:803) at acm.program.Program.start(Program.java:1279)