Loading...
Logo
Processing Forum
hi. this is a pretty nooby question but i would still be very happy to get some tips...

im currently trying to build a program using different frames.

in the forums i found several examples how to make various Frames via the Frame class. 
works fine so far, but all the examples seem to have to open a second PApplet to get something into the second window.

i need 4 different fullscreen windows for the program i am working on and using four different Applets with 4 different draw() methods seemed nice at first but is just producing the worst performance. (with just two Applets, 4 frames and two draw() methods its getting worse, not speaking of 3 or 4 looping Applets...)

is there a method, were i can build several Frames and draw into them via the main Applets draw function?

Im pretty sure there is a way for there must be a reason that there are Frames on the one hand and Applets on the other, but i could not find a simple, readable example.

id really appreciate any help!


Replies(4)

Dunno if this might help you, but in this post:
http://forum.processing.org/topic/is-this-a-multythreading-app
I've tweaked a code there to use Thread + PGraphics.

Dunno much about frames, but perhaps the idea of how splitting the workflow to draw in various PGraphics,
and then glue them together into only 1 PGraphics, and finally use it as background();
may give you some ideas!  

Here's the code:
Copy code
    /**
     * Threads of Particles Alt.(v5.01)
     * by MikiLake92 (2013/Jan)
     * experiments by GoToLoop
     *
     * http://forum.processing.org/topic/is-this-a-multythreading-app
     */
    
    import java.util.concurrent.atomic.AtomicInteger;
    
    final static byte    threads = 3, movement = 0100;
    final static byte    fps = 120, showFpsRate = fps>>2;
    final static int     dots = 0300000, dotsPerThread = dots/threads;
    final static color   dotColor = 0xFF, bg = 0;
    final static boolean isNoLoop = true, isDrawSync = false;
    final static boolean showInfo = false, showFps = true;
    
    PGraphics myPG;
    volatile boolean     isWaiting;
    //volatile byte        currentThread;
    final static AtomicInteger currentThread = new AtomicInteger(0);
    
    final static Particle  particles[] = new Particle[dots];
    final static Processor processes[] = new Processor[threads];
    
    void setup() {
      size(600, 400);
      frameRate(fps);
      if (isNoLoop)      noLoop();
    
      myPG = createGraphics(width, height, P2D);
    
      for ( int i=0; i!=dots; 
        particles[i++] = new Particle(movement) );
    
      for ( int i=0, start=0; i!=threads; 
        processes[i++] = new Processor(start, start += dotsPerThread) );
    
      for ( byte i=0; i!=threads; processes[i++].ignite() );
    
      println("Start!\n");
    }
    
    void draw() {
      if (isDrawSync)      isWaiting = false;
    
      if (frameCount % showFpsRate == 0)
        print(round(frameRate) + "\t");
    }
    
    void display() {
      if (showInfo)    println("\nStarting display!");
    
      background(myPG);
      myPG.background(bg);
    
      currentThread.set(0);
      //currentThread = 0;
      synchronized(g) {
        g.notifyAll();
      }
    
      if (isDrawSync)    isWaiting = true;
      if (isNoLoop)      redraw();
    
      while (isWaiting);
    
      if (showInfo)    println("Exiting display!");
    }
    
    //final class Processor extends Thread {
    final class Processor implements Runnable {
      final int start, stop;
      final PGraphics pg = createGraphics(width, height, P2D);
    
      Processor(int begin, int end) {
        start = begin;
        stop  = end;
    
        pg.beginDraw();
        pg.stroke(dotColor);
        pg.endDraw();
    
        println(start + "\t" + stop);
      }
    
      final void ignite() {
        new Thread(this).start();
    
        println("Exiting ignite!");
      }
    
      final void run() {
        while (true) {
          if (showInfo)    println("Restarting thread!");
    
          for ( int i=start, ii=stop; i!=ii; 
                particles[i++].action(pg) );
    
          myPG.image(pg, 0, 0);
          pg.background(bg, 0);
    
          if (currentThread.incrementAndGet() == threads)
            //if ( (currentThread+=1) == threads )
            display();
    
          else synchronized(g) {
            try {
              g.wait();
            }
            catch (Exception e) {
              println(e);
            }
          }
        } // infinite while loop
      } // method run()// class Processor
    
    class Particle extends PVector {
      PVector vel;
    
      Particle(int spd) {
        super( (int) random(width), (int) random(height) );
    
        vel = new PVector(
        (int) random(2, spd+1) * random(2)<1? -1:1, 
        (int) random(2, spd+1) * random(2)<1? 1:-1);
      }
    
      void action() {
        action(g);
      }
    
      void action(PGraphics pg) {
        if (x>width  || x<0)    vel.x *= -1f;
        if (y>height || y<0)    vel.y *= -1f;
    
        add(vel);
    
        pg.point(x, y);
      }
    }
    

thx. for the reply. i tried your code and got several problems including a weird memory acces thingy that made it crash  at pd.point(x, y);

but i tried a different approach that i got from somewhere in the forums (dont find it at the moment). its sort of an workaround. cause there are still different applets but with noLoop() called for their draw methods and redrawn in the main applets draw(). but...

now i got this code sort of working, but in the moment where i started to try to implement the cp5 stuff it kind of went crazy. the sliders appear on every start randomly in the main window OR in the EINS- Frame. 

Any ideas? i guess im making a basic mistake in the OOP-stuff (maybe while constructing?) or is it something else?

edit: I fixed it. Was pretty dumb not to see that i constructed two times the same CP5 Object.
now i initialized two of 'em and constructed each into their own applet and all is fine (for the moment)

Copy code
  1. PFrame f1;
  2. PFrame f2;
  3. PFrame f3;
  4. secondApplet EINS;
  5. thirdApplet ZWEI;
  6. fourthApplet DREI;

  7. import controlP5.*;

  8. ControlP5 cp5;
  9. Control P5 cp6;
  10. int def = 0;

  11. void setup() {
  12.  //noLoop();
  13.  size(320, 240);
  14.  frameRate = 400;
  15.  PFrame f1 = new PFrame();
  16.  PFrame2 f2 = new PFrame2();
  17.  PFrame3 f3 = new PFrame3();
  18.  
  19.   
  20.   DREI.background(0);
  21.   ZWEI.background(0,255,0);
  22.   EINS.background(0, 0, 255);

  23.  cp5 = new ControlP5(this);
  24.  cp5.addSlider("abc").setRange(0, 255).setPosition(10,10);
  25.  cp5.addSlider("def").setRange(0, 255).setPosition(10,30);

  26. }
  27. void draw() {
  28.   background(255,0,0);
  29.    fill(255);
  30.    rect(0,frameCount,30,0);
  31.    EINS.redraw();
  32.    ZWEI.redraw();
  33.    DREI.redraw();
  34.    
  35. }
  36. public class PFrame extends Frame {
  37.     public PFrame() {
  38.         setBounds(100,100,400,300);
  39.         EINS = new secondApplet();
  40.         add(EINS);
  41.         EINS.init();
  42.         show();
  43.     }
  44. }
  45. public class PFrame2 extends Frame {
  46.     public PFrame2() {
  47.         setBounds(100,100,400,300);
  48.         ZWEI = new thirdApplet();
  49.         add(ZWEI);
  50.         ZWEI.init();
  51.         show();
  52.     }
  53. }

  54. public class PFrame3 extends Frame {
  55.     public PFrame3() {
  56.         setBounds(100,100,400,300);
  57.         DREI = new fourthApplet();
  58.         add(DREI);
  59.         DREI.init();
  60.         show();
  61.     }
  62. }

  63. public class secondApplet extends PApplet {
  64.   
  65.     public void setup() 
  66.     {
  67.         size(400, 300);
  68.         noLoop();
  69.     }
  70.     
  71.     public void draw() 
  72.     {
  73.     }
  74.     
  75.     public ControlP5 control() 
  76.     {
  77.     return cp5;
  78.     }
  79.     ControlP5 cp5;
  80. }

  81. public class thirdApplet extends PApplet {
  82.   
  83.    
  84.     public void setup() {
  85.         size(400, 300);
  86.         noLoop();
  87.     }
  88.     
  89.     public void draw() {
  90.     }
  91. }

  92. public class fourthApplet extends PApplet {
  93.   int d = 0;
  94.     public void setup() {
  95.         size(400, 300);
  96.         noLoop();
  97.         cp6 = new ControlP5(this);
  98.     
  99.      cp6.addTextfield("textValue")
  100.      .setPosition(20,170)
  101.      .setSize(200,40)
  102.      .setFont(createFont("arial",20))
  103.      .setAutoClear(false)
  104.      ;
  105.        

  106.     }
  107.     public void draw() {
  108.     }
  109. }
I still had no time to peruse your new code. Sorry!
At least it opened 4 windows here. 2 of them green & red empty!
1 slide in each of the other 2. Although once happened to be both in 1 window!

Now, it's just it's strange my thread example code haven't worked for ya.
Perhaps it's just that some parameters are too high, like...:

fps = 120 & dots = 0300000 <- you can definitely lower them.

Also the booleans -> isNoLoop = true, isDrawSync = false;
You can experiment w/ them turned on/off.  

thx again. i fixed my code and edited it (top post).

im still gonna try around with your code. but first attempts still got it crashed. more when i found the problem.

what version are you using? i am working with 2.0b6