0

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.

Edited by cafegeo

2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by NormR1
0

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

0

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.

Edited by NormR1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.