Sjamb
Sjamb

Reputation: 81

Concentric Circles and Arrays

I have a follow up question to one I had posted earlier. Link

I'm not sure on the right etiquette in posting these, so forgive me if this is wrong.

I am beginning to learn arrays and am trying to get my concentric circles to now repeat 10 times (10 different circles, all with 6 rings).

Here's my code so far:

public class E4 {



    public static void main(String[] args) 
        throws FileNotFoundException {
        Scanner console = new Scanner(new File("Practice27.txt"));

        int panelX = 400, panelY = 400;
        DrawingPanel panel = new DrawingPanel(panelX, panelY);
        panel.setBackground(Color.WHITE);
        Graphics g = panel.getGraphics();
        Random r = new Random();
        int xCenter=r.nextInt(400);
        int yCenter=r.nextInt(400);

        int [][] diameters = new int[6][10];

      for(int i=0;i<diameters.length;i++){
          for(int j=0;j<diameters.length;j++){
          diameters[i][j]=console.nextInt();
          g.setColor(new Color(r.nextInt(256),r.nextInt(256), r.nextInt(256)));
            g.fillOval(xCenter-diameters[i][j]/2, yCenter-diameters[i][j]/2, diameters[i][j], diameters[i][j]);

        }

    }

}

}

So what I've tried thus far is trying to create a nested for loop that would repeat my original for loop 10 times, but that did not work. Then I tried using a multidimensional array (which I'm not really sure how they work yet, so my syntax is probably completely wrong)

I feel like the solution is what I had tried originally (the nested loop):

for(int i=0;i<diameters.length;i++){
            for(int j=0;j<=10;j++){
                diameters[i]=console.nextInt();
                g.setColor(new Color(r.nextInt(256),r.nextInt(256), r.nextInt(256)));
                g.fillOval(xCenter-diameters[i]/2, yCenter-diameters[i]/2, diameters[i], diameters[i]);

but that only leaves me with one concentric circle. Am I on the right track, at least? Thanks again!

Upvotes: 0

Views: 322

Answers (1)

StanislavL
StanislavL

Reputation: 57381

Actually you need something like this. You need just one array of diameters and for each one you create 10 circles

  int [] diameters = new int[6];

  for(int i=0;i<diameters.length;i++){
      diameters[i]=console.nextInt();
      for(int j=0;j<30;j+=3){
          g.setColor(new Color(r.nextInt(256),r.nextInt(256), r.nextInt(256)));
          g.fillOval(xCenter-diameters[i]/2-j, yCenter-diameters[i]/2-j, diameters[i]+j, diameters[i]+j);
      }
    }

Upvotes: 2

Related Questions