使用循环构造的 Java 变异器方法

发布于 2024-12-09 07:38:38 字数 1324 浏览 0 评论 0原文

所以我有一个用循环构造的类。在构造函数中,我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱人如己 2024-12-16 07:38:39

我说你扔掉它们是因为你这样做了:

  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

但是你在哪里用你创建的 FramedOval 做任何事情呢?每次创建一个新的 FramedOval 对象并让链接引用它时,之前的引用就会丢失并且可能会被垃圾收集;它被丢弃了,这没有意义。

通常你会做这样的事情:

  List<FramedOval> framedOvalList = new LinkedList<FramedOval>(); // or ArrayList
  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // *** here add the created object to some collection
    framedOvalList.add(link);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

但我没有看到你做任何类似的事情。

I say you're throwing them away because you do this:

  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

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:

  List<FramedOval> framedOvalList = new LinkedList<FramedOval>(); // or ArrayList
  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // *** here add the created object to some collection
    framedOvalList.add(link);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

But I don't see you doing anything of the kind.

≈。彩虹 2024-12-16 07:38:38

如果将其移至类(如注释代码所示),那么从哪里调用该函数?我认为这就是造成差异的原因。假设您正在执行以下操作:

  1. 删除“link.setColor(rgb);”从 while 循环内部
  2. 取消注释 public void setColor(Color c) function
  3. 创建 ChainMail 实例(例如 myChainMail)
  4. 调用 myChainMail.setColor(c)

此场景将给出您报告的结果,即只有最后一个圆圈被着色。
有两种方法可以解决该问题:

  1. 首先是您已经在做的事情
  2. 而不是“私有 FramedOval 链接;”创建“private ArrayList linkList = new ArrayList; 现在,在 while 创建 FramedOval 类型的链接变量之前。然后在 while 循环中执行 link = new FramedOval(p,12,12,c); 之后,将其添加到上面创建的 ArrayList。
    现在,在类的 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 :

  1. remove "link.setColor(rgb);" from inside the while loop
  2. Uncomment public void setColor(Color c) function
  3. Create instance of ChainMail (say myChainMail)
  4. Call myChainMail.setColor(c)

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 :

  1. First is what you are already doing
  2. Instead of "private FramedOval link;" create "private ArrayList linkList = new ArrayList; Now before while create link variable of type FramedOval. Then after you do link = new FramedOval(p,12,12,c); in while loop add this to above created ArrayList.
    Now in setColor method of your class iterate over all the items in arraylist and set color on those items
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文