Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By farhanz2009

Thread: Difficulty with 2-d array :(

  1. #1
    farhanz2009 is offline Member
    Join Date
    Feb 2012
    Posts
    14
    Rep Power
    0

    Default 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));
    }

    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default 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.
    sunde887 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    farhanz2009 is offline Member
    Join Date
    Feb 2012
    Posts
    14
    Rep Power
    0

    Default 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)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Difficulty with 2-d array :(

    Have you made the change I suggested?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    farhanz2009 is offline Member
    Join Date
    Feb 2012
    Posts
    14
    Rep Power
    0

    Default Re: Difficulty with 2-d array :(

    Yah i did it.. But still the same.

  6. #6
    farhanz2009 is offline Member
    Join Date
    Feb 2012
    Posts
    14
    Rep Power
    0

    Default 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.. :)
    sunde887 likes this.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Difficulty with 2-d array :(

    Well spotted.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Needless Difficulty with Streeam
    By waitingforzion in forum New To Java
    Replies: 4
    Last Post: 12-15-2011, 03:16 AM
  2. difficulty making java program, Help Me
    By 3c4hy0 in forum New To Java
    Replies: 11
    Last Post: 08-13-2010, 12:08 PM
  3. Applications in different difficulty
    By bubbless in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 12:31 AM
  4. difficulty
    By Daniela_v in forum New To Java
    Replies: 2
    Last Post: 03-04-2009, 05:36 PM
  5. Difficulty in finding the right algorithm
    By SolidCobra in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 11:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •