java - 处理中的面向对象编程错误

标签 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;
    }

    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;
    }
}
<小时/>
Draw rect;

void setup() {
    size (900,600);
    //rect.display();
}

void draw() {
    background(255);
    //rect.drawing();
}
void mousePressed() {
    //rect.press();
}
void mouseReleased() {
    //rect.release();
}
void mouseDragged() {
    //rect.drag();
}

注释或“//”中的区域是我收到的错误,给我“NullPointerException”错误。 我想以某种方式了解如何将“void display()”放在“绘制矩形”下的“void setup()”下;没有任何错误。

最佳答案

您尚未在任何地方初始化rect。您应该初始化它:

void setup() {
    rect = new Draw(0, 0, 0, 0, new PImage());
    size (900,600);
    //rect.display();
}

关于java - 处理中的面向对象编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179211/

相关文章:

java - 使用确定的默认成员类型扩展 ArrayList

java - 如何从另一个事件 Android Studio 中获取数据对象

javascript - 如何删除嵌套对象属性

java - 无效的方法声明;需要返回类型

java - 为什么我的 PVector 应该保持不变,但在此草图中被修改

java - 如何监听日志中抛出的错误?

c++ - 我可以使用 void* 作为函数的参数吗?

java - 在处理层映射 GPS 点

mouse - 将屏幕坐标转换为模型坐标

java - 如何使用 Jackson 序列化注释