1

所以,我正在使用 eclipse 和 processing 来做一些 Java 中较重的编码,但是我的派生类有点麻烦——

我有一个带有成员变量 parent 的直方图类,它是运行程序的主要 PApplet。处理已经有一个很好的 MouseClicked 事件,我希望我的直方图类能够有自己的 onclicked 方法。

所以这是一个大问题:如何让 MouseClicked 事件渗透到我的对象?

public RunOverview(PApplet p, float[] simBuckets, float[] pointBuckets, int xP, int yP, int len, int hi)
{
    this.parent = p;
    this.xPos = xP;
    this.yPos = yP;
    this.height = hi; 
 }
// SOMEHOW LISTEN FOR parent.MouseClicked()........

提前致谢!

4

1 回答 1

1

现在,您的RunOverview类存储对PApplet. 你也可以做相反的事情,让PAppletstore 引用RunOverview实例!在您的构造函数中,您可以调用一些registerOverview(this)在处理代码中定义的函数,以将引用保存在PApplet. 然后,当鼠标函数被调用时,你可以直接RunOverview从那里调用 's 函数!

public RunOverview(PApplet p, float[] simBuckets, float[] pointBuckets, int xP, int yP, int len, int hi)
{
    this.parent = p;
    this.xPos = xP;
    this.yPos = yP;
    this.height = hi; 
    p.registerOverview(this);
 }
 public void mousePressed(int x, int y){}
 public void mouseReleased(int x, int y){}

进而

RunOverview thingy;
void setup(){}
void draw(){}
void registerOverview(RunOverview view){
  thingy = view;
}
void mousePressed(){
  thingy.mousePressed(mouseX,mouseY);
}
void mouseReleased(){
  thingy.mouseReleased(mouseX,mouseY);
}

只要确保在你做任何其他事情之前注册它,否则你会得到一些空指针异常。

于 2013-10-26T20:14:05.870 回答