I cannot get the bullseye to replicate itself 5 times even though I've already set the count to 5... any suggestions?

import java.awt.*;
public class Bullseye {

    /** Creates a new instance of Main */
    public Bullseye() 
    {
        DrawingPanel Target = new DrawingPanel(300,400);
        Graphics g = Target.getGraphics();
        bullseye(g,100,100,50,5);
    }

   public static void bullseye(Graphics g, int x, int y, int diam, int count)
    {
        int r = diam/2;
        int xloc = x;
        int yloc = y;

        boolean change = true;
        for(int j=0;j<count;j++)
        {
            g.drawOval(xloc+(j*((1/count)/2)),yloc+(j*((1/count)/2)),r+(j*((1/count)/2)),r+(j*((1/count)/2)));
            if(change)
            {
                g.setColor(Color.BLACK);
               change = false;
            }
            else
            {
                g.setColor(Color.RED);
                change = true;
            }  

        }

    }

Just a couple of minor changes and you're good to go. First off, int division got you on the 1/count part. This stays zero unless you cast "count" to float. I moved the calc to a variable called "offset". Also, since it was changing by a percentage of diameter, you need to multiply by "r" to get the effect you wanted and I removed the divide by 2, so it filled the area evenly.

The only other changes here are that I moved the drawing call to be after the setColor(), which is usually what you want to do, and I changed it to fillOval() instead of drawOval() so it wasn't just line drawings.

public static void bullseye(Graphics g, int x, int y, int diam, int count) {
    int r = diam;   // because fillOval uses width and height, use diam here
    int xloc = x;
    int yloc = y;

    boolean change = true;
    for(int j=0;j<count;j++) {
        // must cast int divisor to float
        // then cast the final result back to int.
        // also multiplying by r so it's a percentage of diam
        int offset = (int)(r*(j*(1/(float)count)));
        if(change) {
            g.setColor(Color.BLACK);
            change = false;
        } else {
            g.setColor(Color.RED);
            change = true;
        }
        g.fillOval(xloc+offset/2, yloc+offset/2, r-offset, r-offset);
    }
}

Thanks! I got it start actually growing but I couldn't get it lined up. I was wondering how the (int) and (float) work in this line of code:

int offset = (int)(r*(j*(1/(float)(count))));

Thanks! I got it start actually growing but I couldn't get it lined up. I was wondering how the (int) and (float) work in this line of code:

int offset = (int)(r*(j*(1/(float)(count))));

Those cast variables or ( ) blocks of calcs to a specific type. The inner (float) casts the int variable "count" to a float value, so that the fractional calc is performed correctly. The outer level (int) casts the entire calc back to an int value so that it can be assigned to the int offset variable.

Without the cast, an int divided by a larger int will always result in zero because the fractional portion of the value is truncated (0.1 => 0).

Hope that explains it okay.

Ya you explained it crystal clear, makes sense now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.