The document discusses a Java program that encounters an ArrayIndexOutOfBoundsException due to accessing pixel indices outside of allowed ranges. It explains how to check index values to ensure they remain within valid bounds when manipulating a two-dimensional array of pixels. Additionally, it introduces a simple blur method that averages pixel colors based on a specified number of surrounding pixels.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views2 pages
Scanned Document 4
The document discusses a Java program that encounters an ArrayIndexOutOfBoundsException due to accessing pixel indices outside of allowed ranges. It explains how to check index values to ensure they remain within valid bounds when manipulating a two-dimensional array of pixels. Additionally, it introduces a simple blur method that averages pixel colors based on a specified number of surrounding pixels.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
> gysten.out primein(p.geewtaenO):
3
>
150
2 p.getPixel (330,160)
java. lang. ArrayIndexOu
bounds!
at sun.awt. image. Byteln
‘Unknown Source)
‘as java.awt. nage Buf FeredTnage. getRC8 (Unknow pay
at fava. antes gettasicPinel SIRP)EPCEUE-Java20)
at Pixel.setVa bef eAndLocation (Pixel. java:13,.
re einite (Pixel javai57)_ , >
zt Piaerepicture. i TePicture. java:270)
at sun. reflect Nativeve
Flect.NativeMet!
at sun.re e
at sun. reflect-Delegating
Unknown Source) :
at java. 1ang- reflect Method. i
wt printin(p.getHeight());
29
gysten-o
tofBoundsException:
Coordinate oy,
terleavedRaster..2et0atar iene
nts
nvoke(unknown Source)
‘The java. Tang.Arrayne weofBoundsExcept ion tells us that we trig,
‘an array element that ‘was outside the allowed indices. If this happens a
program is executing the program will stop and report the exception. This is pe <
repetime exception, because it 8 aN Or that happens when the program je a
rather than at compile time- am is iy
‘So how do we check that the index values are acceptable? We know ta
indices range from 0 to width ~ 1 and the y indices range from 0 to height — Son
can use
idex0
x= 0 Mix < this.getwidthO && y >= 0 && y < this.getHeishtg)
tobe sure that we stay within te indices ofthe two-dimensional array.
Program 12.10 defines a simple blur() method The blur() method his
parameter that allows you to pass in the number of pixels (numPixels) to be a
then each pixel will be assigned ace
compute the average. If you pass in
computed asthe average ofthe colors of the current pixel, the two pixels oj
fet pls the 2 pixels tots right, assuming this is possible within the borders of i
«two-dimensional array.
Program 12.10: A Simple Blur
’ Method to blur the pixels
{Saran nunPixels the munber of pixels to average in a1?
{ iecetons so 1f the nuePixels is 2 then we will average
, 5 ‘angle defi
{ furrent pixel to 2 after the carrenipiteliee isBring 369
lic void blurCint NUMPAXe1 9)
pu
: ixe) = mutt;
Pi tush aay
Ant redvalue = 0;
int oreenValue =
int blueValue =
int count = 0;
// loop through the pixels
for Cint x=0; x < {4s -getwidtho; XH) ¢
for Cint yao; y this .gettei gi
current Pixel
this.getPixey
// get the
pixel =
/* loop through
i Pixe) numpy
i fumPixels afters
a
For Cint xsample = _ NUnPixe) 5,
XSample