So I have a class that I constructed with a loop. In the constructor I have two while loops which make "grid" of circles. It's supposed to be a scarf for class but I'm calling it chainmail because that sounds cooler. Anyways we're supposed to be able to change the color of the scarf with a different (client) class. I obviously need to add a mutator method to change the color. Luckily there is a mutator for this in objectdraw called setColor(). It works just fine, except when I try to add it to this class it only changes the last circle in the grid. I know why this happens, but I don't know how to fix it. I've commented out the "typical" mutator we've been using in class.
EDIT: Sorry for the confusion guys... this is just the class, I have a client that calls a new ChainMail() and then does a .setColor() on it but it only changes the last framedoval instead of all of them. That's the problem
import objectdraw.*;
import java.awt.*;
public class ChainMail {
private FramedOval link;
public ChainMail(int rows,int links,
Location p,Color rgb,
DrawingCanvas c) {
double numRows = 0;
// create the number of rows specified
while (numRows < rows) {
double numLinks = 0;
// create the number of links specified
while (numLinks < links) {
link = new FramedOval(p,12,12,c);
link.setColor(rgb);
// update the position
p.translate(8,0);
numLinks++;
}
// move position back to front col and down one row
p.translate(-8*links,8);
numRows++;
}
}
public ChainMail(int rows,int links,Location p,DrawingCanvas c) {
this(rows,links,p,Color.BLACK,c);
}
/* this doesn't work, only changes last circle
* public void setColor(Color c) {
* link.setColor(c);
* }
*/
}
If you move it to class (as shown in commented code) then from where do you call this function ? I think this is what making the difference. Suppose you are doing following :
This scenario will give the result which you have reported i.e. only last circled gets colored. There are 2 ways to solve the issue :