Problems with Object Oriented Programming with Processing in Eclipse

(Question copied from Stack Overflow)

So I'm desperately trying to cut my little program I made in Processing into different classes. However I fail on the basic level...

The Project:
We need to program a little game in Processing as the final test of our semester. Problem is, we mostly learn how to do stuff in Processing's editor. However in my opinion it's pretty bugged and the fact it has a pre-compiler from Processing to Java bothers me as well.

The Problem:
So I try using Eclipse instead. With the provided tutorial by on Processing's website I came pretty well along writing the pure Java code instead of the simplified Processing Syntax. However when it comes to making a second class, I'm completely lost. We also learn in university how to code in Java and we just finished object oriented programming as a topic. However we do not learn how to connect it with Processing.

The Problem-Code:
Below you can find my code I got problems with. I cut out most of the unneccessary stuff. However I've uploaded the full code to Pastebin, if you want to see it (warning: very messy).

main:

import processing.core.*;

public class animation extends PApplet{

    public static void main(String[] args) {
        PApplet.main("animation");
    }

    controls keyboard;

    //some variable initializations

    public void setup() {

        frameRate(60);

    }

    public void settings() {

        //some settings stuff

    }

    //some more variables

    boolean moving = false,
            //triggered = false;

    boolean goUp, goDown, goLeft, goRight;

    //more vars

    public void draw() {

        //unneccesary code for drawing all the stuff

        moving = false;


        keyboard.check();

        if(keyboard.goUp) {
            if(ypos > 0) {
                moving = true;
                ypos = ypos - speed * 100 * elapTime;
            }
        }

        if(keyboard.goDown) {
            if(ypos < height - images[0].height) {
                moving = true;
                ypos = ypos + speed * 100 * elapTime;
            }
        }

        if(keyboard.goLeft) {
            if(xpos > 0) {
                dir = 1;
                moving = true;
                xpos = xpos - speed * 100 * elapTime;
            }
        }

        if(keyboard.goRight) {
            if(xpos < width - images[0].width) {
                dir = -1;
                moving = true;
                xpos = xpos + speed * 100 * elapTime;
            }
        }

        if(keyPressed && key == 't' && !triggered) {
            triggered = true;
        }

    }

}

full code on pastebin

controls:

public class controls extends animation {

    boolean goUp, goDown, goLeft, goRight;

    public void check() {

        keyPressed();
        keyReleased();

    }

    public void keyPressed() {
        setMove(keyCode, true);
    }

    public void keyReleased() {
        setMove(keyCode, false);
    }

    boolean setMove(int key, boolean b) {

        switch (key) {

        case UP:

            return goUp = b;

        case DOWN:

            return goDown = b;

        case LEFT:

            return goLeft = b;

        case RIGHT:

            return goRight = b;

        default:

            return b;

        }

    }

}

full code on pastebin

The error log:

#// loaded 'data/harambe/frame0.png'
#// loaded 'data/harambe/frame1.png'
#// loaded 'data/harambe/frame2.png'
#// loaded 'data/harambe/frame3.png'
#// loaded 'data/harambe/frame4.png'
#// loaded 'data/harambe/frame5.png'
#// loaded 'data/harambe/frame6.png'
#// loaded 'data/harambe/frame7.png'
#// loaded 'data/harambe/frame8.png'
#// loaded 'data/harambe/frame9.png'
#// loaded 'data/harambe/frame10.png'
#// loaded 'data/harambe/frame11.png'
#// loaded 'data/harambe/frame12.png'
#// loaded 'data/harambe/frame13.png'
#// LOADED ALL DATA

java.lang.NullPointerException
    at animation.draw(animation.java:120)
    at processing.core.PApplet.handleDraw(PApplet.java:2412)
    at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:880)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
    at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
    at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:759)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

Thanks in advance to everyone who is willing to try solving this problem. I'm really lost at this point and tried a lot of things...

EDIT: When I change controls keyboard to controls keyboard = new controls(); i get the following error log (java.lang.StackOverflowError):

java.lang.StackOverflowError
    at controls.<init>(controls.java:1)
    at animation.<init>(animation.java:9)
    at controls.<init>(controls.java:1)
    at animation.<init>(animation.java:9)
    at controls.<init>(controls.java:1)
   at animation.<init>(animation.java:9)

Above errors repeat VERY often

Tagged:

Answers

Sign In or Register to comment.