从主 class 中的矩形 class 调用方法

call a method from a Rectangle class in the main class

我正在尝试从 Main Class 中的 Rectangle Class 中检索方法,但它不起作用。目标是用户单击“矩形”按钮,然后出现一个“矩形”。但是,矩形不能再次消失。此外,用户应该能够更改属性(如大小和位置)。

我不确定是否应该尝试回调函数?非常感谢您的帮助!

提前致谢!

import controlP5.*;
import de.bezier.data.sql.*;

Rectangle rect;
Button rc;


int posX = 50; 
int posY = 60; 
int w = 100;
int h = 150;

//Rectangle r = new Rectangle(posX, posY, w, h);

public void drawButton() {
   fill(230, 230, 255);
   stroke(0);
   rect(50, 60, 100, 150);
   
  

}
public boolean isMouseInsideRectangleButton() {
   return mouseX > posX && mouseX < posX + w
           && mouseY > posY && mouseY < posY + h;
}


ControlP5 cp5;

SQLite db;

void setup(){
  size(1000, 1000);
  //background(255);
  PFont font = createFont("Calibri", 15);
  
  rect = new Rectangle(250, 500, 100, 100);

  
  cp5 = new ControlP5(this);
  
  cp5.addButton("Save").
  setPosition(770, 20).
  setSize(100, 30).
  setFont(font).
  setColorBackground(color(52, 55, 76));
  
  cp5.addButton("erase").
  setPosition(890, 20).
  setSize(100, 30).
  setFont(font).
  setColorBackground(color(52, 55, 76));
  
  cp5.addButton("Upload").
  setPosition(650, 20).
  setSize(100, 30).
  setFont(font).
  setColorBackground(color(52, 55, 76));
  
  
  rc = cp5.addButton("Rectangle").
  setPosition(5,4).
  setSize(100, 20).
  setFont(font).
  setColorBackground(color(52, 55, 76));
   
   rc.onRelease(new CallbackListener() {
     public void controlEvent(CallbackEvent theEvent)
     
     {
       rect.draw();
     }
     
   
}
   );
   
}
  
  
  
  
  //cp5.addButton("Triangle").
  //setPosition(5,42).
  //setSize(100, 20).
  //setFont(font).
  //setColorBackground(color(52, 55, 76));
  
  //cp5.addButton("Ellipse").
  //setPosition(165, 4).
  //setSize(100, 20).
  //setFont(font).
  //setColorBackground(color(52, 55, 76));
  
  //cp5.addButton("circle").
  //setPosition(165,42).
  //setSize(100, 20).
  //setFont(font).
  //setColorBackground(color(52, 55, 76));







void draw(){
  
  background(255);
  fill(246, 246, 246);
  stroke(246, 246, 246);
  rect(0,0, 1000, 80);
  
 

  
 


//public void mousePressed() {
//  if (isMouseInsideRectangleButton()) {
//      //r.setDrawColorful(!r.getDrawColorful());
//      r.draw();
//  }
  
}


public class Rectangle
{
  private int posX, posY, w, h;
  
  
  private boolean drawColorful = false;
  
  
  //constructor
  
  public Rectangle(int posX, int posY, int w, int h) {
    this.posX = posX;
    this.posY = posY;
    this.w = w;
    this.h = h;
  
}

public boolean getDrawColoful(){
  return this.drawColorful;
}

public void setDrawColorful(boolean flag) {
    this.drawColorful = flag;
  }
  
  
  public void setPosX(int posX){
    this.posX = posX;
  }
  
  public int getPosX(){
    return posX;
  }
  
   public void setPosY(int posY){
    this.posY = posY;
  }
  
  public int getPosY(){
    return posY;
  }
  
   public void setW(int w){
    this.w = w;
  }
  
  public int getW() {
    return w;
  }
  
  public void setH(int h) {
    this.h = h;
  }
  
  public int getH() {
    return h;
  }

    
  
  
  
  

public void draw() {

 if (this.drawColorful) {
      fill(255,255,0);
      stroke(0,0,255);
      
      
      
    } else {
      fill(255,255,255);
      stroke(0,0,0);
    }

rect(posX, posY, w, h) ;




}

}

它确实有效(当您单击按钮时 rect.draw() 函数会触发)。您可以通过在回调函数中放置打印语句来验证这一点:

rc.onRelease(new CallbackListener() {
  public void controlEvent(CallbackEvent theEvent)
  {
    println("rect.draw() fired!");
    rect.draw();
  }   
});

您的问题是矩形仅在单击按钮时的那个瞬间绘制。您的主 draw() 循环一直在循环而不调用 rect.draw() 所以您再也看不到它了。

您可以通过多种方式重构代码以将矩形保留在屏幕上。一种选择是在单击按钮之前不实例化矩形。

然后,在您的主绘制循环中,您可以检查矩形是否存在,如果存在,则将其绘制到屏幕上。由于这会在每个循环中发生,您将始终在屏幕上看到它(一旦创建),并且您可以更新矩形的尺寸并在屏幕上看到它更新。

这里有一个简短的例子来说明我的意思(Rectangle class 未显示):

import controlP5.*;

Rectangle rect; // rect begins as null
Button rc;

ControlP5 cp5;

void setup(){
  size(1000, 1000);

  PFont font = createFont("Calibri", 15);
  
  // don't create the rectangle here!
  //rect = new Rectangle(250, 500, 100, 100);

  cp5 = new ControlP5(this);
 
  // ... setup other controls here
  
  rc = cp5.addButton("Rectangle").
  setPosition(5,4).
  setSize(100, 20).
  setFont(font).
  setColorBackground(color(52, 55, 76));
   
  rc.onRelease(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent)
    {
      // only create the rectangle when the button is clicked
      rect = new Rectangle(250, 500, 100, 100);
    }
  });

}
  
void draw(){
  background(255);
  fill(246, 246, 246);
  stroke(246, 246, 246);
  rect(0,0, 1000, 80);
  
  // if the rect exists, draw it on the screen
  if(rect != null) {
     rect.draw(); 
  }
}