0

I have integrated Display inside JFrame using Canvas and Display.setParent(canvas) method. now i would like to update loader(data) in side Display using JButton aciton listener, Doing this i have faced following error

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No OpenGL context found in the current thread.
    at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
    at org.lwjgl.opengl.GL30.glDeleteVertexArrays(GL30.java:1530)
    at renderEngine.Loader.cleanUp(Loader.java:38)
    at engineTester.MainGameLoop$2.actionPerformed(MainGameLoop.java:79)

I have found useful resource which am exactly facing right now here How to get OpenGL context into my current thread, opengl context not found I know i have to use Queue and Runnables but i never used them and dont know where to start. Can some one help me with some code or steps in doing ? or a resource where i can start and get it work.This is important for me. Thanks

Edit 1: adding program(Error thrown where i line commented inside button ActionListener)

package com.java8;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class LWJGLTester {

    private volatile boolean isRunning = false;

    private int frameWidth = 800;
    private int frameHeight = 600;
    private int displayWidth = 720;
    private int displayHeight = 450;

    private Thread glThread;

    public static void main(String[] args) {
        new LWJGLTester().runTester();
    }

    private void runTester() {
        final JFrame frame = new JFrame("LWJGL in Swing");
        frame.setSize(frameWidth, frameHeight);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                frame.setVisible(false);
                frame.dispose(); //canvas's removeNotify() will be called
            }
        });

        JPanel mainPanel = new JPanel(new BorderLayout());

        JButton button = new JButton("BUTTON");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("From button");
                loader.cleanUp();   // am trying to change the model data here which gives me the error
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(button);
        mainPanel.add(buttonPanel, BorderLayout.NORTH);

        Canvas canvas = new Canvas() {
            @Override
            public void addNotify() {
                super.addNotify();
                startGL();
            }

            @Override
            public void removeNotify() {
                stopGL();
                super.removeNotify();
            }
        };
        canvas.setPreferredSize(new Dimension(displayWidth, displayHeight));
        canvas.setIgnoreRepaint(true);

        try {
            Display.setParent(canvas);
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            //handle exception
            e.printStackTrace();
        }
        JPanel canvasPanel = new JPanel();
        canvasPanel.add(canvas);
        mainPanel.add(canvasPanel, BorderLayout.SOUTH);

        frame.getContentPane().add(mainPanel);

        //frame.pack();
        frame.setVisible(true);

    }

    private void startGL() {
        glThread = new Thread(new Runnable() {
            @Override
            public void run() {
                isRunning = true;
                try {
                    Display.setDisplayMode(new DisplayMode(displayWidth, displayHeight));
                    Display.create();
                } catch (LWJGLException e) {
                    //handle exception
                    e.printStackTrace();
                }

                // init OpenGL here

                while(isRunning) {
                    Display.sync(60);
                    Display.update();
                }

                Display.destroy();
            }
        }, "LWJGL Thread");

        glThread.start();
    }

    private void stopGL() {
        isRunning = false;
        try {
            glThread.join();
        } catch (InterruptedException e) {
            //handle exception
            e.printStackTrace();
        }
    }

}

Joshi
  • 1
  • 1
  • Did you try [this](https://stackoverflow.com/questions/14347340/no-opengl-context-found-in-the-current-thread-how-do-i-fix-this-error)? – George Z. Oct 04 '19 at 08:24
  • @GeorgeZ. yes, i dont understand how to get the context from startGL() inside button actionListener. I have edited and added part of my code. thanks. – Joshi Oct 04 '19 at 08:46

0 Answers0