Results 1 to 7 of 7
Thread: Difficulty with 2-d array :(
- 03-21-2012, 03:58 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
Difficulty with 2-d array :(
What i'm trying to do is grab each pixel of an image and store it in a 2-d array but when do some looping it always say out of index. My image is 240 in height and 364 in width. but my top loop only gives a number 0 and the second one works perfect and goes to its limits. For my code to work perfectly i need my top loop to go to 364 and both loops should work a total of 240*364 times. Hope you got my point.
Here's my code.
int wid = img.getWidth();
int hei = img.getHeight();
int[][] pixels = new int[hei][wid];
for(int i = 0; i<=wid; i++)
{
for(int j = 0;j<=hei;j++)
{
pixels[i][j] = img.getRGB(i,j);
int red = (((pixels[i][j] >> 16) & 0xff));
int green = (((pixels[i][j] >> 8) & 0xff));
int blue = ((pixels[i][j] & 0xff));
}
}
- 03-21-2012, 04:24 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Difficulty with 2-d array :(
i<=wid
and
j<=hei
Both of those should be '<'.
An array index goes up to array.length - 1.
Note that when posting code here you should use [code] tags [/code].
Also if your question involves and exception or error you should post the full error and stack trace, and point out the line on which it occurs in your code.Please do not ask for code as refusal often offends.
- 03-21-2012, 04:59 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
Re: Difficulty with 2-d array :(
still on this line ===> pixels[i][j] = img.getRGB(i,j);
I'm getting this error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 240
at camera.JMFtest$4.actionPerformed(JMFtest.java:131)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6505)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:627 0)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713 )
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
- 03-21-2012, 05:14 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Difficulty with 2-d array :(
Have you made the change I suggested?
Please do not ask for code as refusal often offends.
- 03-21-2012, 05:15 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
Re: Difficulty with 2-d array :(
Yah i did it.. But still the same.
- 03-21-2012, 05:36 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
Re: Difficulty with 2-d array :(
I got it sorted out actually here ===> int[][] pixels = new int[hei][wid];
it should have been like this int[][] pixels = new int[wid][hei];
Thanks.. :)
- 03-21-2012, 06:10 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Needless Difficulty with Streeam
By waitingforzion in forum New To JavaReplies: 4Last Post: 12-15-2011, 03:16 AM -
difficulty making java program, Help Me
By 3c4hy0 in forum New To JavaReplies: 11Last Post: 08-13-2010, 12:08 PM -
Applications in different difficulty
By bubbless in forum New To JavaReplies: 2Last Post: 03-11-2009, 12:31 AM -
difficulty
By Daniela_v in forum New To JavaReplies: 2Last Post: 03-04-2009, 05:36 PM -
Difficulty in finding the right algorithm
By SolidCobra in forum New To JavaReplies: 3Last Post: 10-05-2008, 11:55 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks