Results 1 to 4 of 4
Thread: Java - Error in terminal window
- 04-01-2011, 05:18 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Java - Error in terminal window
hi, i keep getting an error in terminal window after i compile, and i don't know how to fix it. can someone please help?
the error is:
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)
Java 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 ""; } }
-
You've answered your question yourself...
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.
Without reading your code i'm going to point out this: if you are calculating the array index by some form of calculation which can give you +-number, use Math.abs(number) so that the number is always positive. But if that's not the case, then make sure you have your for-loops set up correctly.
- 04-01-2011, 06:37 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
ehhh i found one error. there's 2 errors left apparently?
i changed
for(int j = 0; j < width; i++)
to
for(int j = 0; j <width; j++)
and now I have the error
java.lang.ArrayIndexOutOfBoundsException:
In file WatermarkImages.java on line 119, which reads:
}combine[i][j]=markedPixel;
It seems that the code tried to use an illegal value as an index to an
object. The code was trying to access an element at index 288 of an
array (or other object) on that line. The size of the array may be
less than 288. Keep in mind that if the array size is N, the biggest
index you can access is N-1.
This error is called an ArrayIndexOutOfBoundsException.
at WatermarkImages.watermark(WatermarkImages.java:119 )
at WatermarkImages.init(WatermarkImages.java:41)
at acm.program.Program.start(Program.java:803)
at acm.program.Program.start(Program.java:1279)
-
Okay either this is really freaky or your compiler software is really good. Your answer is right there again.
It seems that the code tried to use an illegal value as an index to an
object. The code was trying to access an element at index 288 of an
array (or other object) on that line. The size of the array may be
less than 288. Keep in mind that if the array size is N, the biggest
index you can access is N-1.
Similar Threads
-
Mac Terminal
By Java8 in forum Other IDEsReplies: 0Last Post: 01-16-2010, 06:19 PM -
ArrayLists do not print to the terminal window, why? long code inside
By caps_lock in forum New To JavaReplies: 5Last Post: 05-25-2009, 09:03 PM -
Error message in executing a simple program from DOS window
By betelgeuse in forum New To JavaReplies: 6Last Post: 02-24-2009, 02:50 PM -
Terminal
By dj kourampies in forum New To JavaReplies: 7Last Post: 01-07-2009, 03:57 PM -
Error logged from JDT Debug UI Window
By tsc in forum EclipseReplies: 1Last Post: 07-10-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks