Java 将MouseClick事件从Papplet传播到对象类
因此,我使用eclipse和processing在Java中进行一些较重的编码,但是我在使用派生类时遇到了一些问题-- 我有一个直方图类,它有一个成员变量parent,它是运行程序的主PApplet。处理已经有了一个很好的MouseClicked事件,我希望我的histogram类能够有自己的onclicked方法 所以这里有一个大问题:我如何让鼠标点击的事件流到我的对象Java 将MouseClick事件从Papplet传播到对象类,java,eclipse,processing,Java,Eclipse,Processing,因此,我使用eclipse和processing在Java中进行一些较重的编码,但是我在使用派生类时遇到了一些问题-- 我有一个直方图类,它有一个成员变量parent,它是运行程序的主PApplet。处理已经有了一个很好的MouseClicked事件,我希望我的histogram类能够有自己的onclicked方法 所以这里有一个大问题:我如何让鼠标点击的事件流到我的对象 public RunOverview(PApplet p, float[] simBuckets, float[] poin
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()........
提前谢谢 现在,您的
RunOverview
类存储对PApplet
的引用。您也可以执行相反的操作,让PApplet
存储对RunOverview
实例的引用!在构造函数中,您可以调用一些函数,如处理代码中定义的registerOverview(this)
,以将引用保存在PApplet
中。然后,当调用鼠标函数时,您可以从那里直接调用RunOverview
的函数
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);
}
只需确保在执行任何其他操作之前注册它,否则会出现一些空指针异常。现在,您的
RunOverview
类存储对PApplet
的引用。您也可以执行相反的操作,让PApplet
存储对RunOverview
实例的引用!在构造函数中,您可以调用一些函数,如处理代码中定义的registerOverview(this)
,以将引用保存在PApplet
中。然后,当调用鼠标函数时,您可以从那里直接调用RunOverview
的函数
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);
}
只需确保在执行任何其他操作之前注册它,否则会出现一些空指针异常