HI,
first thanks for your help
Sorry it’s a bit complicated and I’m french so my english is a bit ~.
I’m writing a little test but as you can see in the following thread http://www.java-gaming.org/forums/index.php?topic=12784.0
I’m running mac os 10.4.5 and I’ve got another problem (but maybe it is the same problem !) :
I can’t resize a window containing a GLJPanel.
Until now I haven’t noticed this in my soft (though i noticed it in the demos)
because I implemented a different resize method.
With my method there isn’t problems while resizing the window (because the window isn’t resized until the user released the mouse button)
but maybe this method produces the bug I’m trying to describe you (context sharing), I don’t know maybe it erases something
With the following code you can test both bugs.
It creates 5 random display lists.
The user can choose which one he wants to see.
There are two resize methods (which can be changed via the MODE variable)
* The default one is a home-made window resize method (the window isn’t resized until the user released the mouse button)
* The other one is the default system window resize method (it doesn’t work here, I’ve got an endless spinning wheel)
Each time the user choose a number in the combo, a different list is shown and the window in randomly resized.
The black button on the right is the home-made resize button.
From time to time (both with the automatic resize method and the home-made resize button) the init method is called and my display lists vanish.
With the second resize method (MODE = RESIZE_ENABLED) i can’t resize the window manually
but with the random resize I can see that when the init method is called display lists vanish.
Hope it’s clear
Hope you can reproduce this
Hope there’s a solution
Léo
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.media.opengl.DefaultGLCapabilitiesChooser;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.GLPbuffer;
import javax.media.opengl.glu.GLU;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestSharingGLJPanel extends JPanel implements GLEventListener, ItemListener
{
// resize mode variables
private final static int RESIZE_DISABLED = 0;
private final static int RESIZE_ENABLED = 1;
private int MODE = RESIZE_DISABLED; // CHANGE THE MODE TO SEE THE DIFFERENT BEHAVIOURS
private JButton resizeBox;
public int sizW = 400;
public int sizH = 400;
private int dTX, dTY;
private Cursor curs;
private int width;
private int height;
private int margin = 2;
private float max = 100;
// display lists variables
private int listID = 0;
private int listToDisplay = 1;
private JFrame frame;
// opengl variables
private GLU glu = new GLU();
private GL gl;
private GLJPanel glp;
private GLPbuffer glpb;
public static void main(String[] args)
{
new TestSharingGLJPanel();
}
public TestSharingGLJPanel()
{
// create a dummy GLPBuffer for context sharing
glpb = GLDrawableFactory.getFactory().createGLPbuffer(new GLCapabilities(), new DefaultGLCapabilitiesChooser(), 1, 1, null);
// create the GLJPanel with the GLContext of the GLPBuffer
glp = new GLJPanel(null, null, glpb.getContext());
glp.addGLEventListener(this);
// GUI
setLayout(new GridLayout(1, 1));
add(glp);
// Combox box for choosing the list to display
JComboBox combo = new JComboBox(new Integer[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5) });
combo.addItemListener(this);
// the "force build lists" button
JButton forceBuild = new JButton("Build Lists");
forceBuild.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// this will force lists to be build once more in the display method
listID = 0;
glp.display();
}
});
// the frame containing all
frame = new JFrame("This is just a test !");
// adds the GLJPanel
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(this, BorderLayout.CENTER);
// panel containing the combo box, the "build lists" button and the resize box)
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
southPanel.add(combo, BorderLayout.CENTER);
southPanel.add(forceBuild,BorderLayout.WEST);
// resize mode
switch(MODE)
{
case RESIZE_ENABLED :
frame.setResizable(true);
break;
case RESIZE_DISABLED :
frame.setResizable(false);
addResizeBox(combo, southPanel);
break;
}
frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(sizW, sizH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// A non-system resize box
private void addResizeBox(JComboBox combo, JPanel southPanel)
{
resizeBox = new JButton();
resizeBox.setBorder(BorderFactory.createRaisedBevelBorder());
resizeBox.setBackground(Color.DARK_GRAY);//.createLineBorder(Color.BLACK));
resizeBox.setSize(new Dimension(14, 14));
resizeBox.setPreferredSize(new Dimension(14, 14));
resizeBox.setMaximumSize(new Dimension(14, 14));
resizeBox.setMaximumSize(new Dimension(14, 14));
resizeBox.setMargin(new Insets(0, 0, 0, 0));
resizeBox.setVisible(true);
resizeBox.setEnabled(true);
resizeBox.setVerticalAlignment(JButton.BOTTOM);
resizeBox.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
// the window will not be resized until the mouse button has been released
resizeBox.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
dTX = e.getX();
dTY = e.getY();
curs = getCursor();
resizeBox.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
}
public void mouseReleased(MouseEvent e)
{
sizW += (e.getX() - dTX);
sizH += (e.getY() - dTY);
resizeBox.setCursor(curs);
frame.setSize(sizW,sizH);
}
});
southPanel.add(resizeBox, BorderLayout.EAST);
}
// when the combo chosen item change
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
try
{
listToDisplay = ((Integer) e.getItem()).intValue();
// automatically resize the window
Random rand = new Random();
int k = (int)(rand.nextFloat() * 200) - 100;
frame.setSize(frame.getWidth()+k,frame.getHeight()+k);
glp.display();
}
catch (ClassCastException ex)
{
}
}
public void init(GLAutoDrawable drawable)
{
System.out.println("init");
gl = drawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(1,1,1,1);
gl.glPointSize(5);
gl.glViewport(0, 0, (int) width, (int) height);
gl.glEnable(GL.GL_POINT_SMOOTH);
gl.glEnable(GL.GL_BLEND);
gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glMatrixMode(GL.GL_PROJECTION);
}
// class for building random lists (in my soft of course those list aren't random)
public void buildLists()
{
listID = gl.glGenLists(5);
float stepSize = 10;
Random rand = new Random();
for (int i = listID; i < listID + 5; i++)
{
System.out.println("creating list : " + i);
gl.glNewList(i, GL.GL_COMPILE);
gl.glColor4f(0, 0, 0, 1);
gl.glLineWidth(1);
gl.glBegin(GL.GL_LINE_STRIP);
for (int j = 0; j <= stepSize; j++)
{
gl.glVertex2f(j * 10, rand.nextFloat() * 100);
}
gl.glEnd();
gl.glEndList();
}
}
public void display(GLAutoDrawable drawable)
{
gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluOrtho2D(0, max, 0, 100 + 2 * margin);
drawMargins(gl);
// flag for creating new lists if they've been destroyed and if the user clicks on "Build Lists"
if (listID == 0)
{
System.out.println("init list");
buildLists();
}
// displaying the random lists
if (listToDisplay > 0 && listToDisplay <= 5)
{
System.out.println("calling list : " + listToDisplay);
gl.glCallList(listToDisplay);
}
gl.glFlush();
}
private void drawMargins(GL gl)
{
gl.glColor4f(0.5f,0.5f,0.5f,0.5f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex2f(0, 100 + margin);
gl.glVertex2f(max, 100 + margin);
gl.glVertex2f(max, 100 + 2 * margin);
gl.glVertex2f(0, 100 + 2 * margin);
gl.glEnd();
gl.glBegin(GL.GL_LINES);
gl.glVertex2f(0, 50 + margin);
gl.glVertex2f(max, 50 + margin);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex2f(0, 0);
gl.glVertex2f(max, 0);
gl.glVertex2f(max, margin);
gl.glVertex2f(0, margin);
gl.glEnd();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
this.width = width;
this.height = height;
glu.gluOrtho2D(0, max, 0, 100 + 2 * margin);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
}
}