Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:13:30+00:00 2026-06-17T21:13:30+00:00

When I run this program that is supposed to detect circles in an image

  • 0

When I run this program that is supposed to detect circles in an image I get Array index out of bounds exception at the last method when I call drawCircles()

findCircle() read the image and searches for circles and drawCircles() draws them using Bresenaham algorithm.

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();

            // you can look at pixel values with this API call:
            int c = img.getRGB(0,0); // get RGB value of pixel at (0,0)

            // you can write out pixels with the setRGB() API.  However,
            // what you get will depend on the colour model, so here
            // we use a Graphics2D object.

            // graphical output
            Graphics2D g2 = img.createGraphics();
            // use red
            g2.setColor(Color.RED);

            // call circle drawing algorithm
           drawCircle(5,5,3,img,g2);


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

         }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:13:31+00:00Added an answer on June 17, 2026 at 9:13 pm

    In your findCircle method,

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

    If r is anything but 0 this will cause an array index out of bounds exception since you’re basically looping from the 0th to the nth index. Basically anywhere you’re adding or subtracting r. For example, this is one of the statements that will cause this error

    pixels[i1+r][j1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everytime I run the program, this mysterious error pops up saying that I have
This is a program I'm writing that's supposed to display some text in a
I have a multithreaded program that's supposed to run 24/7, sometimes I would see
I have a java program that is supposed to run a bat file, and
Ok, so I have a simple c++ program that's supposed to run a couple
I have a program that is supposed to run a set of other programs
I'm trying to run this code that was supposed to be a cut and
when I run this program, it print NameError global name 'viewAll' is not defined
I have run this program before and it worked fine. Then I added the
When I run this program, I enter the username as dcole and the password

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.