Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can someone explain how to fix this error?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone explain how to fix this error?

    Here is the assignment:

    A digitized image is an m X m matrix of pixels. In a binary image each pixel is either 0 or 1. A 0 pixel represents image background, while a 1 represents a point on the image component. We will refer to pixels whose value is 1 as component pixels. Two pixels are adjacent if one is to the left, above, right or below the other. Two component pixels that are adjacent are pixels of the same image component. The objective of component labeling is to label the component pixels so that two pixels get the same label if and only if they are pixels of the same image component.

    Consider Figure 1(a) that shows a 7 X 7 image. The blank squares represent background pixels, and the 1s represent component pixels. Pixels (1,3) and (2,3) are pixels of the same component because they are adjacent. Since component pixels (2,3) and (2,4) are adjacent, they are also from the same component. Hence the three pixels (1,3), (2,3), and (2,4) are from the same component. Since no other image pixels are adjacent to these three pixels, these three define an image component. The image of figure 1(a) has four components. The first component is defined by the pixel set (1,3), (2,3), (2,4); the second is (3,5), (4,4), (4,5), (5,5); the third is (5,2), (6,1), (6,2), (6,3), (7,1), (7,2), (7,3); and the fourth is (5,7), (6,7), (7,6), (7,7). In Figure 1(b) the component pixels have been given labels so that two pixels have the same label if and only if they are part of the same component. We use the numbers 2,3,4,… as component identifiers; there is no component numbered 1 because 1 designates an unlabeled component pixel.
    Here is my code so far:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pixels;
     
    import java.util.LinkedList;
    import java.util.Queue;
     
    /**
     *
     * @author Jared
     */
    public class Pixels {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //This is the 2D array that will be the input to the function...
            int[][] grid = new int[][]{
                { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                { 0, 0, 0, 1, 0, 0, 0, 0, 0},
                { 0, 0, 0, 1, 1, 0, 0, 0, 0},
                { 0, 0, 0, 0, 1, 0, 0, 0, 0},
                { 0, 0, 0, 0, 0, 0, 1, 1, 0},
                { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                { 0, 0, 0, 0, 0, 0, 0, 0, 0}
              };
     
            solver(grid);
     
            //System.out.println(grid[1][2]);
        }
     
        public static void solver(int[][] temp){
     
            //Queues NOTE: will need to make more...
            Queue<point> Queue = new LinkedList<point>();
     
     
            //Running count of number of groups
            int count = 2;
     
            //Iterating through the grid
            for (int x[] : temp){
                int xAxis = 0;
                for (int y : x){
                    if (y == 1){
     
                        //Creates an object of the point
                        point newPoint = new point(xAxis, y);
     
                        //Adds point to the queue
                        Queue.add(newPoint);
     
                        //Changes its value
                        y = count;
     
                        //Checks neighbors
                        while (!Queue.isEmpty()){
                            int tempX = Queue.peek().getX();
                            int tempY = Queue.peek().getY();
                            Queue.remove();
     
                            //Check neighbor up
                            if (temp[tempX + 1][tempY] == 1){
                                point up = new point(tempX + 1, tempY);
                                Queue.add(up);
                                temp[tempX + 1][tempY] = count;
                            }
                            //Check neighbor down
                            if(temp[tempX - 1][tempY] == 1){
                                point down = new point(tempX - 1, tempY);
                                Queue.add(down);
                                temp[tempX - 1][tempY] = count;
                            }
                            //Check neighbor right
                            if(temp[tempX][tempY + 1] == 1){
                                point right = new point(tempX, tempY + 1);
                                Queue.add(right);
                                temp[tempX][tempY + 1] = count;
                            }
                            //Check neighbor left
                            if(temp[tempX][tempY - 1] == 1){
                                point left = new point(tempX, tempY - 1);
                                Queue.add(left);
                                temp[tempX][tempY - 1] = count;
                            }
     
     
                        }
     
                        //Changes group number
                        count++;
                    }
                }
            }
     
            //Outputs the matrix
            System.out.println(temp);
     
        } 
    }


    Here is the code for the point class:

    /*
     * This class will define the point objects that will represent the individual pixels and be stored in the queue.
     */
    package pixels;
     
    /**
     *
     * @author Jared
     */
    public class point {
     
     
        //variables
        public int x, y;
     
        //constructor  for point object
        public point(int x, int y){
            //x-axis
            this.x = x;
     
            //y-axis
            this.y = y;
        }
     
        //Getters
     
        public int getX(){
            return x;
        }
     
        public int getY(){
            return y;
        }
     
        //Setters
     
        public void setX(int x){
            this.x = x;
        }
     
        public void setY(int y){
            this.y = y;
        }
     
     
    }


    Here is the error that is occurring:

    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at pixels.Pixels.solver(Pixels.java:76)
    at pixels.Pixels.main(Pixels.java:34)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone explain how to fix this error?

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at pixels.Pixels.solver(Pixels.java:76)
    Which is line 76 in the solver() method, and why is there an array index of -1 at that line? Determine the variable being used as an array index that is -1, and trace the code backwards to determine why that is happening and fix it.

Similar Threads

  1. [SOLVED] A very peculiar error I just cant fix
    By Cornix in forum AWT / Java Swing
    Replies: 0
    Last Post: August 28th, 2014, 01:42 PM
  2. [SOLVED] Learning OOP; Gettin error messages i cant explain.
    By E.K.Virtanen in forum Object Oriented Programming
    Replies: 2
    Last Post: March 20th, 2013, 11:47 AM
  3. Please explain why I get this error...
    By mrjavajava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2013, 09:58 AM
  4. can anyone help me to fix the error in the program???
    By divi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2013, 01:36 AM
  5. Cant fix this error... Help!
    By Hypermegazord in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2012, 01:06 PM