sharing PGraphics with 2 windows (eclipse / intelliJ)

In the draw, if i disable image(d.pg, 0, 0); the sketch runs fine. Else i get:

Exception in thread "Animation Thread" java.lang.NullPointerException
    at java.lang.System.arraycopy(Native Method)
    at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:425)
    at processing.core.PGraphicsJava2D$ImageCache.update(PGraphicsJava2D.java:1484)
    at processing.core.PGraphicsJava2D.imageImpl(PGraphicsJava2D.java:1301)
    at processing.core.PGraphics.image(PGraphics.java:3575)
    at processing.core.PApplet.image(PApplet.java:12761)
    at MyProcessingSketch.draw(MyProcessingSketch.java:43)
    at processing.core.PApplet.handleDraw(PApplet.java:2384)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2254)
    at java.lang.Thread.run(Thread.java:695)

Can someone help?

import processing.core.*;

public class MyProcessingSketch  extends PApplet  {

    int id;

    Data d;

    public static void main(String args[]) {

        PApplet.main(new String[] { "MyProcessingSketch", "0" });
        PApplet.main(new String[] { "MyProcessingSketch", "1" });

    }

    public void init() {
        id = parseInt(args[args.length - 1]);
        PApplet.println("init:"+id);

        d = Data.getInstance();

        super.init();
    }


    public void setup() {

        size(800, 600);
    }

    public void draw() {

        background(0, 255, 0);

        if (id == 0) {
            d.createGraphics(this);
            image(d.pg, 0, 0);
        }
        else {
            if(d.pg != null) {
               // disable the next line and the sketch
               // runs fine
               image(d.pg, 0, 0);
            }
        }

        fill(255, 0, 0);
        text(frameRate, 20, 40);

    }
}

import processing.core.PApplet;
import processing.core.PGraphics;

public enum Data {

    INSTANCE;

    PGraphics pg;

    public static Data getInstance() {
        return INSTANCE;
    }


    void createGraphics(PApplet p) {
        if (pg == null);
        pg = p.createGraphics(p.width, p.height);

        pg.beginDraw();
        pg.background(255, 0, 0);
        pg.line(0, 0, p.width, p.height);
        pg.endDraw();


    }


}

Answers

  • Here a update: It works now, and framerate is high in both. But the shared image drawn is only fast in one.

    import processing.core.*;
    
    public class MyProcessingSketch  extends PApplet  {
    
        int id;
    
        Data d;
    
        public static void main(String args[]) {
    
            PApplet.main(new String[] { "MyProcessingSketch", "0" });
            PApplet.main(new String[] { "MyProcessingSketch", "1" });
    
        }
    
        public void init() {
            super.init();
    
            id = parseInt(args[args.length - 1]);
            PApplet.println("init:"+id);
    
            d = Data.getInstance();
    
            delay(1000);
    
    
        }
    
    
        public void setup() {
    
            size(800, 600);
            if (id == 0) {
                d.createGraphics(this);
            }
            smooth();
        }
    
        public void draw() {
    
    
            background(0, 255, 0);
    
            if (id == 0) {
                d.createGraphics(this);
                image(d.pg, 0, 0);
            }
    
            else {
    
                image(d.pg, 0, 0);
                fill(0, 0, 200, 50);
                for(int i = 0; i < 100; i++) {
    
                    ellipse(random(width), random(height), random(10, 50), random(10, 50));
                }
    
            }
    
            fill(255, 0, 0);
            text(frameRate, 20, 40);
    
        }
    }
    

    Data.java

    import processing.core.PApplet;
    import processing.core.PGraphics;
    
    public enum Data {
    
        INSTANCE;
    
        PGraphics pg;
    
        public static Data getInstance() {
            return INSTANCE;
        }
    
    
        void createGraphics(PApplet p) {
            if (pg == null) {
    
                pg = p.createGraphics(p.width, p.height);
                pg.smooth();
            }
    
            pg.beginDraw();
            pg.background(255, 0, 0);
            pg.line(0, 0, p.width, p.height);
            for(int i = 0; i < 100; i++) {
                pg.ellipse(p.random(p.width), p.random(p.height), p.random(10, 50), p.random(10, 50));
            }
            pg.endDraw();
    
    
        }
    
    
    }
    
Sign In or Register to comment.