Hello everyone. I am starting a class that utilizes Java for software design. I have no previous java experience, although I have taken c++ prior to this and appreciate all the help I have received in the past. I also would like to thank everyone in advance for all your help and attention.

I have a program which randomly displays ellipses in a frame when a certain JButton is pressed. The paintComponent below colors in the ellipse. In this paintComponent I have pasted below there is a standard For loop. We have to change that to a For|In (int i : args) loop. I am not at all familiar with that kind of loop and am having trouble implementing it. I would appreciate any help or advice.

public void paintComponent(Graphics g){               
        Graphics2D g2 = (Graphics2D)g;                    
        Shape shapes[] = new Shape[count];
        g2.setPaint(Color.white);
        Random randomGenerator = new Random();

        for(int i=0, i < count, i++){                    *//For loop that needs to be changed to For|In*
            int newX = randomGenerator.nextInt(200);
            int newY = randomGenerator.nextInt(200);
            shapes[i] = new Ellipse2D.Float(50+newX, 50+newY, 100, 50);    
            g2.setPaint(Color.red);
            g2.fill(shapes[i]);
                     }
        }
    }

    // Set the value of count to reflect which button was pressed.

    public void actionPerformed(ActionEvent e){       
        if(e.getSource() == buttons[0]){
            count = 1;
        }
        if(e.getSource() == buttons[1]){
            count = 2;
        }
        if(e.getSource() == buttons[2]){
            count = 3;
        }
        if(e.getSource() == buttons[3]){
            count = 4;
        }
        if(e.getSource() == buttons[4]){
            count = 5;
        }
        repaint();
    }
}

Thank you in advance.

Re: change "for" loop to enhanced "for|in" loop 80 80

See the tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

What collection or array do you want to get elements from?

Re: change "for" loop to enhanced "for|in" loop 80 80

I'm not exactly sure, but buttons[] is an array. Could I use that or should I be creating a new array?

Re: change "for" loop to enhanced "for|in" loop 80 80

Where is the collection of elements(for example an array) you want to go through in the for loop?
The idea of looping is to get the elements one at a time from a collection so each can be processed in its turn.

Be a part of the DaniWeb community

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