JFrame with canvas problem

Started by abcdef, September 28, 2011, 07:02:06

Previous topic - Next topic

abcdef

Hi

I spoke to some people on IRC last night, I've made some headway but getting a LWJGL error at the moment which I don't know how to solve

I have a test setup

// imports
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;

/**
 *
 * 
 */
public class JFrameTester extends JFrame
{
    private Canvas canvas = new Canvas();
    final AtomicReference<Dimension> newCanvasSize = new AtomicReference<Dimension>();
    private boolean running = false;
    private Thread renderThread;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                try
                {
                    //load the JFrame
                    JFrameTester bv = new JFrameTester ();                    
                    bv.initComponents(); 
                    bv.startRenderer ();
                } catch (Exception ex)
                {
                    ex.printStackTrace();
                    Logger.getLogger(JFrameTester.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    /** Creates new form BsaViewFrame */
    public JFrameTester() throws Exception
    {                               
    }

    private void initComponents() throws LWJGLException
    {
        canvas = new Canvas()
        {

            public final void addNotify()
            {
                super.addNotify();
                startRenderer();
            }

            public final void removeNotify()
            {
                stopRenderer();
                super.removeNotify();
            }
        };

        canvas.addComponentListener(new ComponentAdapter()
        {

            @Override
            public void componentResized(ComponentEvent e)
            {
                newCanvasSize.set(canvas.getSize());
            }
        });

        addWindowListener(new WindowAdapter()
        {

            @Override
            public void windowClosing(WindowEvent e)
            {
                running = false;
            }

            @Override
            public void windowGainedFocus(WindowEvent e)
            {
                canvas.requestFocusInWindow();
            }
        });
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.setFocusable(true);
        canvas.requestFocus();
        canvas.setIgnoreRepaint(true);
        canvas.setVisible(true);
        JPanel canvasPanel = new JPanel();
        canvasPanel.setBackground(Color.yellow);        
        canvasPanel.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
        JPanel controlsPanel = new JPanel();
        controlsPanel.setBackground(Color.red);

        add(canvasPanel, BorderLayout.CENTER);
        add(controlsPanel, BorderLayout.SOUTH);
                 
        pack();
        setSize(640, 480);
        setVisible(true);
        
    }

    private void startRenderer()
    {

        renderThread = new Thread()
        {

            public void run()
            {
                running = true;

                try
                {
                    Display.setParent(canvas);
                    Display.create ();
                } catch (LWJGLException ex)
                {
                    Logger.getLogger(JFrameTester.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        renderThread.start();
    }

    private void stopRenderer()
    {
        running = false;
        try
        {
            renderThread.join();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}


I definitely set the JFrame to visible=true but I get an error

org.lwjgl.LWJGLException: Parent.isDisplayable() must be true
	at org.lwjgl.opengl.Display.createWindow(Display.java:301)
	at org.lwjgl.opengl.Display.create(Display.java:899)
	at org.lwjgl.opengl.Display.create(Display.java:808)
	at org.lwjgl.opengl.Display.create(Display.java:769)
	at org.lwjgl.opengl.Display.create(Display.java:790)
	at com.test.JFrameTester$5.run(JFrameTester.java:144)


Can anyone help solve this problem? I'm using the 2.8 nightly build at the moment.

kappa

The java awt canvas must be visible before you can set and create the Display on it. That error indicates that you are creating the Display before the canvas is visible. You seems to be using multiple threads so it might be that the Display creation thread is running before the AWT Canvas thread has created and displayed the Canvas.

abcdef

you'll notice in the main method there is

bv.initComponents(); 
bv.startRenderer ();


the initComponents method is called and is finished before startRender creates the render thread. This init method sets both the canvas and JFrame to visible to true.


abcdef

Hi, I have seen that link. I based my code on that link and some other resources on the web.

Anyway I have fixed that problem. But there are other underlying problems because I am running on a Mac (I think!)

If I run my program I get the following error

2011-09-28 18:53:38.679 java[13781:fd03] Make pbuffer: 0 x 0

When I run the program in the link I get

2011-09-28 18:55:40.194 java[13787:1303] Make pbuffer: 1024 x 752
2011-09-28 18:55:40.236 java[13787:903] invalid drawable

(similar to mine)

I suspect it could be to do with the changes done for Lion but I wouldn't want to speculate.