Using createGraphics in Eclipse

I have a class I'd like to call createGraphics on but I get the following error: The method createGraphics(float, float, String) is undefined for the type SceneChild

I'm not sure how to implement it? I can't make the class an extension of PApplet, as this SceneChild class will be instantiated by another which will be an extension of PApplet

import processing.core.*;

public class SceneChild
{
    PApplet parent;

    PGraphics graphics_container;

    public SceneChild(PApplet _parent, float _w, float _h)
    {
        parent = _parent;
        graphics_container = createGraphics(_w, _h, parent.P3D);
    }

    public void display()
    {

    }
}

Answers

  • edited April 2015

    Why don't you use your field parent, which recieves a PApplet reference btW, in order to call main sketch's functions? parent.createGraphics(_w, _h, PConstants.P3D);

    Better yet, rather than your class be responsible to call createGraphics(), demand an already instantiated PGraphics as constructor's parameter:

    public SceneChild(PApplet pa, PGraphics pg) {
      parent = pa;
      graphics_container = pg;
    }
    
  • edited April 2015

    An even shorter version, relying on capturing sketch's reference outta the PGraphics itself: *-:)

    public SceneChild(PGraphics pg) {
      parent = pg.parent;
      graphics_container = pg;
    }
    
  • Thanks for your suggestions. I would like the SceneChild class to be a wrapper for PGraphics so I tried your first suggestion again, as it didn't work for me. But changing the dimensions from float to int I think has fixed it. Thanks again!

  • I would like the SceneChild class to be a wrapper for PGraphics...

    Even being a wrapper, it doesn't mean it has to create the PGraphics by itself.
    It's a very viable option to just ask an already existing PGraphics instance instead! ;)

  • Yep true. But what if the wrapper was a few levels down? PApplet class > Scene class > SceneChild class

    What would be the best way to instantiate PGraphics on SceneChild?

    Thanks for you comments

  • edited April 2015 Answer ✓

    In case SceneChild is the wrapper now, have a PGraphics parameter in its constructor.
    But in the case that Scene class is responsible to create SceneChild objects,
    createGraphics() is used inside the former w/ the help of the PApplet's reference.

  • edited April 2015

    Ok, I think I'm implementing as you've suggested but get a bunch of errors within newChild I think. Doesn't like children.add either. Is the scoping wrong?

    Main

    import processing.core.*;
    
    public class MainApp extends PApplet
    {
        PGraphics pg;
        Scene scene1;
    
        public void setup()
        {
            size(800, 600, P3D);
    
            scene1 = new Scene(this);
        }
    
        public void draw()
        {
            background(200, 190, 90);
    
            scene1.display();
        }
    }
    

    Scene

    import java.util.ArrayList;
    
    import processing.core.*;
    
    public class Scene
    {
        PApplet parent;
    
        ArrayList<SceneChild> children;
    
    
        public Scene(PApplet _parent)
        {
            parent = _parent;
    
            //Could be called dynamically from somewhere
            newChild(parent, 400, 300);
        }
    
        public void newChild(PApplet p, int _w, int _h)
        {
            SceneChild child = new SceneChild(p, _w, _h);
            children.add(child);
        }
    
        public int numChildren()
        {
            return children.size();
        }
    
        public void display()
        {
            if(children.size() > 0)
            {
                for(int i=0; i<children.size(); i++)
                {
                    children.get(i).display();
                }
            }
    
        }
    }
    

    SceneChild (wrapper for PGraphics in this example)

    import processing.core.*;
    
    public class SceneChild
    {
        PApplet parent;
        PGraphics container;
    
        public SceneChild(PApplet p, int _w, int _h)
        {
            parent = p;
    
            container = parent.createGraphics(_w, _h, PConstants.P3D);
        }
    
        public void display()
        {
            container.fill(0,0,200);
            container.ellipse(50, 50, 50, 50);
        }
    }
    
  • edited April 2015

    Hello !

    I started to write to you yesterday but my computer crashed before the end of the message. I thought it was "dead" but it was "saved by the forum". In the timeline of the message, it should be after the first message of GoToLoop.

    I think - but maybe I'm wrong - that what clifford is looking for is

    import processing.core.*;
    
    public class SceneChild
    {
        PApplet parent;
    
        PGraphics graphics_container;
    
        public SceneChild(PApplet _parent, float _w, float _h)
        {
            parent = _parent;
            graphics_container = parent.createGraphics(_w, _h, parent.P3D);
        }
    
        public void display()
        {
    
        }
    }
    

    "The method createGraphics(float, float, String) is undefined for the type SceneChild"

    It means exactly what it means :) You trying to call a function called "createGraphics" inside a class called "SceneChild" without any function "createGraphics" inside it, so there is a problem.

    Your issue come from the fact that when you're using the Processing IDE instead Eclipse, it's like you are inside the PApplet class, even if you are in another class. That's why you can call "createGraphics" from everywhere if you work with the Processing IDE, because your code is running "inside / at the scope of " the class PApplet.

    When you work in Eclipse, it's a bit different because Eclipse is a tool for developer and Processing IDE is a tool for artist / student unfamiliar with object-oriented-programming. The Processing IDE is kind of layer over the true java code, it allows the users to use the function of the PApplet class directly to get a smaller & more readable code.

    But if you use Eclipse, it's a bit different because it's not very usual to be able to call a function from a class inside another class without saying explicitly where does the functions come from.

    Anyway, don't give up ! It's not so hard ! :D The single thing you have to change if you work in Eclipse is to call the function from the instance of the class PApplet. That's what I'm doing in the code.

    The instance of PApplet is what you called "parent" , it contains every Processing functions so you can do every what you want from it ! :)

  • Sorry, I didn't read your code until the end. The problem come from the fact you didn't create your arrayList

  • edited April 2015

    The problem come from the fact you didn't create your arrayList

    Doh! Well spotted. Yes even though I understand the concept of how Processing works in Eclipse when it comes to practical it's still a bit weird to get my head around trying to work in oop but not oop in the way I'm used to :)

    Many thanks guys for your input and quick responses. Very helpful!

    Below is the code if anyone may find it useful. Thanks

    Main

    import processing.core.*;
    
    public class MainApp extends PApplet
    {
        PGraphics pg;
        Scene scene1;
    
        public void setup()
        {
            size(800, 600, P3D);
    
            scene1 = new Scene(this);
        }
    
        public void draw()
        {
            background(200, 190, 90);
    
            scene1.display();
        }
    }
    

    Scene

    import java.util.ArrayList;
    
    import processing.core.*;
    
    public class Scene
    {
        PApplet parent;
    
        ArrayList<SceneChild> children;
    
    
        public Scene(PApplet _parent)
        {
            parent = _parent;
            children = new ArrayList<SceneChild>();
            //Could be called dynamically from somewhere
            newChild(parent, parent.width, parent.height);
        }
    
        public void newChild(PApplet p, int _w, int _h)
        {
            SceneChild child = new SceneChild(p, _w, _h);
            children.add(child);
        }
    
        public int numChildren()
        {
            return children.size();
        }
    
        public void display()
        {
            if(children.size() > 0)
            {
                for(int i=0; i<children.size(); i++)
                {
                    children.get(i).display();
                }
            }
    
        }
    }
    

    SceneChild

    import processing.core.*;
    
    public class SceneChild
    {
        PApplet parent;
        PGraphics container;
    
        public SceneChild(PApplet p, int _w, int _h)
        {
            parent = p;
    
            container = parent.createGraphics(_w, _h, PConstants.P3D);
        }
    
        public void display()
        {
            container.beginDraw();
            container.fill(0,0,200);
            container.ellipse(50, 50, 50, 50);
            container.endDraw();
            parent.image(container, 0, 0); 
    
            //parent.println(container);
        }
    }
    
Sign In or Register to comment.