Java 处理过程中的面向对象编程错误
我知道我做错了什么,但我似乎无法修复它Java 处理过程中的面向对象编程错误,java,oop,processing,Java,Oop,Processing,我知道我做错了什么,但我似乎无法修复它 class Draw { int x,y; int xp,yp; PImage image; Draw(int dragx, int dragy, int movex, int movey, PImage a) { x = dragx; y = dragy; xp = movex; yp = movey; image = a; }
class Draw {
int x,y;
int xp,yp;
PImage image;
Draw(int dragx, int dragy, int movex, int movey, PImage a) {
x = dragx;
y = dragy;
xp = movex;
yp = movey;
image = a;
}
void display() {
smooth();
background(255);
fill(255);
rect (0,180,40,40);
fill(0);
rect (0,240,40,40);
image = get();
stroke(0);
fill(255);
}
void drawing() {
background(image);
float sizex = xp - x;
float sizey = yp - y;
if (mousePressed && mouseButton == LEFT) {
rect(x, y, sizex, sizey);
}
}
void press() {
x = mouseX;
y = mouseY;
}
void release() {
xp = mouseX;
yp = mouseY;
noLoop();
image = get();
loop();
}
void drag() {
xp = mouseX;
yp = mouseY;
}
}
注释或“/”中的区域是我得到的错误,给了我“NullPointerException”错误。
我想了解如何将“void display()”放在“Draw rect”下的“void setup()”下;没有任何错误。您没有在任何地方初始化
rect
。您应该初始化它:
void setup() {
rect = new Draw(0, 0, 0, 0, new PImage());
size (900,600);
//rect.display();
}
您需要为
rect
创建一个Draw
对象以供参考。另外,我不认为您已经在这里展示了所有的代码-似乎存在漏洞。还要记住,在OOP类中,名称不应该是像Draw这样的动词。谢谢,这非常有帮助。但现在我又犯了一个新的错误。。。其中“void drawing()”下的“background(image)”表示“背景图像必须与应用程序大小相同”。我现在要试着弄清楚。
void setup() {
rect = new Draw(0, 0, 0, 0, new PImage());
size (900,600);
//rect.display();
}