Eclipse Multiple Frames avoid null Frame value.

edited June 2014 in Questions about Code

This is a followup to my Frame=Null frame.setState(...) issue answered in http://forum.processing.org/two/discussion/5836/eclipse-papplet-frame-setstateiconified-not-working-as-frame-null#Item_5

Here is the Eclipse code for creating 2 frames. Both will by Iconified with a mouseclick.

import java.awt.Frame;

import processing.core.PApplet;


public class Part1 extends PApplet
{
    SecondFrame frame2;
    public static void main(String args[])
    {
        PApplet.main(new String[]
        {"Part1" });
    }

    public void setup()
    {
        size(300, 500);
        frame2 = new SecondFrame(550,250,500,300,"Second Frame");
    }

    public void draw()
    {
        background(125);
        text("Hello World!", 30, 30);
    }

    public void mousePressed()
    {
        frame.setState(Frame.ICONIFIED);
    }
}

with the added class

import java.awt.Frame;
import java.awt.BorderLayout;

import processing.core.*;

public class SecondFrame extends java.awt.Frame
{
    int xLoc;
    int yLoc;
    int h;
    int w;
    String name;
    SecondApplet frame2;

    SecondFrame( int x_, int y_, int w_, int h_, String name_)
    {
        super("Embedded PApplet");
        xLoc = x_;
        yLoc = y_;
        w = w_;
        h = h_;
        name = name_;
        frame2 = new SecondApplet();
        add(frame2, BorderLayout.CENTER);
        frame2.init();
        setBounds(xLoc, yLoc, w, h);
        setTitle(name);
        setVisible(true);
    }

    public class SecondApplet extends PApplet
    {
        public void setup()
        {
            background(175);
            size(500, 300);
        }

        public void draw()
        {
            background(225);
            text("Hello Universe!", 60, 40);
        }

        public void mousePressed()
        {
            setState(Frame.ICONIFIED);
        }
    }
}

2 frames

Answers

  • Answer ✓

    I couldn't find your question or request anywhere!
    Nonetheless, the link below got some examples about multi PApplet instantiations:
    http://forum.processing.org/two/discussion/4140/use-displayn-command

  • edited June 2014

    Thanks for the pointer.

    That reference shows how to have multiple PApplet instances that share the same setup() and draw() code. Cool idea. (Granted the logic applied to each frame can be controlled by if (Id == suchandsuch) ... references.)

    I was looking for an approach and permits interactive window creation, where each frame is its own PApplet with its own setup() and draw().

    Two different multiple window approaches, each having its place.


    One approach is multiple instances of the same PApplet sharing logic, the other separate instances of different PApplets each with its own logic.

    Note an array of SecondFrame secondFrames[]; could accomplish your referenced approach, but would no longer share the logic but have separate copies of the logic. This would use more memory and more cpu I would think.

  • edited June 2014

    Further investigation's confirmed that PApplet.main() can instantiate any top class (".java") as long as it extends PApplet. :-bd
    In my huge example below, I've got 3 separate apps united as 1 executable, but w/ their own canvas. Check it out: :bz

    "Multiple_PApplet_Instantiations.pde":


    /**
     * Multiple PApplet Instantiations (v1.01)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/5840/
     * eclipse-multiple-frames-avoid-null-frame-value-
     */
    
    static final void
    main(String[] args) {
      PApplet.main(concatArgs("RotatingCard", args
        , "--location=50,50"));
    
      PApplet.main(concatArgs("RotatingCars", args
        , "--location=600,300", "--display=1"));
    
      //PApplet.main(concatArgs("SquareFlower", args
      //, "--location=1300,50"));
    
      PApplet.runSketch(concatArgs("", args
        , "--location=1300,50"), new SquareFlower());
    }
    
    static final String[]
    concatArgs(String name, String[] oldArgs, String... newArgs) {
      return append(concat(newArgs, oldArgs), name);
    }
    


    "RotatingCard.java":


    import processing.core.PApplet;
    import processing.core.PImage;
    import processing.core.PGraphics;
    
    public class RotatingCard extends PApplet {
      /** 
       * Rotating Card (v2.83)
       * by  Redwire (2013/Oct)
       * mod GoToLoop
       * 
       * forum.processing.org/two/discussion/92/displaying-text-in-p3d
       * studio.processingtogether.com/sp/pad/export/ro.9oKy5N3D6zFZT/latest
       */
    
      static PApplet self;
      {
        self = this;
      }
    
      //static final float TAU = TWO_PI;   // JS
    
      static final float STEP = .01f;
      static final short FPS  = 60, SMOOTH = 4;
    
      PImage front, back;
      float angle, dir = 1f;
      boolean isPaused;
    
      public void setup() {
        size(500, 500, P3D);
        frameRate(FPS);
        smooth(SMOOTH);
        rectMode(CORNER);
    
        camera(width - 100, -100, 300, 0, 0, 0, 0, 1, 0);
    
        front = createCard(true);
        back  = createCard(false);
      }
    
      public void draw() {
        background(-1);
        rotateY(angle);
    
        showInfo();   // Java
    
        final boolean bool = angle < HALF_PI + QUARTER_PI
          | angle > TAU - QUARTER_PI/1.5f;
    
        image(bool? front : back, 0, -180);
    
        if ((angle += STEP*dir) > TAU)  angle = 0;
        else if (angle < 0)             angle = TAU;
      }
    
      public void mousePressed() {
        if      (mouseButton != LEFT)   dir *= -1f;
        else if (isPaused ^= true)      noLoop();
        else                            loop();
      }
    
      public void keyPressed() {
        mousePressed();
      }
    
      void showInfo() {   // Java
        frame.setTitle("Rotating Card \t\t\t Angle: " 
          + nf(angle, 0, 2));
      }
    
      PImage createCard(boolean hasText) {
        final PGraphics pg = createGraphics(200, 340);
    
        pg.beginDraw();
        pg.smooth(SMOOTH);
        pg.rectMode(CORNER);
        pg.background(0200);
        pg.stroke(-1);
        pg.textSize(040);
    
        pg.fill(89, 92, 255, 100);
        pg.rect(20, 20, 150, 250);
    
        if (hasText) {
          pg.fill(0xffFF0000);
          pg.text("Action", 30, 50);
    
          pg.fill(0xff00FFFF);
          pg.text("Hey", pg.width - 100, pg.height - 100);
        }
    
        pg.endDraw();
        return pg.get();
      }
    }
    

  • edited June 2014 Answer ✓

    "RotatingCars.java":


    import processing.core.PApplet;
    
    public class RotatingCars extends PApplet {
      /** 
       * Rotating Cars (v2.03)
       * by  TFGuy44 (2013/Aug)
       * mod GoToLoop
       * 
       * forum.processing.org/topic/adding-in-acceleration-and-momentum
       * studio.processingtogether.com/sp/pad/export/ro.9NBw8x3YGExwu/latest
       */
    
      static PApplet self;
      {
        self = this;
      }
    
      int gw, gh;
      float x, y, z, v;
      boolean north, south, west, east;
    
      static final float SPD = .2f, ROT = .1f, ACCEL = .98f;
    
      static final short CARS = 50;
      final Car[] cars = new Car[CARS];
    
      public void setup() {
        size(640, 480);
        smooth();
    
        gw = width;
        gh = height;
    
        x = gw>>1;
        y = gh>>1;
    
        for ( int i = 0; i != CARS; cars[i++] = new Car() );
      }
    
      public void draw() {
        background(0300);
    
        for (Car c: cars)  c.go();
    
        update();
        display();
      }
    
      public void keyPressed() {
        setKeys(keyCode, true);
      }
    
      public void keyReleased() {
        setKeys(keyCode, false);
      }
    
      void setKeys(int k, boolean decision) {
        if      (k == UP    | k == 'W')   north = decision;
        else if (k == DOWN  | k == 'S')   south = decision;
        else if (k == LEFT  | k == 'A')   west  = decision;
        else if (k == RIGHT | k == 'D')   east  = decision;
      }
    
      void update() {
        v += (north? SPD : 0) - (south? SPD : 0);
        z += (east?  ROT : 0) - (west?  ROT : 0);
    
        x = (x + gw + cos(z)*v) % gw;
        y = (y + gh + sin(z)*v) % gh;
    
        v *= ACCEL;
      }
    
      void display() {
        translate(x, y);
        rotate(z);
    
        noStroke();
        fill(-1); 
        rect(-20, -20, 40, 40);
    
        stroke(0);
        noFill();
        triangle(-15, -15, 15, 0, -15, 15);
    
        resetMatrix();
      }
    
      final class Car {
        boolean a, b, c;
        float x, y;
        float z = random(TWO_PI), v = random(5);
        final int k = (int) random(0xff000000);
    
        Car() {
          x = width>>1;
          y = height>>1;
        }
    
        void go() {
          update();
          display();
        }
    
        void update() {
          if (random(1) > .98f)   a = !a;
          if (random(1) > .95f)   b = !b;
          if (random(1) > .95f)   c = !c;
    
          if (a)   v += SPD;
          z += (b? ROT : 0) - (c? ROT : 0);
    
          x = (x + width  + cos(z)*v) % width;
          y = (y + height + sin(z)*v) % height;
    
          v *= ACCEL;
        }
    
        void display() {
          translate(x, y);
          rotate(z);
    
          noStroke();
          fill(k); 
          rect(-10, -10, 20, 20);
    
          stroke(0);
          noFill();
          triangle(-8, -8, 7, 0, -8, 8);
    
          resetMatrix();
        }
      }
    }
    


    "SquareFlower.java":


    import processing.core.PApplet;
    
    public class SquareFlower extends PApplet {
      /** 
       * Square Flower (v2.1)
       * by  Jordan Orelli (2013/Dec)
       * mod GoToLoop
       * 
       * jordanorelli.tumblr.com/post/70910152988/square-flower
       * gist.github.com/jordanorelli/8100924
       *
       * studio.processingtogether.com/sp/pad/export/ro.9AzN8ufMfio2x/latest
       */
    
      static PApplet self;
      {
        self = this;
      }
    
      static final int DIM = 040, CURVE = -010;
      static final int NUM = 050, FRAMES = 0200;
      static final float RAD = 0200, LARGE = 0120, FORM = 5;
      static final int BG = -1, FG = 0xff0000FF;
    
      static final boolean SAVE_FRAMES = false;
      static String path;
    
      public void setup() {
        size(500, 500);
        frameRate(60);
    
        smooth(4);
        rectMode(CENTER);
    
        strokeWeight(2);
        stroke(FG);
        fill(BG);
    
        path = dataPath("###.png");
      }
    
      public void draw() {
        background(BG);
        translate(width>>1, height>>1);
    
        for (int i = 0; i != NUM; drawRect(i++));
    
        if (SAVE_FRAMES & frameCount <= FRAMES)  saveFrame(path);
      }
    
      void drawRect(int i) {
        final float n1 = norm(i, 0, NUM) * TWO_PI;
        final float n2 = norm(frameCount, 0, FRAMES) * TWO_PI;
        final float offset = sin(FORM*n1 + n2) * LARGE + RAD;
    
        pushMatrix();
        rotate(n1);
        rect(offset, 0, DIM, DIM, CURVE);
        popMatrix();
      }
    }
    

Sign In or Register to comment.