setup vs. draw

I'm a new user to processing (but an experienced developer). For various reasons related to teaching programming concepts to new students, I'm actually just using the processing libraries inside IntelliJ and have everything working just fine, or so I thought :-)

One of my colleagues who had just been using the Processing IDE had a simple example where they were loading an image inside the setup() method as follows:

        size(400, 400);
        img = loadImage("cat.jpg");//load image
        image(img, 0,0);

and indeed the image would be drawn.

However, the same thing does not work if we run this directly as a Java program and in particular, the call "image(img, 0, 0)" has to be inside the draw() method rather than the setup() method.

Now, that actually wasn't a surprise to me and indeed in the online documentation for PImage it's quite clear that the call SHOULD be inside the draw() method. My recommendation to my colleague is to do it right and put the call in draw()!

However, I'm still curious as to why it works in the setup() in processing but not in Java. I noted that if I don't include an @Override version of draw() but put a call to draw() at the end of the setup() method, the image does draw, although very pixelated.

There must be some extra behind-the-scenes stuff going on. I looked for some calls for explicitly redrawing/repainting (found redraw() which didn't seem to do anything).

I realize there's an ANTLR preprocessor involved and perhaps some extra code is being injected into the end of the setup() method.

Thanks in advance.


  • edited September 2015

    For any troubleshooting request it's an understatement that much more details should be given.
    How is the "Java" code exactly? Which Processing version is being used in IntelliJ, etc.

  • I was trying to avoid a lot of details if they weren't necessary.

    But by your asking that question, I went back and looked at the actual versions of the core.jar file that my colleague was using (and so which I used to check her code) and it turns out she was using a different version, a newer one.

    So let me ask the question a different way. Is it the case that calling image() from inside setup() used to work but is not supposed to work any more (per the example I saw for processing 3.0, for example)?

  • Realized from looking at the exported Java that no extra code is being inserted into the setup() method --- just some scaffolding and adding attributes like @Override

  • edited September 2015

    Processing 3 besides being in beta and having some slightly new syntax, it's becoming more strict too.
    AFAIK, that excerpt should work up till Processing 3.0a5 version.
    After that it needs some more testing. Nothing's decided yet till stable is released.

  • My advice is export some sketch via CTRL+SHIFT+E and study the ".java" source code it generates.

  • Yes, I did that --- indeed that is precisely how I got a "sketch" working in IntelliJ in the first place, just pulled in the exported Java code and added the processing jars.

    My guess is the problem was due to a mismatch of the jars.

    Thanks for your feedback.

Sign In or Register to comment.