How to draw a rect() in a Thread

Hello,

I am trying to draw a rectangle in the window from a Thread.

void setup(){ size(100,100); T thread = new T(); thread.start(); } class T extends Thread{ void run(){ rect(10,10,10,10); } }

But this didn't work. I hobe someone can help me?

Thank you!

Answers

  • Answer ✓

    this is a bad idea. only the draw() thread should update the image.

  • edited January 2018 Answer ✓

    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    /**
     * Threaded PGraphics (v1.1.1)
     * GoToLoop (2018/Jan/03)
     *
     * Forum.Processing.org/two/discussion/25799/
     * how-to-draw-a-rect-in-a-thread#Item_2
     */
    
    static final float FPS = 10;
    final Layer layer = new Layer();
    
    void setup() {
      size(250, 200);
      smooth(3);
      frameRate(FPS);
    
      colorMode(RGB);
      imageMode(CORNER);
    
      layer.start();
      while (layer.pg == null || layer.pg.height != height)  delay(1);
    }
    
    void draw() {
      getSurface().setTitle("Frames: " + frameCount);
      background((color) random(#000000));
      image(layer.pg, 0, 0);
    }
    
    class Layer extends Thread {
      static final short INTERVAL = 1*1000, TXT_SIZE = 40;
      static final float BOLD = 2.5;
      static final color BORDER = 0, TXT_COLOR = -1;
    
      PGraphics pg;
    
      void run() {
        initDisplay();
        displayLoop();
      }
    
      void initDisplay() {
        pg = createGraphics(width, height, JAVA2D);
        pg.beginDraw();
    
        pg.smooth(3);
        pg.colorMode(RGB);
        pg.rectMode(CENTER);
    
        pg.strokeWeight(BOLD);
        pg.stroke(BORDER);
    
        pg.textSize(TXT_SIZE);
        pg.textAlign(CENTER, CENTER);
    
        pg.endDraw();
      }
    
      void displayLoop() {
        final int cx = pg.width>>1, cy = pg.height>>1;
    
        for (int frames = 1;; delay(INTERVAL), ++frames) {
          pg.beginDraw();
    
          pg.clear();
          pg.fill((color) random(#000000));
          pg.rect(cx, cy, cx, cy);
    
          pg.fill(TXT_COLOR);
          pg.text(frames, cx, cy);
    
          pg.endDraw();
        }
      }
    }
    
  • edited June 11
    """
     Threaded PGraphics (v1.1.3)
     GoToLoop (2018/Jan/03)
    
     Forum.Processing.org/two/discussion/25799/
     how-to-draw-a-rect-in-a-thread#Item_3
    """
    
    from java.lang import Thread
    
    FPS = 10
    
    def setup():
        size(250, 200)
        smooth(3)
        frameRate(FPS)
    
        colorMode(RGB)
        imageMode(CORNER)
    
        global layer
        layer = Layer()
        layer.start()
        while not layer.pg or layer.pg.height != height: delay(1)
    
    
    def draw():
        this.surface.title = 'Frames: ' + `frameCount`
        background(int(random(PImage.ALPHA_MASK)))
        image(layer.pg, 0, 0)
    
    
    class Layer(Thread):
        INTERVAL, TXT_SIZE = 1*1000, 40
        BOLD, BORDER, TXT_COLOR = 2.5, 0, -1
    
        def __init__(self): self.pg = None
    
        def run(self): self.initDisplay(), self.displayLoop()
    
        def initDisplay(self):
            pg = self.pg = createGraphics(width, height, JAVA2D)
            pg.beginDraw()
    
            pg.smooth(3)
            pg.colorMode(RGB)
            pg.rectMode(CENTER)
    
            pg.strokeWeight(Layer.BOLD)
            pg.stroke(Layer.BORDER)
    
            pg.textSize(Layer.TXT_SIZE)
            pg.textAlign(CENTER, CENTER)
    
            pg.endDraw()
    
    
        def displayLoop(self):
            pg, frames = self.pg, 1
            cx, cy = pg.width>>1, pg.height>>1
    
            while True:
                pg.beginDraw()
    
                pg.clear()
                pg.fill(int(random(PImage.ALPHA_MASK)))
                pg.rect(cx, cy, cx, cy)
    
                pg.fill(Layer.TXT_COLOR)
                pg.text(frames, cx, cy)
    
                pg.endDraw()
    
                delay(Layer.INTERVAL)
                frames += 1
    
  • What koogs said is correct. You should not draw from multiple threads. You can do some processing of data on another thread, but all drawing needs to be done from the animation thread.

  • Answer ✓

    Thank you for the anwseres. I solved the problem another way.

    Thank you

Sign In or Register to comment.