【发布时间】:2021-08-16 01:07:10
【问题描述】:
我试图从主类中的矩形类中检索一个方法,但它不起作用。目标是用户单击矩形按钮,然后出现一个矩形。但是,矩形不能再次消失。此外,用户应该能够更改属性(如大小和位置)。
我不确定是否应该尝试回调函数?非常感谢您的帮助!
提前非常感谢!
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) ;
}
}
【问题讨论】:
-
Cadin 的答案在 (+1) 上。我只是想对未来的问题提出一些建议:请格式化代码(Ctrl+T / CMD + T)并尝试发布一份您的草图副本,该副本最少(不包括注释或未使用的代码)。这将使扫描/阅读变得更加容易,因此更多人将更容易提供答案。祝你好运!
标签: java oop user-interface processing implementation