calling the init() method...

Hi, I know the init() method is an intializing function which sorts out the graphics information on program start up. I load my objects here etc.

I was just wondering if it can be called again while running of the program? I would like to write over objects created here by loading new objects. (i.e. on the action of a button press)

There may be a better solution to this? All I can think of is calling the init() method again. But I am still unsure how to do this.

public void init(GLDrawable gLDrawable) {
      
       GL gl = drawable.getGL();     
       ...
       if (buttonPressed)
         loadNewObjects(gl);
       else
         loadObjects(gl);
    }

Thanks

Hi,

It seems to be a bad idea to call init method multiple times. Can’t you simply add a flag in your application and test it in your display method to perform your loading operations?

Hi,

Yeah that seems like a good idea. Thanks for your reply :slight_smile:

Hi again

correct me if im wrong, but if I use a flag in the dispay method does that mean objects will be loaded every time display is called? (i assume display is called constantly?)

my concern is, i load quite a few objects and it would be inefficient to load the objects everytime display is called?

any suggestions?

Hi, you certainly have to reset your flag…

ButtonDown —> flag = true

if( flag )
{
loadNewObjects(gl)
flag = false.
}

You will reload your objects only when you do an action on the button… at least i think so

One thing to remember is that the init method will get called every time you resize the canvas, so take care that any stuff you initialise there can handle being called unexpectably

Actually it will be called any time a new OpenGL context is created. For the GLCanvas this only happens when it is removed from the component hierarchy and added again. For the GLJPanel in the case where it uses pbuffers for its implementation (currently the default except with Mustang and -Dsun.java2d.opengl=true) it will call GLEventListener.init() on most, but not all, resizes.