【问题标题】:Processing / How to make selectable object, that moves where i click处理/如何制作可选择的对象,在我点击的地方移动
【发布时间】:2016-04-10 21:16:52
【问题描述】:

这真的很糟糕......我知道如何制作类、对象、接口、循环等。但是每次我尝试制作一个或多个单元时,都会移动(当我选择它时)到我点击的地方我得到错误,错误和错误......为什么地狱那里没有教程呢?

我的新课看起来很大气:

class Unit {

    int X;
    int Y;
    int Breite;
    int Laenge;
    int ID;
    boolean ausgewaelht = false;

    Unit() {
    }

    Unit(int x, int y, int breite, int laenge) {
    }

    void create(UnitContent function) {
        function.form();
    }

    void move(float geschwindigkeit) {
        if(isTriggerd(X,Y,Breite,Laenge) == true){
            X = X+(int)geschwindigkeit;
            if(X > width) {
                X = 0;
            }
        }
    }  

    void setXandY(int x , int y) {
        X = x;
        Y = y;
    } 

    void setBreiteandLaenge(int breite, int laenge) {
        Breite = breite;
        Laenge = laenge;
    }  

    void setID(int id) {
        ID = id;
    }  

    int getX() {
        return X;
    }

    int getY() {
        return Y;
    } 

    int getBreite() {
        return Breite;
    }

    int getLaenge() {
        return Laenge;
    }    

    int getID() {
        return ID;
    }  

    boolean isTriggerd(int x, int y, int breite, int laenge) {
        if(mouseX > x && mouseX < x+breite && mouseY > y && mouseY < y+laenge ) {
            return true;
        }
       else {
            return false; 
       }
   }
}

有什么我忘记了吗? 以及如何显示 10 或 50 个单位?

抱歉我的英语不好 :) 并感谢您的帮助

【问题讨论】:

  • isTriggerd(...) 中的 if 条件似乎不正确,而不是 x,不应该是 X,而不是 mouseX,不应该是 xymouseY 也是如此。
  • 嗯刚刚尝试过,在下面的代码中,它是正确的 :) 我测试了它,当我将光标移到矩形上时,它向右移动,直到我的光标不再在对象上......但是如何显示不止一次呢? :x

标签: java windows class object processing


【解决方案1】:

一些友好的建议:你的语气听起来有点苛刻,而且你的描述很模糊。如果您尝试让其他人更容易地帮助您,您的运气会好得多。

  • 准确告诉我们您遇到了什么错误。
  • 发布MCVE 并提供足够的代码以便我们运行它,但不要发布任何与您的问题没有直接关系的代码。
  • 尝试将您的问题分解为小步骤,并且一次只提出一个具体问题。
  • 查看question checklist 并确保您已完成列表中的所有操作。

为什么没有教程呢?

请记住,在 Stack Overflow 上回答问题的人是在业余时间免费回答的。开发 Processing 的人在业余时间免费这样做。即便如此,仍有大量关于您的问题的教程。你试过用谷歌搜索吗?

Here 是一个完全符合您要求的教程。这些示例随处理编辑器一起提供(转到文件 -> 示例)。 The reference 是另一个您应该查看的好资源。

说了这么多,我将引导您解决这个问题,并希望将来如何解决其他问题。

第 0 步:将您的问题分解为更小的步骤。 这是编程的黄金法则。 当你遇到困难时,回到这一步。这一步是其余步骤的动力,当你遇到困难时,它应该是你要做的第一、第二和第三件事。

第 1 步:你能画一个物体吗?不要担心交互或多个对象或其他任何事情,只需绘制一个对象。首先让该代码工作。

这是绘制单个圆圈的代码:

void setup(){
  size(500, 500);
  ellipseMode(CENTER);
}

void draw(){
 background(0); 
 ellipse(100, 200, 50, 50);
}

第 2 步:您能否将绘制对象所需的信息封装在一个类中?同样,只需担心下一个小步骤 - 不要担心多个形状。只需让一个类为单个对象工作。以下是我们如何为我们的圈子封装数据:

Circle circle;

void setup() {
  size(500, 500);
  ellipseMode(CENTER);
  circle = new Circle(100, 200, 50);
}

void draw() {
  background(0); 

  circle.draw();
}

class Circle {
  float x;
  float y;
  float r;

  public Circle(float x, float y, float r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }

  void draw() {
    ellipse(x, y, r, r);
  }
}

如果您在这一步遇到问题,那么您可以发布类似这个带有更具体问题的小示例,这比您发布整个草图的一部分而没有任何具体错误更容易为您提供帮助.

第 3 步:你能在你的 circle 类中添加一些简单的用户交互逻辑吗?不用担心单击,只需在将鼠标移到圆圈上时尝试更改圆圈的颜色。

Circle circle;

void setup() {
  size(500, 500);
  ellipseMode(RADIUS);
  circle = new Circle(100, 200, 50);
}

void draw() {
  background(0); 

  circle.draw();
}

class Circle {
  float x;
  float y;
  float r;

  public Circle(float x, float y, float r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }

  void draw() {

    if(dist(mouseX, mouseY, x, y) < r){
      //mouse is inside circle
      fill(0, 255, 0);
    }
    else{
      //mouse is outside circle
      fill(0, 0, 255);
    }

    ellipse(x, y, r, r);
  }
}

通过将较大的问题分解为这些较小的步骤,与尝试一次编写整个草图相比,调试代码变得容易得多。

第 4 步:您能否改进交互代码以检测点击?检测到拖动时可以移动圆圈吗?

您可能应该进一步分解这些步骤,但为了保持这篇文章简短(呃),我将它们合并为一个:

Circle circle;

void setup() {
  size(500, 500);
  ellipseMode(RADIUS);
  circle = new Circle(100, 200, 50);
}

void draw() {
  background(0); 

  circle.draw();
}

class Circle {
  float x;
  float y;
  float r;

  public Circle(float x, float y, float r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }

  void draw() {

    if(dist(mouseX, mouseY, x, y) < r){
      //mouse is inside circle

      if(mousePressed){
        //mouse is being dragged
        fill(255, 0, 0);

        //move the circle to the mouse position
        x = mouseX;
        y = mouseY;
      }
      else{
        //mouse is not clicked
        fill(0, 255, 0);
      }


    }
    else{
      //mouse is outside circle
      fill(0, 0, 255);
    }

    ellipse(x, y, r, r);
  }
}

第 5 步:你能让它适用于多个对象吗?如果您已经很好地将问题分解为小步骤并将您的逻辑封装到一个类中,那么这一步将变得非常简单。

ArrayList<Circle> circles = new ArrayList<Circle>();

void setup() {
  size(500, 500);
  ellipseMode(RADIUS);

  for (int i = 0; i < 10; i++) {
    circles.add(new Circle(random(width), random(height), random(10, 50)));
  }
}

void draw() {
  background(0);

  for (Circle circle : circles) {
    circle.draw();
  }
}

class Circle {
  float x;
  float y;
  float r;

  public Circle(float x, float y, float r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }

  void draw() {

    if (dist(mouseX, mouseY, x, y) < r) {
      //mouse is inside circle

      if (mousePressed) {
        //mouse is being dragged
        fill(255, 0, 0);

        //move the circle to the mouse position
        x = mouseX;
        y = mouseY;
      } else {
        //mouse is not clicked
        fill(0, 255, 0);
      }
    } else {
      //mouse is outside circle
      fill(0, 0, 255);
    }

    ellipse(x, y, r, r);
  }
}

总而言之,您需要将您的问题分解为更小的步骤,然后一次一个地执行这些步骤。如果您在某个特定步骤上遇到困难(或者如果您不理解我回答中的某个步骤),那么您可以像我上面的示例一样发布一个小示例 MCVE,并提出一个具体问题。

编码愉快!

【讨论】:

  • 好吧,对不起,我的解释含糊不清,但我英语说得不太好:/但下次我不尊重这一切:)谢谢你的回答对我有很大帮助^^
【解决方案2】:

除了一些坐标之外,您似乎没有任何图形代码。为了在 Java 中显示某些内容,您必须使用支持它的库,例如 SwingJavaFXHere's 一个 SO 问题,可以让您了解不同之处。编码愉快!

【讨论】:

  • ok thx 老兄,我看不下去了 :) 但是你能告诉我,我需要一个 for 循环来一次显示多个单位吗?
  • 你知道如何在 Processing 中制作一个可选择的对象吗? ^^
  • 恐怕我没有,但框架的网站上有一个reference section
猜你喜欢
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多