Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理过程中的面向对象编程错误_Java_Oop_Processing - Fatal编程技术网

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();
}