Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 24th, 2013, 12:52 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
ArrayIndexOutOfBounds error

This program takes as input an image, scans it and highlights circles in red. However I get an ArrayOutOfBounds exception at the last method when I call drawCircle(). I dont know how to fix it.

Code:
public class Assig1 {

    public static void main(String[] args) {
        try {
            // arg 0 is the input image name
            BufferedImage img = ImageIO.read(new File(args[0]));

            // arg 1 is the min radius
            int minr = Integer.parseInt(args[1]);
            // arg 2 is the max radius
            int maxr = Integer.parseInt(args[2]);

            // if present, arg 3 is the max width we consider
            int w = (args.length>3) ? Integer.parseInt(args[3]) : img.getWidth();
            // if present, arg 4 is the max height we consider
            int h = (args.length>4) ? Integer.parseInt(args[4]) : img.getHeight();

            
            findCircle(minr,img, w, h);
            
            //----
            // write out the image
            File outputfile = new File("outputimage1.png");
            ImageIO.write(img, "png", outputfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Bresenham's algorithm to draw a circle
    // requires circle center and radius, as well as the
    // image and Graphics2D object with drawing colour already set.
    static void drawCircle(int cx,int cy,int r,BufferedImage img,Graphics2D g) {
        int f = 1-r;
        int ddF_x = 1;
        int ddF_y = -2 * r;
        int x = 0;
        int y = r;

        // draw cardinal points
        g.drawLine(cx,cy+r,cx,cy+r);
        g.drawLine(cx,cy-r,cx,cy-r);
        g.drawLine(cx+r,cy,cx+r,cy);
        g.drawLine(cx-r,cy,cx-r,cy);

        // draw 1/8 of the circle, taking advantage of symmetry
        while(x < y) {
            if(f >= 0) {
                y--;
                ddF_y += 2;
                f += ddF_y;
            }
            x++;
            ddF_x += 2;
            f += ddF_x;

            g.drawLine(cx+x,cy+y,cx+x,cy+y);
            g.drawLine(cx-x,cy+y,cx-x,cy+y);
            g.drawLine(cx+x,cy-y,cx+x,cy-y);
            g.drawLine(cx-x,cy-y,cx-x,cy-y);
            g.drawLine(cx+y,cy+x,cx+y,cy+x);
            g.drawLine(cx-y,cy+x,cx-y,cy+x);
            g.drawLine(cx+y,cy-x,cx+y,cy-x);
            g.drawLine(cx-y,cy-x,cx-y,cy-x);
        }
    
}

    static void findCircle(int r,BufferedImage img, int w, int h) {

    	//getting all the pixels from an image
    	int[][] pixels = new int[w][h];

    	for( int i = 0; i < w; i++ ){
      	 	 for( int j = 0; j < h; j++ ){
         		   pixels[i][j] = img.getRGB( i, j );
      	 	 }
    	}
    	
    	 // graphical output
        Graphics2D g2 = img.createGraphics();
        // use red
        g2.setColor(Color.RED);
    	
    	
    	for (int i1=0; i1<pixels.length; i1++) {
    	     for (int j1=0; j1<pixels[i1].length; j1++) {
    	         
    	    	 if(pixels[i1][j1] != pixels[i1+r][j1]
    	     			|| pixels[i1][j1] != pixels[i1-r][j1]
    	     			|| pixels[i1][j1] != pixels[i1][j1+r]
    	     			|| pixels[i1][j1] != pixels[i1][j1-r]){
    	     		drawCircle(i1,j1,r,img,g2);
    	     	}
    	     }
    	  
    	 }
    }
}

Reply With Quote
  #2  
Old January 24th, 2013, 01:01 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Quote:
I get an ArrayOutOfBounds exception

Please copy the full text of the error message and post it here. It shows where the exception happened and the value of the index.

Remember array indexes range from 0 to the array length-1

Reply With Quote
  #3  
Old January 24th, 2013, 01:06 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
ArrayIndexOutOfBoundsException : -3
at Assig1.findCircle<Assig1.java:112>
at Assig1.main<Assig1.java:42>

Reply With Quote
  #4  
Old January 24th, 2013, 01:19 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Look at the values of all the indexes used on line 112 and see if any of them could be -3.

If you can not tell by looking, add a println statement that prints out the values of all the indexes used on that line to see which one has the value of -3.

Reply With Quote
  #5  
Old January 24th, 2013, 01:56 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
Updated

in the findCircle method I put i1 and j1 to start at r+1. (ie so that the image starts searching at after a given radius and not before that) but I still get same error this time instead of 3 it is 321

Code:
 static void findCircle(int r,BufferedImage img, int w, int h) {

    	long timeBefore = System.currentTimeMillis();
    	
    	//getting all the pixels from an image
    	int[][] pixels = new int[w][h];

    	for( int i = 0; i < w; i++ ){
      	 	 for( int j = 0; j < h; j++ ){
         		   pixels[i][j] = img.getRGB( i, j );
      	 	 }
    	}
    	
    	 // graphical output
        Graphics2D g2 = img.createGraphics();
        // use red
        g2.setColor(Color.RED);
    	
    	
    	for (int i1=r+1; i1<pixels.length; i1++) {
    	     for (int j1=r+1; j1<pixels[i1].length; j1++) {
    	         
    	    	 if(pixels[i1][j1] != pixels[i1+r][j1]
    	     			|| pixels[i1][j1] != pixels[i1-r][j1]
    	     			|| pixels[i1][j1] != pixels[i1][j1+r]
    	     			|| pixels[i1][j1] != pixels[i1][j1-r]){
    	     		
    	     		
    	     		//Question 3a). Measuring time
    	     		long timeAfter = System.currentTimeMillis();
    	     	    long elapsed = timeAfter - timeBefore;
    	     	    System.out.println("elapsed:" + elapsed);
    	     	    
    	     	   drawCircle(i1,j1,r,img,g2);
    	     	}
    	     }
    	  
    	 }
    }
}
    
 

Reply With Quote
  #6  
Old January 24th, 2013, 02:18 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Do you see what variable's content is causing the problem? When you fidn that, Then you need to check your algorithm to see why that variable has values outside the range of array indexes.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > ArrayIndexOutOfBounds error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap