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.