Hey there. First post, haven’t figured out if there’s anywhere else I could go for similar questions, thought I should ask here before creating an issue on Processing’s github issues board.
I’ve found some really strange behavior regarding a piece of code I’m writing, and I’ve isolated the issue here. This might be a java scope issue, a Reference/Value issue, or it could be an issue caused by the way Processing is intended to work.
Rather than explain what my issue is and how I’ve tried to solve it, and since I’m quite stuck, let me just start out with a demo.
PGraphics spriteImage;
MajorObject container;
class MajorObject{
ArrayList<Object> contains;
PGraphics localCanvas;
MajorObject(){
contains=new ArrayList<Object>();
localCanvas=createGraphics(180,30);
}
void addObject(Object takingInObject){
contains.add(takingInObject);
}
PImage displayObjects(){
localCanvas.beginDraw();
localCanvas.clear();
for(int i=0; i<contains.size();i++){
localCanvas.image(contains.get(i).getImage(),0,20*i);
}
localCanvas.endDraw();
return localCanvas;
}
void printContainer(){
image(displayObjects(),10,110);
}
PImage getOImage(int i){
return contains.get(i).getImage();
}
}
class Object{
PImage storedImage;
Object(PImage takingInImage){
storedImage=takingInImage;
}
PImage getImage(){
return storedImage;
}
}
void setup(){
frameRate(2);
size(200,150);
spriteImage=createGraphics(180,10);
spriteImage.beginDraw();
spriteImage.stroke(255);
spriteImage.fill(0);
spriteImage.rect(0,0,179,9);
spriteImage.endDraw();
container=new MajorObject();
container.addObject(new Object(spriteImage));
container.addObject(new Object(spriteImage));
}
void draw(){
background(0);
spriteImage.beginDraw();
spriteImage.line(frameCount,0,frameCount,10);
spriteImage.endDraw();
image(spriteImage,10,10); //1 display from global object [line 60]
image(container.displayObjects(),10,30); //2, 3 display buffer from container [line 61]
image(container.getOImage(0),10,70); //4 get value directly from Object's child [line 62]
image(container.getOImage(1),10,90); //5 possibly referencing original global obj [line 63]
container.printContainer(); //6, 7 run container's print of c's buffer [line 64]
}
In this demo, I have an image spriteImage in the global scope. I also have a class MajorObject that can contain objects of another type called Object, which are simply containers that store images.
I’d like to be able to have MajorObject be a kind of UI Frame that can hold several UI components. There are several ways to implement this but the route that I’m going down.
I’d like several UI Objects to be able to pull from the same image and synchronize all changes between them. The way I thought about doing this would be to pass a reference to every new UI Object with that Image as a PImage object. I might also want it to be a PGraphics object so it is editable. In my example, setup() creates these objects and puts them inside of MajorObject.
What’s happening in my code is I can (by line number)
60. print the original Image object
61. print a buffer stored in MajorObject that is composed of multiple Object’s images
62/63. print the image object inside of each Object container directly (acts like line 60 for some reason)
64. print the buffer than MajorObject receives internally after compositing the Object’s stored images
The main issue I’m having: running image(globally scoped or locally referenced image, int, int) separates what the global definition of the original PGraphics object from whatever references are made to the original object
For Example: Running line 60/62/63 first makes it so all future changes made to spriteImage in the global scope don’t apply to any of the references to spriteImage that may exist inside of any of the objects.
Running lines 61/64 first makes it so all future changes made to spriteImage in the global scope don’t apply to any of the references to spriteImage that may exist in the global scope.
Running any of these lines in setup instantly separates the references made and makes it so changes to spriteImage only affect the references made to global scope
Running none of these lines allows all of them to stay synchronized until image() is run somewhere. (try it out, wrap lines 60-63 in an if(key==‘a’) statement and after a few seconds of runtime press the ‘a’ key
Anyways what I expected to happen was for all of these to remain references and stay synchronized OR for every new Object with it’s run to Object’s constructor to act as a copy constructor and not pass the image object by reference but neither of those things happened.
I’d love to get help in understanding this behavior in any way possible. I can already think of an easy fix for the purpose of what I’m doing (have the constructor for an object act as a copy constructor in order to make it so nothing passes by reference) but I’d also love the ability to forcibly keep everything a reference in order to shrink memory consumption.