Pregunta

I know I am doing something wrong but I can't seem to fix it.

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

The areas in comments or "//" are the errors that I get, giving me 'NullPointerException' error. I want to somehow understand how I can put 'void display()' under 'void setup()' under Draw rect; without any errors.

¿Fue útil?

Solución

You have not initialized rect anywhere. You should initialize it:

void setup() {
    rect = new Draw(0, 0, 0, 0, new PImage());
    size (900,600);
    //rect.display();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top