使用循环构造的 Java 变异器方法
所以我有一个用循环构造的类。在构造函数中,我有两个 while 循环,它们构成圆圈的“网格”。它应该是课堂用的围巾,但我称之为链甲,因为这听起来更酷。不管怎样,我们应该能够用不同的(客户)类别来改变围巾的颜色。我显然需要添加一个修改器方法来改变颜色。幸运的是,objectdraw 中有一个名为 setColor() 的变体。它工作得很好,除了当我尝试将它添加到此类时,它只更改网格中的最后一个圆圈。我知道为什么会发生这种情况,但我不知道如何解决。我已经注释掉了我们在课堂上使用的“典型”变异器。
编辑:很抱歉让大家感到困惑......这只是一个类,我有一个客户端调用一个新的 ChainMail(),然后对其执行 .setColor() 但它只更改最后一个帧而不是所有帧。这就是问题所在
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);
* }
*/
}
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);
* }
*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我说你扔掉它们是因为你这样做了:
但是你在哪里用你创建的 FramedOval 做任何事情呢?每次创建一个新的 FramedOval 对象并让链接引用它时,之前的引用就会丢失并且可能会被垃圾收集;它被丢弃了,这没有意义。
通常你会做这样的事情:
但我没有看到你做任何类似的事情。
I say you're throwing them away because you do this:
But where do you do anything with the FramedOval that you've created? Every time you create a new FramedOval object and have link refer to it, the previous reference gets lost and is likely garbage collected; it's discarded, which doesn't make sense.
Usually you'd do something like:
But I don't see you doing anything of the kind.
如果将其移至类(如注释代码所示),那么从哪里调用该函数?我认为这就是造成差异的原因。假设您正在执行以下操作:
此场景将给出您报告的结果,即只有最后一个圆圈被着色。
有两种方法可以解决该问题:
现在,在类的 setColor 方法中迭代 arraylist 中的所有项目并为这些项目设置颜色
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 :
Now in setColor method of your class iterate over all the items in arraylist and set color on those items