drawing shapes in setup() not working

Hi guys,

I'm working on a game currently and I'm trying to setup the playground with the setup() function of processing. I tried to break down the problem into easy code.

Consider the following sketch:

 void setup(){
     surface.setSize(300, 300);
     background(0);
     fill(255);
     rect(100, 100, 30, 30);
 }

 void draw(){
 }

When i execute this code, I'd expect a canvas of size=300x300 with black background and a white rectangle of size=30x30 at x=100 and y=100 on the canvas.

However the rectangle does not appear. Could anybody clarify why you can't draw rectangles in the setup function?

I'm using Processing 3.0.2

I've already looked at the processing references pages for:
setup() : https://processing.org/reference/setup_.html
background() : https://processing.org/reference/background_.html
rect() : https://processing.org/reference/rect_.html

It also appears to me that the reference page is not updated on all the issues that may break processing 2.0 code when updating on 3.0 https://github.com/processing/processing/wiki/Changes-in-3.0#things-that-may-break-your-2x-sketches
, so I'm not sure if I'm missing something.

I haven't found anything on this issue yet. Maybe I'm just misunderstanding the behavior of the setup function.

Answers

  • Answer ✓
    void setup() {
      size(300, 300);
      background(0);
      fill(255);
      rect(100, 100, 30, 30);
    }
    
    void draw() {
    }
    
  • Wow, thanks for the quick reply! According to that github link I posted I thought that I should set the size with the surface.setSize() method. I think I just misunderstood, that the size function still works fine for setting up the processing sketch.

    Thanks so much!!!

  • edited May 2016

    PSurface's setSize() method is necessary when using variables and other expressions rather than pure values. But it takes a whole draw() callback in order for the resizing to fully complete.

  • Answer ✓

    A more practical solution is placing size() & smooth() inside settings():
    https://Processing.org/reference/settings_.html

  • Thanks for the useful links. I guess the settings option works great for me!

  • When I use
    _ size(1000, 800, P2D);

      surface.setResizable(true);
    
     // determinerequired size e.g. _screenWidth, _screenHeight
    
      println("Before resize");
    
      surface.setSize(_screenWidth, _screenHeight);
    
      println("After resize");_
    

    in setup() I get following output to the console:

    Before resize RunnableTask.run(): A caught exception occured on thread main-Display-.windows_nil-1-EDT-1: RunnableTask[enqueued true[executed false, flushed false], tTotal 0 ms, tExec 0 ms, tQueue 0 ms, attachment null, throwable java.lang.RuntimeException: Waited 5000ms for: <40e85364, 69ef6adf>[count 3, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-Display-.windows_nil-1-EDT-1>] java.lang.RuntimeException: Waited 5000ms for: <40e85364, 69ef6adf>[count 3, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-Display-.windows_nil-1-EDT-1> at jogamp.common.util.locks.RecursiveLockImpl01Unfairish.lock(RecursiveLockImpl01Unfairish.java:198) at jogamp.newt.WindowImpl$ResizableAction.run(WindowImpl.java:2118) at jogamp.newt.DisplayImpl.runOnEDTIfAvail(DisplayImpl.java:450) at jogamp.newt.WindowImpl.runOnEDTIfAvail(WindowImpl.java:2782) at jogamp.newt.WindowImpl.setResizable(WindowImpl.java:2154) at com.jogamp.newt.opengl.GLWindow.setResizable(GLWindow.java:370) at processing.opengl.PSurfaceJOGL$5.run(PSurfaceJOGL.java:487) at com.jogamp.common.util.RunnableTask.run(RunnableTask.java:127) at jogamp.newt.DefaultEDTUtil$NEDT.run(DefaultEDTUtil.java:375) DefaultEDT.run(): Caught exception occured on thread main-Display-.windows_nil-1-EDT-1: RunnableTask[enqueued false[executed true, flushed false], tTotal 5002 ms, tExec 5002 ms, tQueue 0 ms, attachment null, throwable java.lang.RuntimeException: Waited 5000ms for: <40e85364, 69ef6adf>[count 3, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-Display-.windows_nil-1-EDT-1>] java.lang.RuntimeException: Waited 5000ms for: <40e85364, 69ef6adf>[count 3, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-Display-.windows_nil-1-EDT-1> at jogamp.common.util.locks.RecursiveLockImpl01Unfairish.lock(RecursiveLockImpl01Unfairish.java:198) at jogamp.newt.WindowImpl$ResizableAction.run(WindowImpl.java:2118) at jogamp.newt.DisplayImpl.runOnEDTIfAvail(DisplayImpl.java:450) at jogamp.newt.WindowImpl.runOnEDTIfAvail(WindowImpl.java:2782) at jogamp.newt.WindowImpl.setResizable(WindowImpl.java:2154) at com.jogamp.newt.opengl.GLWindow.setResizable(GLWindow.java:370) at processing.opengl.PSurfaceJOGL$5.run(PSurfaceJOGL.java:487) at com.jogamp.common.util.RunnableTask.run(RunnableTask.java:127) at jogamp.newt.DefaultEDTUtil$NEDT.run(DefaultEDTUtil.java:375) After resize

    Is this a bug or a feature?

Sign In or Register to comment.