Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiple GLJPanels and GLJCanvases  (Read 1204 times)
0 Members and 1 Guest are viewing this topic.
Offline anton81

Junior Newbie





« Posted 2007-04-12 16:15:54 »

Hello,

I found an example on the net where someone show how to create an application with two canvases with sharing the context.
The example , you can see it here:
http://www.cs.plu.edu/~dwolff/talks/jogl-ccsc/src/h_MultipleGLCanvases/MultipleGLCanvasesExample.java

It works fine and the code is clear Smiley
Now on my problem, I'm developping a program and I would like to use a multiple GLJPanels and not GLCanvases. That because I'm using an JInternalFrame and GLCanvas can't work with the internal frame.
So I tried to make the same example but with GJJPanel ... but it doesn't work good. When I'm trying to put the second GLJPanel, there is an exception...
This is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


import com.sun.opengl.util.FPSAnimator;



public class MultipleGLJPanelExample extends JFrame implements ActionListener {
   
   private static final long serialVersionUID = 0L;

   // The animator object
   private FPSAnimator anim1, anim2;
   
   // Frames per second
   private int fps = 30;
   
   // The rotation angle
   private double angle = 0.0;
   
   // Start and stop button
   private JButton startStopButton;
   
   // The ID for the shared display list
   private int cubeList;
   
   /*
    * Start or stop both animators.
    */

   public void actionPerformed( ActionEvent e )
   {
      if( startStopButton.getText() == "Start")
      {
         anim1.start();
         //anim2.start();
         startStopButton.setText("Stop");
      }
      else if( startStopButton.getText() == "Stop") {
         anim1.stop();
         //anim2.stop();
         startStopButton.setText("Start");
      }
     
   }
   
   public MultipleGLJPanelExample()
   {
     
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   
     
      GLJPanel mainPanel = new GLJPanel(); // this is like in the original code : mainPanel contains the 2 others panels 1 and 2
                                                                                           //when I'm using this panel here, the panel1 appears too small, and when I don't it appears fine ... I don't know why :(
                                                                                           //this is another one of my problems with this application
     
      GLJPanel panel1 = new GLJPanel();
      panel1.setSize(200, 200);
      panel1.addGLEventListener(new Panel1Listener());
      mainPanel.add(panel1);
      panel1.requestFocus();
      anim1 = new FPSAnimator(panel1,fps);
      anim1.start();

     
      //here I'm trying to put the second JPanel
      /*
      GLJPanel panel2 = new GLJPanel(null,null,panel1.getContext()); // and here I have the exception, compile the code and you'll see it
      panel2.setSize(200,200);
      panel2.addGLEventListener(new Panel2Listener());
      mainPanel.add(panel2);
      panel1.requestFocus();
      anim2 = new FPSAnimator(panel2,fps);
      anim2.start();
      */

     
      JPanel buttonPanel = new JPanel();
      buttonPanel.add( startStopButton = new JButton("Stop"));
      startStopButton.addActionListener(this);
      getContentPane().add( buttonPanel, BorderLayout.SOUTH );
     
      getContentPane().add( mainPanel, BorderLayout.CENTER );
     
     
      setSize(600,600);
      setVisible( true );
   }
   
   private class Panel1Listener implements GLEventListener
   {
      // Cube data
      private float[][] cubeData = { {-0.5f, -0.5f, 0.5f}, {0.5f, -0.5f, 0.5f},
            {0.5f, 0.5f, 0.5f}, {-0.5f, 0.5f, 0.5f}, {-0.5f, -0.5f, -0.5f},
            {0.5f, -0.5f, -0.5f}, {0.5f, 0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f}
      };
     
      private GLU glu = new GLU();
     
      public void init( GLAutoDrawable d ) {
         GL gl = d.getGL();
         gl.glClearColor( 0,0,0,0 );
         
         float[] lightAmbient = {0.4f, 0.4f, 0.4f, 1.0f};
         float[] lightDiffuse = {0.8f, 0.8f, 0.8f, 1.0f};
         float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
         float[] lightPosition = {0.0f, 0.0f, 10.0f, 1.0f};
         
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightSpecular,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition,0);
         
         gl.glEnable(GL.GL_DEPTH_TEST);
         gl.glEnable(GL.GL_LIGHT0);
         gl.glEnable(GL.GL_LIGHTING);
         
         gl.glCullFace(GL.GL_BACK);
         gl.glEnable(GL.GL_CULL_FACE);
         gl.glMatrixMode( GL.GL_MODELVIEW );
         gl.glLoadIdentity();
         
         // Create the cube display list
         cubeList = gl.glGenLists(1);
         gl.glNewList(cubeList, GL.GL_COMPILE);
         drawCube(gl);
         gl.glEndList();
      }
     
      public void reshape( GLAutoDrawable d, int x, int y, int width, int height ) {
         GL gl = d.getGL();
         gl.glMatrixMode( GL.GL_PROJECTION );
         gl.glLoadIdentity();
         glu.gluPerspective(60.0, (float)width/height, 0.5, 100.0);
         gl.glMatrixMode( GL.GL_MODELVIEW );
      }
     
      /*
       * Draw the scene.
       */

      public void display( GLAutoDrawable d ) {

         angle += 1.0;
         if( angle > 180.0 ) angle -= 360.0;
         
         // Draw the frame
         GL gl = d.getGL();
         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
         gl.glLoadIdentity();
         glu.gluLookAt(0,2.0,2.0,0,1.0,0,0,1,0);
         drawFloor(gl,true);
         gl.glTranslated(0,1,0);
         gl.glRotated(angle,0.5,0.5,-0.5);
         gl.glCallList(cubeList);
      }
     
      private void drawCube(GL gl)
      {
         gl.glBegin(GL.GL_QUADS);
            // Front face
            gl.glNormal3f(0,0,1.0f);
           
            gl.glVertex3fv(cubeData[0],0);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[2],0);
            gl.glVertex3fv(cubeData[3],0);
            // Back face  
            gl.glNormal3f(0,0,-1.0f);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[7],0);
            gl.glVertex3fv(cubeData[6],0);
            // Top face
            gl.glNormal3f(0,1.0f,0.0f);
            gl.glVertex3fv(cubeData[3],0);
            gl.glVertex3fv(cubeData[2],0);
            gl.glVertex3fv(cubeData[6],0);
            gl.glVertex3fv(cubeData[7],0);
         gl.glEnd();
         
         gl.glBegin(GL.GL_QUADS);
            // Left face
            gl.glNormal3f(-1.0f, 0.0f,0.0f);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[0],0);
            gl.glVertex3fv(cubeData[3],0);
            gl.glVertex3fv(cubeData[7],0);
            // Right face
            gl.glNormal3f(1.0f,0.0f,0.0f);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[6],0);
            gl.glVertex3fv(cubeData[2],0);
            // Bottom face
            gl.glNormal3f(0,-1.0f,0.0f);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[0],0);
         gl.glEnd();
      }

      public void displayChanged( GLAutoDrawable d, boolean modeChanged,
         boolean deviceChanged ) { }

   }
   
   private class Panel2Listener implements GLEventListener
   {      
     
      private GLU glu = new GLU();
     
      public void init( GLAutoDrawable d ) {
         d.setGL(new DebugGL(d.getGL()));
         GL gl = d.getGL();
         gl.glClearColor( 0,0,0,0 );
         
         float[] lightAmbient = {0.4f, 0.4f, 0.4f, 1.0f};
         float[] lightDiffuse = {0.8f, 0.8f, 0.8f, 1.0f};
         float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
         float[] lightPosition = {0.0f, 0.0f, 10.0f, 1.0f};
         
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightSpecular,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition,0);
         
         gl.glEnable(GL.GL_DEPTH_TEST);
         gl.glEnable(GL.GL_LIGHT0);
         gl.glEnable(GL.GL_LIGHTING);
     
         gl.glCullFace(GL.GL_BACK);
         gl.glEnable(GL.GL_CULL_FACE);
         gl.glMatrixMode( GL.GL_MODELVIEW );
         gl.glLoadIdentity();        
      }
     
      public void reshape( GLAutoDrawable d, int x, int y, int width, int height ) {
         GL gl = d.getGL();
         gl.glMatrixMode( GL.GL_PROJECTION );
         gl.glLoadIdentity();
         glu.gluPerspective(60.0, (float)width/height, 0.5, 100.0);
         gl.glMatrixMode( GL.GL_MODELVIEW );
      }
     
      public void display( GLAutoDrawable d ) {      
         // Draw the frame
         GL gl = d.getGL();
         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
         gl.glLoadIdentity();
         glu.gluLookAt(10.5,10.5,10.5,0,1,0,0,1,0);
         drawFloor(gl,true);
         gl.glTranslated(0,1,0);
         gl.glRotated(angle,0.5,0.5,-0.5);
         gl.glCallList(cubeList);
      }

      public void displayChanged( GLAutoDrawable d, boolean modeChanged,
         boolean deviceChanged ) { }
     
   }
   
   private void drawFloor(GL gl, boolean wireframe) {
      int xdivs = 50;
      int zdivs = 50;
      double xmin = -10.0, xmax = 10.0;
      double zmin = -10.0, zmax = 10.0;
      double x1, x2, y = 0.0, z;
      double xsize = (xmax - xmin) / xdivs;
      double zsize = (zmax - zmin) / zdivs;
     
      if( wireframe ) {
         gl.glPushAttrib(GL.GL_LIGHTING_BIT | GL.GL_POLYGON_BIT
               | GL.GL_COLOR_BUFFER_BIT | GL.GL_TEXTURE_BIT);
         gl.glDisable(GL.GL_LIGHTING);
         gl.glDisable(GL.GL_TEXTURE_2D);
         gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
         gl.glColor3f(1.0f,0.2f,0.2f);
      }
     
      for( int i = 0; i < xdivs ; i++ ) {
         x1 = (i * xsize) + xmin;
         x2 = ((i+1) * xsize) + xmin;
         
         gl.glBegin(GL.GL_QUAD_STRIP);
         for(int j = 0; j <= zdivs; j++) {
            z = (j * zsize) + zmin;
            gl.glVertex3d(x2,y,z);
            gl.glVertex3d(x1,y,z);
         }
         gl.glEnd();
      }
     
      if(wireframe) {
         gl.glPopAttrib();
      }
   }
   
   public static void main( String[] args ) {

      new MultipleGLJPanelExample();
   }

}


Any ideas ?


Regards,

Anton
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (56 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!